• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 618
  • Last Modified:

Convert C/C++ Structure/Union/BitFields to VB

I have converted C/C++ Header files to VB plenty of times but the following combination looks to be more difficult:

typedef struct{
               BYTE G0operationCD;

               union
                    {
                     BYTE G0logicalUnitFld;

                     struct
                           {
                            BYTE G0rsvdBit1          : 2;
                            BYTE G0selfTest          : 1;
                            BYTE G0rsvdBit2          : 2;
                            BYTE G0LogicalUnitNumber : 3;
                           };
                    };

               BYTE G0rsvd1;
               BYTE G0rsvd2;
               BYTE G0length;

               union
                    {
                     BYTE G0flgLinkFld;

                     struct
                           {
                            BYTE                    G0Link     : 1;
                            BYTE                    G0Flag     : 1;
                            BYTE                    G0rsvdBit3 : 6;
                           };
                    };
} MST_CDB_GROUP0;

Any ideas?
0
tward
Asked:
tward
1 Solution
 
shchukaCommented:
Basically, 'union' keyword specifies that it's either one or another.  In VB this would be EXACTLY translated into something like this:

Type ByteForUnion
    MyValue As Byte
End Type

Type MST_CDB_GROUP0
    G0operationCD As Byte
    G0logicalUnitFld As ByteForUnion
    G0flgLinkFld As ByteForUnion
End Type

But you can simply do it like this:

Type MST_CDB_GROUP0
    G0operationCD As Byte
    G0logicalUnitFld As Byte
    G0flgLinkFld As Byte
End Type

Either of these methods will give you a VB user-defined type with the exact same size (in bytes) as your C structure.  In your code those you'll need to use bit operations to retrieve
the fields.  Assume, you are using the second construct and also note that VB does not have bitwise shift operators.  You will use the following code to access the fields denoted as BYTE ... : x (basically declared as bits).  Say, you have

Dim MyStruct as MSG_CDB_GROUP0

G0rsvdBit1 = (MyStruct.G0logicalUnitFld Div 64) And &H3
G0selfTest = (MyStruct.G0logicalUnitFld Div 32) And &H1
G0rsvdBit2 = (MyStruct.G0logicalUnitFld Div 8) And &H3
G0LogicalUnitNumber = MyStruct.G0logicalUnitFld And &H7

G0Link = (MyStruct.G0flgLinkFld Div 128) And &H1
G0Flag = (MyStruct.G0flgLinkFld Div 64) And &H1
G0rsvdBit3 = MyStruct.G0flgLinkFld Div &H3F

You can also wrap the structure in a class and write methods for accessing the bit fields.
0
 
twardAuthor Commented:
Thanks, don't know why I didn't think of this!!  
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now