I got some problems translating a C Union to C#. Here's the definition of the Union:
union Info
{
char varChar[8];
short varShort[4];
int varInteger[2];
float varFloat[2];
double varDouble;
__int64 varInteger64;
char varSzText[8];
};
And here is one of my tries in C#:
[StructLayout(LayoutKind.Explicit)]
public struct Info
{
[FieldOffset(0)]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string varChar;
[FieldOffset(0)]
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I2, SizeConst = 4)]
public short[] varShort;
[FieldOffset(0)]
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I4, SizeConst = 2)]
public int[] varInteger;
[FieldOffset(0)]
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.R4, SizeConst = 2)]
public float[] varFloat;
[FieldOffset(0)]
public double varDouble;
[FieldOffset(0)]
public long varInteger64;
[FieldOffset(0)]
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.LPStr, SizeConst = 8)] //LPStr, LPWStr, BStr oder LPTStr
public string[] varSzText;
};
I probably do everything wrong here. I read something about reference- and value types and that they are treated differently in the memory layout. Since most of the fields here are arrays, I guess, those are value types. Which means they are not all at [FieldOffset(0)]?
Another headache is the last field varSzText.
I got a sample C/C++ programm which uses this field as follows:
printf("Text: %s\n", &info.varSzText[0]);
My C isn't very good but if my interpretation is right, then char varSzText[8]; stores addresses to (\0-terminated) strings. The sample program prints at least a 20-something character string.
Could somebody tell me, how to marshal this union?
I googled and searched the forum before posting but mostly I found really simple unions with simple types (no arrays/strings). Does anyone know a good read about pinvoke/marshaling?
EDIT:
I found out, that the union is only used within a struct which has a type-field which gives me a hint on what is actually stored in the union. Therefore I'll stick with Deduplicator's answere. I'll create different structs for each field and parse the union according to the type-field.
Your problem is that a union can be many things, and the programmer decides what it is in any given moment (and better not screw it up).
As it is not tagged, there is no inherent way to decide.
So, the way to go is:
Decide what option it is.
Marshall accordingly.
(You might want to use a C++/CLI wrapper instead for flexibility in the more interesting cases.)
Related
I'm trying to read/edit Diablo's save file. Specification here, if anyone's interested, but I dont think it is relevant to the question.
I have a byte array with the file bytes that i'm trying to parse to some structs. I can already read the file header fine, btu im having trouble with the quests data. I got there structs:
[StructLayout(LayoutKind.Explicit, Size = 10, Pack = 1)]
public struct QuestCompletationDataHeader {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
[FieldOffset(0)]
public string Identifier;
[FieldOffset(4)]
uint _0x0004;
[FieldOffset(8)]
short _0x008;
}
[StructLayout(LayoutKind.Explicit, Size = 96, Pack = 1)]
public struct QuestData {
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 96, ArraySubType = UnmanagedType.U1)]
[FieldOffset(0)]
byte[] _0x0000; //Irrelevant for now.
}
[StructLayout(LayoutKind.Explicit, Size = 298, Pack = 1)]
public struct QuestCompletationData {
[MarshalAs(UnmanagedType.LPStruct)]
[FieldOffset(0)]
QuestCompletationDataHeader Header;
[MarshalAs(UnmanagedType.LPStruct)]
[FieldOffset(10)]
QuestData NormalQuests;
[MarshalAs(UnmanagedType.LPStruct)]
[FieldOffset(106)]
QuestData NightmareQuests;
[MarshalAs(UnmanagedType.LPStruct)]
[FieldOffset(202)]
QuestData HellQuests;
}
The D2SFile class:
[StructLayout(LayoutKind.Explicit, Size = 638, Pack = 1)]
public struct D2SFile {
[MarshalAs(UnmanagedType.LPStruct)]
[FieldOffset(0)]
public D2SHeader Header;
[MarshalAs(UnmanagedType.LPStruct)]
[FieldOffset(335)]
public QuestCompletationData Quests;
}
And the function Im using to do the byte to struct conversion:
public static D2SFile ByteArrayToD2SFile(byte[] bytes) {
GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
D2SFile stuff = (D2SFile)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(D2SFile));
handle.Free();
return stuff;
}
As I said, I can already read the file header alone without a problem, but when I add the quest data to the D2SFile struct I get: Could not load type 'MedianXLEditor.QuestCompletationData' from assembly '...' because it contains an object field at offset 10 that is incorrectly aligned or overlapped by a non-object field.
Since no one answered and I already found out what was wrong I think I should answer it myself so that anyone that happens to end up here after googling have a easier time.
It turns out you cant have a array starting at offsets that are not multiples of 4.
On the example above, the QuestCompletationDataHeader is 10 bytes long, so on the QuestCompletationData struct the next field will start at position 10. The next field happens to be of the QuestData struct which is basically a big array (for now). So it will try to place that array at the offset 10, 10 is not a multiple of 4 so it gives the exception.
I changed the QuestData struct so it does not use a array on the first position and it works fine now.
Also, use [MarshalAs(UnmanagedType.Struct)] when Marshalling structs like the ones above. I was using [MarshalAs(UnmanagedType.LPStruct)] which gave me another exception later.
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.
Hey!
I've just begun fiddling with pinvoke and have encountered a problem. I'm getting the AccessViolationException. First of all, is there some way to debug or trace out which field is causing this error? The only thing being written to is the result struct.
The c++ call looks like:
MyFunc(int var1, _tuchar *var2, _tuchar *var3, _tuchar *var4, MyStruct *Result,
_tuchar *var5, _tuchar *var6);
The c++ struct:
typedef struct MyStruct
{
_tuchar *id;
_tuchar *ErrorMessages;
int int1;
_tuchar language[3];
_tuchar *result;
int type;
int number;
int *type2;
_tuchar **blocks;
}
The C# struct:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct MyStruct
{
[MarshalAs(UnmanagedType.LPStr)]
public string Id;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=500)]
public char[] ErrorMessages;
public int int1;
[MarshalAs(UnmanagedType.LPStr)]
public string language;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 15)]
public char[] result;
public int type;
public int number;
public int type2;
[MarshalAs(UnmanagedType.ByValArray)]
public string[] blocks;
The C# method declaration:
[DllImport(MyPath, EntryPoint = "MyEntryPoint", SetLastError = true,
CharSet = CharSet.Unicode)]
internal static extern int MyFunc(int var1, string var2, string var3,
string var4, ref MyStruct Result, string var5, string var6);
The C# Call:
var result = new MyStruct();
MyFunc(0, "var2", "var3", "var4", ref result, "var5", "var6");
Hope I haven't left anything out.
Thanks for any help!
Ooooh, man! You've picked quite a complex case for your first fiddling experience. I recommend doing something simpler first, and then moving on to the real stuff.
Firstly, CharSet=CharSet.Ansi looks suspicious. All your strings and chars are _tuchar, and I gather the u in there means "Unicode", doesn't it? If that's the case, you need CharSet=CharSet.Unicode.
Secondly, (and this is the most likely culprit) why is the ErrorMessages field marshaled as ByValArray? You know that ByVal here means "by value", don't you? As in, not by reference. And you know that little asterisk thing in C++ means "reference", don't you? So why does your reference field ErrorMessages marshaled as a by-value array? In case you don't know, an array is generally said to be passed "by value" when all of it's content is being passed, instead of just passing a reference (pointer) to a memory location where all that content is stored. In C++ struct definition, you specify _tuchar*, which means "a reference (pointer) to some memory containing one or more of _tuchars", whereas in C# you specify [MarshalAs(UnmanagedType.ByValArray, SizeConst=500)], which means "500 _tuchars are supposed to be here, no more and no less". Seeing how a reference (pointer) usually takes 4 bytes (or 8 bytes on 64bit machines), and 500 unicode characters take 1000 bytes, you have an obvious mismatch right here.
Thirdly and fourthly, same point goes for result and blocks fields.
Fifthly, the language field is exactly reverse situation: the C++ code says "there are 3 _tuchars here", while C# code says "there is a reference (pointer) to a string here" (in case you don't know, LPStr means "Long Pointer to STRing")
And finally, after you have fixed all those problems, I recommend you execute your program and print out the result of call to Marshal.SizeOf( typeof( MyStruct ) ). That will give you exactly how big your struct is, in .NET's opinion. The go on the C++ side and print out sizeof( MyStruct ). That will give you what C++ thinks about the size.
If they turn out different, see what's wrong. Try to remove fields one by one, until they become same. This will give you the culprit field(s). Work with them.
Overall, I suggest you need a better understanding of how things work first. This case is waaaay too complex for a beginner.
Good luck!
This is a bit of a shot in the dark, but have you tried decorating the string parameters with MarshalAs(UnmanagedType.LPWStr):
[DllImport(MyPath, EntryPoint = "MyEntryPoint", SetLastError = true,
CharSet = CharSet.Unicode)]
internal static extern int MyFunc(
int var1,
[MarshalAs(UnmanagedType.LPWStr)]
string var2,
[MarshalAs(UnmanagedType.LPWStr)]
string var3,
[MarshalAs(UnmanagedType.LPWStr)]
string var4,
ref MyStruct Result,
[MarshalAs(UnmanagedType.LPWStr)]
string var5,
[MarshalAs(UnmanagedType.LPWStr)]
string var6);
I believe that the default marshaling chosen for strings is BStr and _tuchar should expand to wchar_t so I'd guess that LPWStr is the correct marshalling method (pointer to a wide character string).
Update: Various things on MyStruct don't look quite right:
ErrorMessages is marked as ByValArray, and so the .Net interop is probably expecting MyStruct to look a bit like this:
typedef struct MyStruct
{
_tuchar *id;
_tuchar ErrorMessages[500];
// Rest of MyStruct
Which is probably going to cause problems - same thing for result.
Also I think that language should be using ByValArray with a size of 3.
Finally blocks should probably be passed using LPArray - ByValArray doesn't seem right.
(This is all mostly guesswork btw - I hope this is pointing you in the right direction but I don't have that much experience with P/Invoke interop)
Another update: On MyStruct you declare the charset to be Ansi, but on MyFunc its Unicode... is the unmanaged dll compiled with Unicode or Ansi? If it uses Unicode then I believe that you should use LPWStr when marshalling strings, and with Ansi it should be LPStr.
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.
It seems I have yet another problem in the understanding of marshalling to C++ DLL.
Here is the def of the C++ function & struct :
#define SIZE_PLATE (28l)
#define SIZE_HJT (15l)
#define SIZE_DATE (10)
typedef struct _tyrfdePlate
{
TCharA PlateID[SIZE_PLATE];
TInt32 NetworkID;
TInt32 CityID;
TCharA DateS[SIZE_DATE];
TCharA DateE[SIZE_DATE];
TInt32 Width;
TInt32 Height;
TBool Light;
TBool Roll;
TCharA CycleID[4];
TInt16 OrHjt1;
TCharA HJTID1[SIZE_HJT];
TInt16 OrHjt2;
TCharA HJTID2[SIZE_HJT];
TInt16 OrHjt3;
TCharA HJTID3[SIZE_HJT];
TInt16 OrHjt4;
TCharA HJTID4[SIZE_HJT];
} tyrfdePlate;
TInt32 __stdcall tyrfdeSetResults(TInt32 TargetNbr, const TInt32* pTargets, TInt32 PlateNbr, const tyrfdePlate* pPlates);
This is what I made in C# based on a previous question I asked:
[StructLayout(LayoutKind.Sequential, Size = 138), Serializable]
public struct Plate
{
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 28)]
public string PlateID;
public int NetworkID;
public int CityID;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 10)]
public string DateS;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 10)]
public string DateE;
public int Width;
public int Height;
public bool Light;
public bool Roll;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 4)]
public string CycleID;
public short OrHJT1;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 15)]
public string HJTID1;
public short OrHJT2;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 15)]
public string HJTID2;
public short OrHJT3;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 15)]
public string HJTID3;
public short OrHJT4;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 15)]
public string HJTID4;
}
[DllImport("tyrfde.dll", EntryPoint = "tyrfdeSetResults")]
public static extern int SetResults(int targetNbr, [MarshalAs(UnmanagedType.LPArray)] int[] targetIds, int plateNbr, [MarshalAs(UnmanagedType.LPArray)] Plate[] plates);
Here is an example of the call:
List<Plate> plates = new List<Plate>();
plates.Add(new Plate() { PlateID = "56013208", NetworkID = 992038, CityID = 60010, DateS = "01012009", DateE = "31122010", Width = 400, Height = 300, Light = false, Roll = false, CycleID = "0", OrHJT1 = 2, HJTID1 = "579026356", OrHJT2 = 2, HJTID2 = "579026377", OrHJT3 = 2, HJTID3 = "58571903", OrHJT4 = 0, HJTID4 = "0" });
int[] targets = new int[]{1,2,11,12,130};
int result = SetResults(5, targets, 1, plates.ToArray());
Note that I also tried with native Array instead of generic list with same result.
So basically I'm redoing a test app made in C++ with the same Data. Unfortunately, the function return me -1 which means an error occurred, but the C++ app returns 23. So I guessed something is wrong with either my struct and/or the parameter I pass. Probably the int[]. I've tried to let the default marshal with ref, but didn't work. Any ideas?
EDIT
Since the type definition seem to be very important here is the def :
typedef void TVoid;
typedef bool TBool;
typedef char TCharA; // character 8
typedef TCharA TChar; // character 8
typedef wchar_t TCharW; // character 16
typedef signed char TInt08; // integer signed 8
typedef unsigned char TUnt08; // integer unsigned 8
typedef signed short TInt16; // integer signed 16
typedef unsigned short TUnt16; // integer unsigned 16
typedef signed long TInt32; // integer signed 32
typedef unsigned long TUnt32; // integer unsigned 32
typedef float TFlt32; // float 32
typedef double TFlt64; // float 64
The Size attribute you gave in the [StructLayout] attribute is a good hint. Verify that with this snippet:
int len = Marshal.SizeOf(typeof(Plate));
System.Diagnostics.Debug.Assert(len == 138);
The only way you are going to get passed this assertion is when you replace "bool" with "byte" (So TBool = 1 byte) and use a packing of 1:
[StructLayout(LayoutKind.Sequential, Pack=1), Serializable]
public struct Plate {
//...
}
If that still doesn't work then you'll really have to debug the unmanaged code.
What you really want is a way to debug this. The easiest way is to write your own dll that consumes this data type and see what happens to the struct on the other side.
I suspect that your real problem is structure alignment and how that's working out. What I see in your code is a bunch of elements with odd sizes (15, 28, 10). Chances are the target system has gone and aligned the structure elements on at least 2 byte, if not 4 byte boundaries. Nonethless you should check.
You can also save yourself some time by writing the C that consumes the actual struct and outputs the results of a bunch of offsetof() invocations on the struct elements.
You should be methodical in your approach instead of shotgun, and part of the methodology is to have measurements and feedback. This will give you both.
1) How many bytes is a TBool? 1? 4?
2) You can remove the StructLayout(LayoutKind.Sequential, Size = 138) attribute because it's Sequential by default and the Size can be determined by the runtime.
3) You can drop the [MarshalAs(UnmanagedType.LPArray)] attribute. The marshaller knows how to marshal arrays, but note, by default arrays are Marshalled as [IN], so if the c++ code is going to edit the contents of the arrays, you need to use the [IN,OUT] attribute.
How about the const TInt32* pTargets argument in the dll. I don't know how it's used, but that suggests a pointer to a single TInt32 instance, not an array of TInt32. Granted, it depends on how it's used in the code.