Solved

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

Posted on 1998-10-14
2
566 Views
Last Modified: 2010-08-05
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
Comment
Question by:tward
2 Comments
 
LVL 2

Accepted Solution

by:
shchuka earned 200 total points
ID: 1439881
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
 
LVL 7

Author Comment

by:tward
ID: 1439882
Thanks, don't know why I didn't think of this!!  
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question