473,499 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshalling a struct (which contains a union and the union contains a struct ... also all the members are byte arrays) for Platform Invoke

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

Jul 22 '06 #1
1 1536
"jsshah" <ja******@gmail.comwrote:
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

--
http://barrkel.blogspot.com/
Jul 22 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
2408
by: hermes_917 | last post by:
I want to use memcpy to copy the contents of one struct to another which is a superset of the original struct (the second struct has extra members at the end). I wrote a small program to test...
8
1667
by: Mike | last post by:
The following struct, DataStruct, is only part of a larger one that contains additional fields and arrays. I need the explicit layout because this struct is really a union, where some of the...
8
3392
by: Ben Terry | last post by:
What's the most efficient way to transfer data from a byte to a struct? The struct is rather complex--contains other structs as well as byte members. I've tried to use Marshal.Copy and an IntPtr...
3
2831
by: PickwickBob3 | last post by:
I am try to marshall the following: /*typedef struct tagRAWMOUSE { USHORT usFlags; union { ULONG ulButtons; struct { USHORT usButtonFlags;
5
4015
by: Hallvard B Furuseth | last post by:
Does struct assignment copy padding bytes? Some compilers do, but I couldn't find anything in the standard which says they must. What I need is for any padding bytes to contan initialized values...
24
29313
by: Grey Alien | last post by:
If I have the ff struct: struct A { unsigned int i; char s; } a, b; And use them in code like this:
18
6056
by: Bryan Parkoff | last post by:
I hate using struct / union with dot between two words. How can I use one word instead of two words because I want the source code look reading clear. three variables are shared inside one...
2
3318
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
10
1738
by: Chris Saunders | last post by:
Here is the declaration of a struct from WinIoCtl.h: // // Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION // typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT { union { //
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7012
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7225
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.