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

Importing a binary file which contains packed datatypes


I'm trying to import a file in VB, which is exported in a C++ routine. However, they used a pragma pack(1) in C++ to export the structure to the file. When I'm trying to import the file in VB, I get a shifting in the different members of the datatype.

The size of the struct saved in C++ is also smaller than the size of the datatype created in VB that I'm trying to import.
I'm using this datatype in C++:

struct {
     char                    name[33];
     char                    nRecChannels;
     char                    invertedACChannels;
     short               maximumVoltage;
     short               normalVoltage;
     short               calibrationSignal;
     short               calibrationScale;
     short               videoControl;
     unsigned short     nSensitivities;
     unsigned short     nLowFilters;
     unsigned short     nHighFilters;
     float               sensitivity[32];
     float               lowFilter[32];
     float               highFilter[32];

     char                    montageName[33];

And this equivalent in VB:
Type RecorderInfo
    name                As String * 33
    nRecChannels        As Byte
    invertedACChannels  As Byte
    maximumVoltage      As Integer
    normalVoltage       As Integer
    calibrationSignal   As Integer
    calibrationScale    As Integer
    videoControl        As Integer
    nSensitivities      As Integer
    nLowFilters         As Integer
    nHighFilters        As Integer
    sensitivity(32)     As Single
    lowFilter(32)       As Single
    highFilter(32)      As Single
    montageName         As String * 33
End Type

Everything until the 3 float-array's is imported as expected, then it goes wrong. The sensitivity float array imports nicely, but only 14 of the 32 items are filled in the file. Then there is a sort of overlap in the lowfilter array.

I guess this got to do with the pragma pack (1) used in C++, which is apperently not supported in VB.

Is there some sort of command for it, or a workaround?

Best regards.
1 Solution
No, VB does not like packing structures in memory - but you should still be able to read the structure from disk.  Try inputting the fields seperately, like this:

Dim MyStruct as RecorderInfo
Input #1, MyStruct.name
Input #1, MyStruct.nRecChannels
Input #1, MySturct.invertedACChannels

Keep going like this, and the structure should be filled out correctly.
You shouldn't have any problem at all.  I suggest hexediting the resulting file to be sure it's members are all aligned and positioned correctly.  The pack(1) should've ensured that.  I load/save structures at once all the time.

(5 minutes go by while testing)

aha!  you're so gonna kick yourself.  Your structure arrays have 1 too many elements.  In C the number by a array declaration means the number of elements.  In VB the number by a array declaration is the upper bound.

   sensitivity(31)     As Single
   lowFilter(31)       As Single
   highFilter(31)      As Single

That's what they're supposed to look like.  Also, here's the fastest way to load the structure:

Dim Recorder As RecorderType

Open FilePath For Binary As #1
   Put #1, , Recorder
Close #1

Don't ya just love the efficiency in THAT.  ;)
pckosterAuthor Commented:

so simple indeed. But when used to a C++ compiler so confusing :$

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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