I would like to declare a record in Delphi that contains the same layout as it has in C.

For those interested : This record is part of a union in the Windows OS's LDT_ENTRY record. (I need to use this record in Delphi because I'm working on an Xbox emulator in Delphi - see project Dxbx on sourceforge).

But alas: it's size becomes 10 bytes, instead of the expected 4.
I would like to know how I should declare the record, so that I get a record with the same layout, the same size, and the same members. Preferrably without loads of getter/setters.

Thanks for the compliment. I did make a few mistakes in the code, which I've fixed now, Cheers!
– PatrickvLNov 12 '08 at 22:28

Thanks, this is very helpful. "Flags" shouldn't be an integer type?
– JustMeMay 26 '11 at 12:24

3

Yes, very nifty indeed. Just a couple of observations: 1) shouldn't the record be "packed" ? It depends on the implementation of the C/C++ compiler I believe but it is possible that the equivalent struct is tightly packed. 2) I found the public/private/public sections a little confusing at first glance. Wouldn't it perhaps be clearer to simply keep all record values private (fBaseMid, fFlags, fBaseHi) and then have public properties to expose the "Base" bytes (property BaseMid read fBaseMid write fBaseMid etc) ?
– DelticsOct 12 '11 at 19:54

Rudy's Delphi Corner is the best resource I know of regarding Delphi and C/C++ interoperability. His Pitfalls of conversion is pretty much a must read when using C/C++ APIs in Delphi. The chapter you'll be most interested in is Records and alignment -> Bitfields, but I urge you to read the entire thing top to bottom, twice. The other articles are definitely worth the time investment, too.

Well, you basically need to get down to the dirty with bit-manipulation.

Why, specifically, do you need to retain that structure?

If you only need to talk to a legacy program that either talks in this dialect (TCP/IP or similar), or stores data in this manner (files, etc.), then I would map a normal Delphi structure to a bit-version compatible. In other words, I would use a normally structured Delphi structure in memory, and write code to write and read that structure in a compatible manner.

If you need to save memory, I would make getters and setters that manipulate bits of internal integers or similar. This will have a performance impact, but not much more than what the original C program would have, the only difference is that the bit-manipulation would be added by compiler magic in the C version, whereas you will have to write it yourself.

If you don't have many records in memory, and don't need to talk to another program, I'd use a natural Delphi structure. Trade-off for higher performance will be more memory used.

But it all depends on your criteria.

In any case, you won't be able to talk the Delphi compiler into doing the same job for you as the C compiler.

PACKED RECORD, suggested by another here, doesn't do that, and was never meant to. It will only remove alignment padding to put integers on 32-bit boundaries and similar, but won't pack multiple fields into one byte.

Note that a common way to do this is through Delphi SETS, which are implementing internally using bit-fields. Again, you will have different code than the C variant.