Link to home
Start Free TrialLog in
Avatar of borgz
borgz

asked on

Maintaining 16 bit data alignment in Win32 platform

Is there a way to maintain the 16 bit data alignment in win32 platform.  We have a Borland C program compiled in 16 bit platform that we recompiled into 32bit dll.  The dll accesses a binary file with 16 bit data alignment.  How could we do this correctly since we usually make use of the sizeof function?
Avatar of alexo
alexo
Flag of Antarctica image

There are several solutions.

1) Add a compiler switch to pack data on 2 byte boundaries.
2) Add a #pragma before the code to the same effect.
3) Add a couple of #pragma's around critical data structures to the same effect.

Unfortunately, I don't have the Borland compiler in front of me but MS compiler uses the "/Zp2" compiler switch or "#pragma pack(2)".  Check the compiler documentation.
Avatar of borgz
borgz

ASKER

For example we have this data structure:
typedef struct {
 char Tword[50];
 TCategory Category;
} TMeaning;

typedef struct {
 char Word[20];
 TMeaning Meaning[20];
} TElement;

These are the very data structure that I am using for the so called binary file that was created using 16 bit (actually in Turbo C ver 2.0) compiler. Granting that we are going to use functions compiled in win32 platform that will access this binary file having the previous data alignment, how do we add the #pragma directive?
Avatar of borgz

ASKER

Could someone help us with this fast?

alexo, do you know the equivalent of #pragma pack(2) in Borland C compiler?
I don't think there is a pragma for this in the Borland compiler but there is a command line switch.  Sorry, I don't hremember what it is and I don't have a copy of BC++ anymore.  You should be able to get a listing of the command line options by typing the TC/BC (or whatever it is) command with no options.
ASKER CERTIFIED SOLUTION
Avatar of abesoft
abesoft

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would recommend that you DON't use the general approach that I suggested, but instead wrap your structs with

#pragma options -a2
struct blah{}
#pragma options -a.

Just so you can use system defined structures safely.  The -a. resets the original value (which will almost always be -a4.)
Sorry.  Typo.  It should be #pragma option, not options.
Avatar of borgz

ASKER

abesoft: thanks but we kinda did things differently - we
took the sizeofs of each field of the struct and converted the
binary file into the win32 platform.
jhance: thanks for the comment about the compiler switches. Too bad I wasn't able to actually use these comments.