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?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

abesoftConnect With a Mentor Commented:
From the command-line it's -a2.
In BC4.51, you can use the menu to choose Options|Project|32-bit Compiler|Processor, and choose "Word Alignment".

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.
borgzAuthor Commented:
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?
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

borgzAuthor Commented:
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.
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.
borgzAuthor Commented:
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.
All Courses

From novice to tech pro — start learning today.