Sending pointer to C# struct into C++ DLL - c#

I have a C++ function in a DLL which takes a pointer to a struct, JPInfo, which in the function is filled with data received from a server, the layout of the C++ struct is as seen below:
typedef struct JP
{
unsigned char type;
DWORD value;
} JP;
typedef struct JPInfo
{
JP jps[3];
_int16 ConT;
_int16 CallT;
unsigned char ret;
unsigned char count;
unsigned char JPOffset;
unsigned char JPPeriod;
} JPInfo;
The function is exported in the DLL like so:
__declspec(dllexport) DWORD __stdcall GetJPInfo(JPInfo* jpi, DWORD time);
The function takes a pointer to a JPInfo struct, I have tried to emulate this struct in C#
[StructLayout(LayoutKind.Sequential, Size = 5), Serializable]
public struct JP
{
byte type;
int value;
}
[StructLayout(LayoutKind.Sequential,Size=23),Serializable]
public struct JPInfo
{
JP[] jps;
Int16 ConT;
Int16 CallT;
byte ret;
byte count;
byte JPOffset;
byte JPPeriod;
}
I attempt to call the function from C# like so:
[DllImport("DLLImp.dll")]
unsafe public static extern int GetJP(ref JPInfo jpi, int time);
// then in main...
JPInfo jpi = new JPInfo;
GetJackpotValues(ref jpi, 4000);
I get an unhandled exception of type "System.ExecutionEngineException". I can't have a fixed size array of JP structs in my JPInfo struct, so I don't know how to approach this.
Thanks.

Have you tried removing your Size attributes on your structs? I haven't had to specify a Size when doing something similar. For your array properties, try attributing them like:
[StructLayout(LayoutKind.Sequential)]
public struct JPInfo
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
JP[] jps;
Int16 ConT;
Int16 CallT;
byte ret;
byte count;
byte JPOffset;
byte JPPeriod;
}

Assuming that the C++ structs are packed, your C# structs should look like this:
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct JP
{
byte type;
uint value;
}
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct JPInfo
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=3)]
JP[] jps;
Int16 ConT;
Int16 CallT;
byte ret;
byte count;
byte JPOffset;
byte JPPeriod;
}
On the other hand, if they are not packed then remove the Pack parameter to the StructLayout attribute. You should look for a #pragma pack statement in the C++ header file to understand whether or not the C++ structs are packed.
I'm guessing that the C++ structs are packed because you said that they are mapped onto data received from the server.
Your import should be like so:
[DllImport("DLLImp.dll")]
public static extern uint GetJP(ref JPInfo jpi, uint time);
A DWORD translates to uint rather than int and there is no need for unsafe code here.

Related

Issues in structure conversion from C++ to C# .Net Compact Framework

