delphi's packed record to visual c++


Hello all,

does anybody know how treat these packed records in visual c++?

On the help file it's stated:
1
2
3
4
5
6
7
8
9
10
// Database info structure
TRTDatabaseInfo   = packed record
Version : LongInt;
Size : LongInt;
ImageWidth        : LongInt;
ImageHeight       : LongInt;
ImageChannels     : LongInt;
ActiveDetectors   : LongInt;
MaxChannelCount   : LongInt;
end;



And this is used in a function like this:

1
2
3
4
5
6
7
8
9
var   Info           : TRTDatabaseInfo;
      res,i          : longint;

      Info.Version:=1;
         Info.Size:=SizeOf(Info);
         // Read database info without preview image and spectrum
         ImgBufferSize:=0;
         SpcBufferSize:=0;
         res:=GetRoentecSpectrumDatabaseInfo(Info,nil,ImgBufferSize,nil,SpcBufferSize);


Now how would i use this Info variable in visual c++?

thanks for any help!
C++ gives no control on member alignment, as far as I know. There might be some compiler specific options though..
However, since LongInt is 4 bytes, there is no need for any padding. I think you can be sure that the order of members will be preserved, so a plain struct should be safe to use.

Thanks hamsterman!

is not the default member alignment in visual c++ 8 bytes. In Project -> settings-> C/C++-> Code Generation
one can change this. Could there be some problems in other parts of the code if i change this to 4?


Would this do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
typedef struct TMyStruct{
long Version ;
long Size;
long ImageWidth;
long ImageHeight;
long ImageChannels;
long ActiveDetectors;
long MaxChannelCount;
}Arects;

    float * imbuf  = new float;
	float * spcbuf = new float;

	Arects Info1;
	Info1.Version = 1;  //Info.Version
	Info1.Size = sizeof(Info1);  //Info.Size

 long res = GetRoentecSpectrumDatabaseInfo1(Info1, imbuf,0,spcbuf,0);
barbis wrote:
Could there be some problems in other parts of the code if i change this to 4?
Yes, if you call system functions.

You can surround the struct with #pragma pack. Here's an example from msdn

1
2
3
4
5
6
7
8
#pragma pack()   // n defaults to 8; equivalent to /Zp8
#pragma pack(show)   // C4810
#pragma pack(4)   // n = 4
#pragma pack(show)   // C4810
#pragma pack(push, r1, 16)   // n = 16, pushed to stack
#pragma pack(show)   // C4810
#pragma pack(pop, r1, 2)   // n = 2 , stack popped
#pragma pack(show)   // C4810 
Topic archived. No new replies allowed.