?
Solved

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

Posted on 1998-10-14
2
Medium Priority
?
594 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
[X]
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
2 Comments
 
LVL 2

Accepted Solution

by:
shchuka earned 800 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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