I am converting c/c++ structures into C# standards.
C/C++ Structures:
typedef struct _sta_conn_info{
STA_CONNECT_STATE connect_state;//Enum
STA_ASSOC_STATE assoc_state;//Enum
unsigned char bssid[6];
unsigned char ssid[34];
unsigned long channel;
enum mode mode;//Enum
unsigned long signalStrength;
unsigned long noiseLevel;
STA_AUTH_ALG auth_alg;//enum
STA_ENCRYPT_ALG encrypt_alg;//enum
}STA_CONN_INFO;
typedef struct _NDISUIO_QUERY_OID
{
NDIS_OID Oid;
PTCHAR ptcDeviceName;
UCHAR Data[sizeof(ULONG)];
} NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID;
Respective C# structures:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct _sta_conn_info
{
public _sta_connect_state connect_state;
public _sta_assoc_state assoc_state;
[MarshalAs(UnmanagedType.ByValArray,SizeConst = 6)]
public char[] bssid ;//= new char[6];
[MarshalAs(UnmanagedType.ByValArray,SizeConst = 34)]
public char[] ssid ;//= new char[34]
public uint channel;
public mode mode;
public uint signalStrength;
public uint noiseLevel;
public _sta_auth_alg auth_alg;
public _sta_encrypt_alg encrypt_alg;
}
QUERY STRUCT:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct _NDISUIO_QUERY_OID
{
public uint Oid;
[MarshalAs(UnmanagedType.LPWStr)]
public string ptcDeviceName;
public byte[] Data;
};
I converted the data types using this >>>reference
Marshal.SizeOf() is working in WIN CE. I tested it.
If my structure conversion is fine then definitely Marshal.SizeOf() will work to get the size of the structure, but it is throwing exceptions and returning error code 87 in DeviceIoControl() API.
Can anyone clarify me about the conversions and let me know If I did anything wrong.
For bssid and ssid the C++ declarations are:
unsigned char bssid[6];
unsigned char ssid[34];
Now, unsigned char is a single byte and is typically used for byte arrays rather than text. So the C# should be:
[MarshalAs(UnmanagedType.ByValArray,SizeConst = 6)]
public byte[] bssid ;//= new byte[6];
[MarshalAs(UnmanagedType.ByValArray,SizeConst = 34)]
public byte[] ssid ;//= new byte[34]
Your use of char in the C# is not correct because char is two bytes wide in C#.
In _NDISUIO_QUERY_OID where you have
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = sizeof(uint))]
public byte[] Data;
I believe that you need to use ByValArray rather than ByValTStr. But as we have discussed in many of your recent questions, the exact meaning of this member is unclear. Is it really a fixed length byte array, or is it a variable length buffer? Do you have sample C++ code that works? That would settle the debate once and for all.
OK, from the header nuiouser.h header file I have this:
//
// Structure to go with IOCTL_NDISUIO_QUERY_OID_VALUE.
// The Data part is of variable length, determined by
// the input buffer length passed to DeviceIoControl.
//
typedef struct _NDISUIO_QUERY_OID
{
NDIS_OID Oid;
#ifdef UNDER_CE
//
// In CE land app is allowed to query without having to do
// IOCTL_NDISUIO_OPEN_DEVICE
// Hence the device name to query argument needed..
// For app that does IOCTL_NDISUIO_OPEN_DEVICE this argument
// is then not necessary..
//
PTCHAR ptcDeviceName;
#endif
UCHAR Data[sizeof(ULONG)];
} NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID;
Which tells you conclusively that Data is variable length. You'll need to allocate the struct with AllocHGlobal and do all the marshalling by hand I am afraid.

PInvoke for DWORD-aligned handles to memory

I was trying to write a C# equivalent for ACCESS_DENIED_ACE struct as defined in MSDN:
typedef struct _ACCESS_DENIED_ACE {
ACE_HEADER Header;
ACCESS_MASK Mask;
DWORD SidStart;
} ACCESS_DENIED_ACE, *PACCESS_DENIED_ACE;
Where SidStart is the first DWORD of a trustee's SID. The remaining bytes of the SID are stored in contiguous memory after the SidStart member.
I have seen examples where its used like (PSID) &accessAllowedAce->SidStart as in,
if ( EqualSid (pSid, (PSID) &accessDeniedAce->SidStart) )
{
//
}
Now how can I write the C# StructLayout for this and how can I use it in EqualSid function, also explain how your solution works.
Start with the sub structs.
[StructLayout(LayoutKind.Sequential)]
struct ACE_HEADER
{
byte AceType;
byte AceFlags;
uint AceSize;
}
DWORD = uint
[Flags]
enum ACCESS_MASK : uint
{
// ...
}
Its a flag based enum struct to be represented as 32 bits (i.e. multiple states can be toggled).
You'll need to define the bits as documented.
[StructLayout(LayoutKind.Sequential)]
struct ACCESS_DENIED_ACE
{
ACE_HEADER Header;
ACCESS_MASK Mask;
uint SidStart;
}
Simple structure now.
When I get the IntPtr for ACCESS_DENIED_ACE (assume deniedAceIntPtr), I can get the IntPtr for SID in the ACCESS_DENIED_ACE by adding the offset of the SidStart
IntPtr tempSid = IntPtr.Add(deniedAceIntPtr, 8);

Create empty native C++ byte* in C# to PInvoke with

