
Declaring C-Language Union and Bit-field data types in Visual Basic
An application is written in
Visual Basic and in that code we need to call
functions from ".dll" which is written in C-Language. For processing the data
received from those APIs we need to declare various data types. Those data types
included some "c-language" enumeration, "c-language" structures, "c-language"
unions and "c-language" bit fields. So I created a type library by compiling
an ".odl" file, which had declaration of all these types by including
appropriate ".h" header file, with MIDL compiler.Type library was successfully
created. Then after adding this type library as a reference in Visual Basic
project while compiling wherever tried to access these data types, I got a
compilation errors for structures, union and bitfields, stating that these are
not "Automation" data type. Then I tried to explicitly declare these data types
in the Visual Basic code the I was stuck with how to declare unions and bit
fields as Visual Basic doesn't support these data types. Then as a solution I
thought that I should simulate them by declaring data type as an array of byte
with no of bytes equal to the size in byte of the "c-language" data type and then
appropriate care has to be take to taken to access these data types. e.g.
In C- Language Bit Field
#pragma pack (4)
typedef struct {
ULONG a : 6; // Bits 0:5
ULONG b : 8; // Bits 6:13
ULONG c : 1; // Bit 14
ULONG d : 15; // Bits 15:29
ULONG e : 2; // Bit 30:31
Quote:
} MyBitField;
In Visual Basic can be defined as
Public Type MyBitField
Data(3) As byte //4 bytes or 32 bits
End Type
In C- Language Bit Field
typedef union {
MyBitField bitfield;
ULONG params;
} My_UNION
In Visual Basic can be defined as
Public Type My_UNION
Data(3) As byte //4 bytes or 32 bits as both the fields of union are of 32 Bits
End Type
I would appreciate if any one can point me in the right direction
Thanx
Subhash Chopra
Hughes Software Systems
India.