Solved

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

Posted on 1998-10-14
2
556 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
Comment Utility
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
Comment Utility
Thanks, don't know why I didn't think of this!!  
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now