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

Posted on 1998-10-14
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;

                     BYTE G0logicalUnitFld;

                            BYTE G0rsvdBit1          : 2;
                            BYTE G0selfTest          : 1;
                            BYTE G0rsvdBit2          : 2;
                            BYTE G0LogicalUnitNumber : 3;

               BYTE G0rsvd1;
               BYTE G0rsvd2;
               BYTE G0length;

                     BYTE G0flgLinkFld;

                            BYTE                    G0Link     : 1;
                            BYTE                    G0Flag     : 1;
                            BYTE                    G0rsvdBit3 : 6;

Any ideas?
Question by:tward
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

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

    G0operationCD As Byte
    G0logicalUnitFld As ByteForUnion
    G0flgLinkFld As ByteForUnion
End Type

But you can simply do it like this:

    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.

Author Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

717 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