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?
+3
A:
No.
Your best best is to write the low level stuff in C or C++ (where you do have #pragma pack), then expose the interface via COM.
(Out of curiosity, why are you still working with VB6 instead of VB.NET?)
Uhall
2008-09-27 05:20:22
It's a legacy app written in VB6 and a long time ago it was decided it was better to keep as much in VB6 as possible rather than mix-and-match .NET and COM. Probably a bit idealistic on our part, but then again COM Interop isn't always the most fun to work with ;-)
Mike Spross
2008-09-27 05:35:53
Use in conjuction with the advice given below
2008-09-27 06:56:00
+1
A:
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"
David L Morris
2008-09-27 05:37:41