Solved

Maintaining 16 bit data alignment in Win32 platform

Posted on 1998-07-09
8
256 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:borgz
8 Comments
 
LVL 11

Expert Comment

by:alexo
ID: 1251574
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.
0
 

Author Comment

by:borgz
ID: 1251575
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?
0
 

Author Comment

by:borgz
ID: 1251576
Could someone help us with this fast?

alexo, do you know the equivalent of #pragma pack(2) in Borland C compiler?
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 32

Expert Comment

by:jhance
ID: 1251577
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.
0
 
LVL 2

Accepted Solution

by:
abesoft earned 70 total points
ID: 1251578
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".


0
 
LVL 2

Expert Comment

by:abesoft
ID: 1251579
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.)
0
 
LVL 2

Expert Comment

by:abesoft
ID: 1251580
Sorry.  Typo.  It should be #pragma option, not options.
0
 

Author Comment

by:borgz
ID: 1251581
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.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question