Does VB6 have a #pragma pack equivalent?
-
02-07-2019 - |
Question
I am developing a TCP/IP client that has to deal with a proprietary binary protocol. I was considering using user-defined types to represent the protocol headers, and using CopyMemory to shuffle data to and from the UDT and a byte array. However, it appears that VB6 adds padding bytes to align user-defined types. Is there any way to force VB6 to not pad UDT's, similar to the #pragma pack
directive available in many C/C++ compilers? Perhaps a special switch passed to the compiler?
Solution
No.
Your best bet is to write the low level code in C or C++ (where you do have #pragma pack
), then expose the interface via COM.
OTHER TIPS
There is not any way to force VB6 to not pad UDT's, similar to the #pragma pack directive available in many C/C++ compilers, but you can do it the other way around.
According to Q194609 Visual Basic uses 4 bytes alignment and Visual C++ uses 8 bytes by default.
When using VB6 to call out to a C DLL, I used the MS "pshpack4.h" header files to handle the alignment because various compilers do this in different ways, as shown in this (rather edited) example:
// this is in a header file called vbstruct.h ... # define VBSTRING char # define VBFIXEDSTRING char # define VBDATE double # define VBSINGLE float # ifdef _WIN32 # define VBLONG long # define VBINT short # else // and this was for 16bit code not 64bit!!!! # define VBLONG long # define VBINT int # endif ... # include "pshpack4.h" ... typedef struct VbComputerNameStruct { VBLONG sName; VBSTRING ComputerName[VB_COMPUTERNAME_LENGTH]; } VbComputerNameType; typedef struct VbNetwareLoginInfoStruct { VBLONG ObjectId; VBINT ObjectType; VBSTRING ObjectName[48]; } VbNetwareLoginInfoType; ... # include "poppack.h"