?
Solved

Importing a binary file which contains packed datatypes

Posted on 2003-03-12
3
Medium Priority
?
137 Views
Last Modified: 2010-05-18
Hi,

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.
0
Comment
Question by:pckoster
[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
3 Comments
 
LVL 5

Expert Comment

by:JMoon5FTM
ID: 8119758
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.
0
 
LVL 1

Accepted Solution

by:
SilentRage earned 240 total points
ID: 8120147
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.  ;)
0
 

Author Comment

by:pckoster
ID: 8121393
Thanx,

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

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

801 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