Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Maintaining 16 bit data alignment in Win32 platform

Posted on 1998-07-09
8
Medium Priority
?
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

715 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