JohnOne

msdn uncertainty.

6 posts in this topic

msdn page

I need to translate some C++ structs which contain unions to C# structs. The above link is the page from search I landed on.

Here's what it says about it...

Attributes on Structs
By using attributes you can customize how structs are laid out in memory. For example, you can create what's known as a union in C/C++ by using the StructLayout(LayoutKind.Explicit) and FieldOffset attributes.

[StructLayout(LayoutKind.Explicit)]
struct TestExplicit 
{
    [FieldOffset(0)] 
    public long lg;
    [FieldOffset(0)] 
    public int i1;
    [FieldOffset(4)] 
    public int i2;
    [FieldOffset(8)] 
    public double d;
    [FieldOffset(12)] 
    public char c;
    [FieldOffset(14)] 
    public byte b1;
}

The two int fields, i1 and i2, share the same memory locations as lg. This sort of control over struct layout is useful when using platform invocation.

I'm not too familiar with struct attributes, so I could be wrong here but it seems to me that lg and i1 share the same space but i2 does not.

Or is it that lg is 8 bytes and i1 shares the first 4 and i2 shares the second 4?


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites



This is another question but related so I'm leaving it in here.

Some other structs which I will need to convert have bit fields such as...

typedef struct _EVENT_HEADER_EXTENDED_DATA_ITEM {
  USHORT    Reserved1;
  USHORT    ExtType;
  struct {
    USHORT Linkage  :1;
    USHORT Reserved2  :15;
  };
  USHORT    DataSize;
  ULONGLONG DataPtr;
}

...and it just seems to be getting too complicated.

My related question is, is there some way I can make a C++ dll to simply return these native structures to a C# application?

They will be used in native functions via DllImport.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

About the example, it's:

|--------long-------|
|---int---|---int---|

So the integers i1 and i2 will occupy the same space as long lg. You could try it in a small example.

var test = new TestExplicit();

test.i1 = 5;
Console.WriteLine(test.lg);

test.i2 = 100;
Console.WriteLine(test.lg);

Console.ReadLine();

This will print 

5
429496729605

For the bit fields, start with a short in C#. All you care about is the size of struct in bytes so you can begin with copying the data. Once you have the data then it helps to have it defined correctly so you can easily do things with it but it's not a technical requirement. Once you have it and turns out you need it, either change the struct or bitfuck the data, using bitwise ANDs and ORs.

It's possible to write a C++ dll to change the structs before you copy them but I wouldn't bother because it doesn't exactly make everything a lot easier. Maybe if you have big C++ samples to copy/paste from and you are interested only in very limited functionality.

Edited by Manadar
Forgot dll question
1 person likes this

Share this post


Link to post
Share on other sites

Cheers. I have tried Vance Morrison's samples, but I cannot seem to extract the same data I do from using C++ code.

For example there does not appear to be a method to filter the network protocols TCPIP and I'm getting a bit wound up with it, so decided to look into using native functions in C# and other ways of achieving my goal, which is basically a decent GUI with the functionality and control of native code. creating GUI in C++ without paid external libraries is just to complex and convoluted for my liking you see.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now