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:29:22 GMT  
 Declaring C-Language Union and Bit-field data types in Visual Basic
Right, unions and bitfields are not directly supported by VB, but
you can use different UDT's to access fields in different manners.
You can use LSet or MoveMemory API to copy the UDT from
one type to another.

By using logical AND you can get your bits out of a integer value.
I would use a single Longword value instead of  an array with
4 byte values.

Be aware that VB does not use unsigned longword integers, they are signed,
which
means that bit 32 indicates whether the number is negative or positive, so
to
get that bit you'll have to test that separately.
Of course, there are many ways to extract bits and maybe others will make
different
suggestions.
Maybe the best would be a bitmask, one for each field:
Const MaskA = 63
Const ShiftA = 0
Const MaskB = 16320 ' 255 * (2^6)
Const ShiftB = 6
...

a = (bitfield And MaskA) \ (2 ^ ShiftA)
b = (bitfield And MaskB) \ (2 ^ ShiftB)
...

Hope this helps

Alex


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 file://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 file://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 23:19:09 GMT  
 Declaring C-Language Union and Bit-field data types in Visual Basic
Thanks for multiposting ...


Quote:
> Right, unions and bitfields are not directly supported by VB, but
> you can use different UDT's to access fields in different manners.
> You can use LSet or MoveMemory API to copy the UDT from
> one type to another.

> By using logical AND you can get your bits out of a integer value.
> I would use a single Longword value instead of  an array with
> 4 byte values.

> Be aware that VB does not use unsigned longword integers, they are signed,
> which
> means that bit 32 indicates whether the number is negative or positive, so
> to
> get that bit you'll have to test that separately.
> Of course, there are many ways to extract bits and maybe others will make
> different
> suggestions.
> Maybe the best would be a bitmask, one for each field:
> Const MaskA = 63
> Const ShiftA = 0
> Const MaskB = 16320 ' 255 * (2^6)
> Const ShiftB = 6
> ...

> a = (bitfield And MaskA) \ (2 ^ ShiftA)
> b = (bitfield And MaskB) \ (2 ^ ShiftB)
> ...

> Hope this helps

> Alex



> 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
> } MyBitField;

> In Visual Basic can be defined as

> Public Type MyBitField
> Data(3) As byte file://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 file://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 23:59:25 GMT  
 
 [ 3 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