Is it possable to do a c type union in vb.net?

G

Guest

to all

I need to intigrate a vb.net application to a API (written for c) that deals
with processing messages. Each message type is represented by a structure.
All message structures are containded in a union "Message".

In c the code would be as follows:
typedef struct
{
EventHeaderType eventHeader;
union
{
Event1Type event1;
Event2Type event2;
Event3Type event3;
Event3Type event4;
} event;
char heap[MAX_HEAP];
}EventType;

How can this be done in vb.net?
 
T

Tom Shelton

to all

I need to intigrate a vb.net application to a API (written for c) that deals
with processing messages. Each message type is represented by a structure.
All message structures are containded in a union "Message".

In c the code would be as follows:
typedef struct
{
EventHeaderType eventHeader;
union
{
Event1Type event1;
Event2Type event2;
Event3Type event3;
Event3Type event4;
} event;
char heap[MAX_HEAP];
}EventType;

How can this be done in vb.net?

You need to adjust the marshalling attributes of the structure. I can't
give you an exact declaration, because you don't includ ethe
EventHeaderType and evnet type declarations... But, it would look
something like:

<StructLayout (LayoutKind.Explicit)> _
Public Structure UnionType
<FieldOffset (0)> _
Public event1 As Event1Type

<FieldOffset (0)> _
Public event2 As Event2Type

<FieldOffset (0)> _
Public event3 As Event3Type

<FieldOffset (0)> _
Public event4 As Event4Type
End Union

<StructLayout (LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure EventType
Public eventHeader As EventHeaderType
Public union As UnionType

<MarshalAs (UnmanagedType.ByValTStr, SizeConst:=MAX_HEAP)> _
Public heap As String
End Structure
 
H

Herfried K. Wagner [MVP]

JohnH said:
I need to intigrate a vb.net application to a API (written for c) that
deals
with processing messages. Each message type is represented by a structure.
All message structures are containded in a union "Message".

You can simulate unions using the 'FieldOffset' attribute. This should only
be used in interop scenarios. The link below contains a sample:

Changing the screen resolution
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=setscreenresolution&lang=en>
 

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