Declaring C-Language Union and Bit-field data types in Visual Basic 
Author Message
 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.



Tue, 05 Aug 2003 14:47:57 GMT  
 Declaring C-Language Union and Bit-field data types in Visual Basic
You indeed have the solution in hand,. the only solution. You would then
have to use bitmasking to make it look correct to the API.

I did this in my book with Uniscribe (which heavily uses bit fields). Unions
are a lot easier since they usually just represent multiple ways of looking
at the same data, so you are best off declaring it as the actual type that
you usually want to use, provided that you can get to the other if you need
it.

--
MichKa

a new book on internationalization in VB at
http://www.i18nWithVB.com/


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.



Tue, 05 Aug 2003 18:45:54 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Declaring C-Language Union and Bit-field data types in Visual Basic

2. Passing of C language's union data type in VB application

3. Does VB a similar data type like the C++ Union type

4. Does VB a similar data type like the C++ Union type

5. problem of converting C's union type to VB's a data type

6. problem of converting C's union type to VB's a data type

7. Cannot assign a variable declared as a string to a variable declared as data type

8. Declaring an array of pointers in Visual Basic (and putting and getting data from it)

9. shared variables between Visual Basic (16 bit) and Visual C++ (32-bit)

10. Declaring an array of pointers in Visual Basic (and putting and getting data from it)

11. Unions and bit fields in VB

12. Union Data type in VB??

 

 
Powered by phpBB® Forum Software