Jump to content

msdn uncertainty.

Recommended Posts

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.

struct TestExplicit 
    public long lg;
    public int i1;
    public int i2;
    public double d;
    public char c;
    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?

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...

  USHORT    Reserved1;
  USHORT    ExtType;
  struct {
    USHORT Linkage  :1;
    USHORT Reserved2  :15;
  USHORT    DataSize;

...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.

Share this post

Link to post
Share on other sites

About the example, it's:


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;

test.i2 = 100;


This will print 


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

Share this post

Link to post
Share on other sites

If you're looking at getting Event Tracing for Windows support in C#, look at System.Diagnostics.Tracing.EventSource. I read that in this article http://blogs.msdn.com/b/vancem/archive/2012/08/13/windows-high-speed-logging-etw-in-c-net-using-system-diagnostics-tracing-eventsource.aspx

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.

Share this post

Link to post
Share on other sites

I might not have been very clear.

In Vance's classes, when event data is passed to the handler delegate it does not appear to include event data header.

In c++ I use that to filter out what I need.

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

  • Create New...