[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Using a c++ written dll

Posted on 2000-01-26
8
Medium Priority
?
139 Views
Last Modified: 2011-09-20
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

0
Comment
Question by:lfrodrigues
  • 4
  • 2
  • 2
8 Comments
 
LVL 10

Expert Comment

by:Lischke
ID: 2388512
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
 
LVL 1

Accepted Solution

by:
mscatena earned 100 total points
ID: 2394047
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
 
LVL 10

Expert Comment

by:Lischke
ID: 2395877
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 1

Author Comment

by:lfrodrigues
ID: 2396049
What do you sujest i do Lischke, if you anwser is good i will reject Mascadena's anwser.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2396345
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
 
LVL 1

Author Comment

by:lfrodrigues
ID: 2481485
How slow is acessing record with the packed keyword??
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2481521
I'm sure mscatena will help you...
0
 
LVL 1

Expert Comment

by:mscatena
ID: 2482107
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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…

613 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