?
Solved

Using C++ Code in Delphi

Posted on 2005-03-08
11
Medium Priority
?
185 Views
Last Modified: 2010-04-05
Hi,

I have a set of maths conversion functions written in C++, in a C++ header and source file.

Rather than re-write these lengthy equations in Delphi I was wondering if there is a straight forward method for using C++ functions from within a Delphi Project?

Thanks in Advance,

AT.
0
Comment
Question by:Attack_Trax
  • 5
  • 5
11 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 160 total points
ID: 13485291
I don't use C++, but I know you could use a DLL written by C++ inside Delphi, same as you do with windows API

look at this articles for more info
http://www.drbob42.com/delphi/headconv.htm
http://www.faqts.com/knowledge_base/view.phtml/aid/23661
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 13485551
you can also import .obj files
if not mistaken they have to be compiled with a borland c++ compiler (cpp 5 is free)
you then simply declare the methods declare them as external
in the file they're declared you add an {$L filename.obj} directive to link it in
examples are the zlib and jpeg units
0
 

Author Comment

by:Attack_Trax
ID: 13504953
Thanks for your posts.

I have successfully managed to use the C functions in a DLL, but only when I bind the DLL functions using an index number rather than by name, for example:

-------------------------------------
function CalculateE(SemiMajorAxis, SemiMinorAxis: double): double;
  stdcall; external 'gpsfunc.dll'
-------------------------------------

Doesn't work, but

-------------------------------------
function CalculateE(SemiMajorAxis, SemiMinorAxis: double): double;
  stdcall; external 'gpsfunc.dll' index 1;
-------------------------------------

does.

If I try and bind by name the result is an access violation. Any suggestions as to what I am doing wrong?

Thanks again,

AT.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Expert Comment

by:Lee_Nover
ID: 13504976
export names are case sensitive
you can still use a different name by specifing the dll function's name
function CalculateE(SemiMajorAxis, SemiMinorAxis: double): double;
  stdcall; external 'gpsfunc.dll' name 'CalculateExtended';
0
 

Author Comment

by:Attack_Trax
ID: 13505186
Hi Lee Nover,

Thanks for your suggestions - as you suggested I have tried using aliases with no success, and I have cut and pasted the export names from the C++ header file so the case is the same.

Could this be anything to do with the format of the string being passed as the name?

AT.
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 13505255
is it a c++ dll ? did you declare them as extern "C" in the dll ? if not then your export names will get some code prepended like 16@FunctionName

#ifdef __cplusplus
extern "C" {
#endif

functions ...

#ifdef __cplusplus
}
#endif
0
 

Author Comment

by:Attack_Trax
ID: 13505355
Thanks again Lee Nover,

My C++ dll does include the extern "C":

extern "C" __declspec(dllexport) double __stdcall CalculateE(double SemiMajorAxis, double SemiMinorAxis);

Is there anything else I might have ommitted from the c++ code?!

Is there any way to figure what the 'Name Mangled' function name would be using Visual C++ 6.0?

AT.
0
 
LVL 12

Assisted Solution

by:Lee_Nover
Lee_Nover earned 240 total points
ID: 13505404
looks ok .. you can check dependencies and export functions with dependency walker :)
http://www.dependencywalker.com/
0
 

Author Comment

by:Attack_Trax
ID: 13506488
Using Dependency Walker, the function name is _CalculateE@16. If I use this string it works correctly.

Am I missing a setting in my Visual C++ 6.0 Project settings, or is there anything else which could be preventing the extern "C" from taking effect?

Thanks,

AT.
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 13506531
as I thought .. well I don't know bout VC nor what could be the problem
I had it once and I corrected it .. can't remember what I did
0
 

Author Comment

by:Attack_Trax
ID: 13507152
Oh well, I'll try posting it on the C++ forum. Thanks for all your help, I've split the points as mnasman's post was a helpful starting point.

Many Thanks,

AT.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

571 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