Marshalling a struct (which contains a union and the union contains a struct ... also all the member


J

jsshah

Hi

I want to marshal following Win32 struct into .NEt class or struct as I
want to call a native dll function from a Csharp code.

The win32 struct
-----------------------

typedef struct _FILE_OBJECT_ID_BUFFER
{

BYTE ObjectId[16];

union
{
struct
{
BYTE BirthVolumeID[16];
BYTE BirthObjectID[16];
BYTE DomainID[16];
}; //end struct

BYTE ExtendedInfo[48];
} //end union
}

Currently my first version of the code just needs following four
fields:
objectID, BirthVolumeID, BirthObjectID, DomainID

Thats why I use the following class definition
C#
----
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto),
Serializable]
[BestFitMapping(false)]
internal class OBJECT_ID_BUFFER
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
internal Byte[] ObjectID;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
internal Byte[] BirthVolumeID;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
internal Byte[] BirthObjectID;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
internal Byte[] DomainID;
}

and it works perfectly.

However in future I need to perform complete marshalling by which I can
also address the union involved. I searched MSDn but couldnt find any
convincing answers.

Any help will be greatly appreciated.

thanks
 
Ad

Advertisements

B

Barry Kelly

jsshah said:
I want to marshal following Win32 struct into .NEt class or struct as I
want to call a native dll function from a Csharp code.
However in future I need to perform complete marshalling by which I can
also address the union involved. I searched MSDn but couldnt find any
convincing answers.

For unions, there is LayoutKind.Explicit and the FieldOffset attribute.

-- Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top