Link to home
Start Free TrialLog in
Avatar of lfrodrigues
lfrodrigues

asked on

Using a c++ written dll

I wrote some time a agoo a lot of important functions in c++, now i need to use
that source in delphi and i didn't want to have to translate all the source so i made
dll. Some of the exported function use (structures)records i would like to know if to
call those functions i need to use the packed reserved word on the record declaration on delphi and why i need to use it or not.

Thanks

                 LR

Avatar of Lischke
Lischke

Mmmh, I think this depends on the way you declared the records in C++. I don't know if "pragma pack" applies also to C++ but this is one way to declare a packing alignment. For instance:

#pragma pack 8

if I recall correctly. There's also the possibility to set it in the IDE (BCB, VC). So, essentially, you need to determine which packing directive was active when you declared a particular record. If there was 1 byte packing active then you need the "packed" keyword in Delphi, otherwise not.

BUT (!), if you have used 8 (or more) byte alignment then you need to add some dummy members in the Delphi structure, to make them aligning correctly as Delphi aligns automatically on 4 or 8 byte boundaries (usually 4 byte, for Double etc. 8 byte).

Ciao, Mike
ASKER CERTIFIED SOLUTION
Avatar of mscatena
mscatena

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi mscatena, welcome to Experts-Exchange :-). I don't agree with your conclusion because packed arrays are not a good choice when it comes to speed (depending of the members of the structures of course). So generally using packed to be on the save side is not a good recommendation.

Additionally, please avoid proposing an answer to a question unless you are totally sure you have the ultimate answer for the question. You haven't asked questions yet so you don't know that the questioneer is able to pick a certain comment and declare it as answer. Explicitly proposing an answer often leads to wrongly accepted contributions (as I have encountered too often with my own ones). So please, use only comments.

Ciao, Mike
Avatar of lfrodrigues

ASKER

What do you sujest i do Lischke, if you anwser is good i will reject Mascadena's anwser.
Well, you asked whether to use "packed" on Delphi side and I said that you need this only if you also have packed the record on C side (byte alignment, packing size 1 or however it is called there). In any other case don't use "packed".

After you decided this I'd recommend to try out whether your stuff works (as far as the parameters are concerned). If there was an eight byte (or higher) alignment on the C side then you will quickly encounter strange results. Because in Delphi you can you only specify to align or not to align and Delphi automatically chooses a boundary appriate to the type to be aligned you should tribut to this fact and never use general alignment of 8+ in C.

Ciao, Mike
How slow is acessing record with the packed keyword??
I'm sure mscatena will help you...
About the speed: it doesn't matter in 99.9% of the times.

I don't have hard data on the latest CPUs, but we are talking about something like 2~5 clock cycles. At 500 Mhz. This would be 0.004 to 0.01 microseconds per access. YMMV, because it depends on what else the CPU is doing, memory speed and architecture.