Advertisement

C++ and VB structures

Started by December 31, 2003 12:59 AM
1 comment, last by ShadowMaster 21 years, 2 months ago
Greets ppl, I got a problem interfacing VB & C++... B4 you post flipcode's tutorial on the subject read on... I have writtin most of my tools for my projects in Visual Basic 6. These tools do things like convert models/textures/pack files etc... These are then used in my Opengl Projects writen in Visual C++.. The problem is this... I might have a structure that looks like:

Type HEADER
     strId as string * 4
     version as Long
     numLumps as integer
End Type


and in C++
   
struct HEADER
{
    char strID[4];
    int version;
    word numLumps;
};
In vb that structure is 4 + 4 + 2 = 10 bytes is size, In C++ you would expect it to be 4,4,2 as well, but sizeof(HEADER) might report it to be say 16 bytes... this causes havoc when I simply want to load the whole structure in c++.. I can load vairible by it's self... but that's a pain, more so when I'm changing things alot... Is this something to do with bit alignment? Thanks! -TheShadowMaster http://evilentities.p5.org.uk [edited by - ShadowMaster on December 31, 2003 2:01:16 AM]
Home: EvilEntities ;)
Well if you're generating the binary data from your tools written in VB, then wanting to read it from file (say with fread()) into a C++ structure, yea the alignment could cause problems. Use the pre-processor directive for Visual C++:

// Push the current "structure packing" value on a stack and set// it to 1 byte for the structure you're defining after#pragma pack(push, 1)struct HEADER{    char strID[4];    int version;    word numLumps;};// Restore the original packing alignment setting#pragma pack(pop)


That basically packs structures on a 1-byte boundary, i.e. no packing. Typically, structures are padded or packed on 4 or 8 byte boundaries to increase the speed of accessing the memory (it's usually a good thing).

It's late as hell, so I might have made a mistake, but let me know if it doesn't work


[edited by - chawk on December 31, 2003 3:48:44 AM]
Advertisement
You rock! That worked, Thanks...

Is there a big speed hit on that, because, as I said, I''m using it for my models.
Home: EvilEntities ;)

This topic is closed to new replies.

Advertisement