I need to create a binary blob of empty data to PInvoke a native C++ dll that needs a unsigned char* of nulls.
The native C++ program is expecting a structure of data, and there's a nulled area of bytes in the middle, but in C# I can't just make a struct with an initialized byte[] in the middle.
My struct in C++ looks like this
struct myStruct
{
byte command;
byte returncode;
void* Source (a pointer to a string, rather a null term char*)
void* Destination (same thing)
byte filler [99]
byte options;
}
I've already figured that I can take a string and convert it to an array of bytes using myStruct.Source = (void*)Marshal.StringtToGlobalAnsi(source) (correct me if I'm wrong).
But I don't know how to fill out that empty array of bytes in the middle.
This is my C# struct so far.
[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct Trans_s
{
public byte command;
public byte returnCode;
public void* pSource;
public void* pDest;
public byte* filler;
public byte options;
}
For the array of bytes, you need to mark the field in the C# struct as a byvalarray. The default marshaller uses LPArray if you do not.
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 99)]
public byte[] filler;
You need to allocate the memory for filler when you create an instance of the struct.
No, the C++ code doesn't expect an unsigned char* of nulls. It expects that many padding bytes inside the struct.
The C# fixed keyword might help you here.

Interop Structure: Should Unsigned Short be Mapped to byte[]?

I have such a C++ structure:
typedef struct _FILE_OP_BLOCK
{
unsigned short fid; // objective file ID
unsigned short offset; // operating offset
unsigned char len; // buffer length(update)
// read length(read)
unsigned char buff[240];
} FILE_OP_BLOCK;
And now I want to map it in .Net. The tricky thing is that the I should pass a 2 byte array for fid, and integer for len, even though in C# fid is an unsigned short and len is an unsigned char
I wonder whether my structure ( in C#) below is correct?
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
public struct File_OP_Block
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] fid;
public ushort offset;
public byte length;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 240)]
public char[] buff;
}
Your CharSet property on the [DllImport] attribute is definitely wrong, you need CharSet.Ansi to get the P/Invoke marshaller to convert it to a char[]. Declare the buff member as a string for easier usage. While declaring the fid member as a byte[] isn't wrong, I really don't see the point of it. That the unmanaged code copies a char[] into it is an implementation detail. Thus:
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct File_OP_Block
{
public ushort fid;
public ushort offset;
public byte length;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 240)]
public string buff;
}
Given that the C++ short data type is actually two bytes, a two byte array should work. The integer sizes in C/C++ are not strictly defined, so the standard only says that a short is at least two bytes.
The C# char data type is a 16 bit unicode character, so that doesn't match the C++ char data type which is an 8 bit data type. You either need an attribute to specify how the characters are encoded into bytes, or use a byte array.
You might need an attribute to specify the packing, so that there is no padding between the members.

Marshaling a struct while keeping it "unmanaged"

I'm p-invoking into a DLL that returns a void** list of struct pointers, all of the same type. From what I've read, in order to cast and get my structure out of that list, the struct needs to be considered unmanaged. The main culprits to the struct I'm trying to marshal over are the following two fields from the C side:
char name[1024];
int crop[4];
Most guides suggest using string or int[] on the corresponding struct on the managed side, but that having those fields makes it into a managed struct and thus incapable of extracting from the void** list.
What's another way I can marshal these fields that gives me an unmanaged struct?
The structure will marshal without help or need for the unsafe keyword if you declare it like this:
using System.Runtime.InteropServices;
...
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Example {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
public string name;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
int[] crop;
}
Convert the void* to the structure with Marshal.PtrToStructure().
You can use the fixed keyword to create a buffer with a fixed size array in a data structure:
unsafe struct Foo
{
public fixed byte name[1024];
public fixed int crop[4];
}
static unsafe void DumpCrops(void** ptr, int count)
{
Foo** p = (Foo**)ptr;
for (int i = 0; i < count; i++)
{
Foo* f = p[i];
for (int j = 0; j < 4; j++)
{
Console.WriteLine(f->crop[j]);
}
}
}
You need to add a line in that point on the struct as shown...
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct _FOOBAR {
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 1024, ArraySubType = System.Runtime.InteropServices.UnmanagedType.I2)]
char name[1024];
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = System.Runtime.InteropServices.UnmanagedType.I4)]
int crop[4];
};
You need to double check on the last bit in the attribute bit, UnmanagedType...
Hope that helps,
Best regards,
Tom.

Categories