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

LVL 1
lfrodriguesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LischkeCommented:
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
0
mscatenaCommented:
Usually C/C++ structures map quite well to Delphi. One proof that it's true is that you can call most, if not all, Windows APIs.

The only exception are bit fields, integers that occupy a few bits and are not supported by Delphi. Not many people use them nowadays: bit fields save a little memory but are very slow.

"packed" only means that an agregated type (record, array) is NOT to be aligned to a word or double word boundary. If you are mixing C/C++ with Delphi, you must check how the C/C++ is aligning structures - you can usually change it - and do the same in Delphi.

The best rule, and the one used in the Windows API, would be NOT to align anything and so use "packed" in Delphi.





0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LischkeCommented:
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
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

lfrodriguesAuthor Commented:
What do you sujest i do Lischke, if you anwser is good i will reject Mascadena's anwser.
0
LischkeCommented:
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
0
lfrodriguesAuthor Commented:
How slow is acessing record with the packed keyword??
0
LischkeCommented:
I'm sure mscatena will help you...
0
mscatenaCommented:
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.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.