?
Solved

Using C++ Code in Delphi

Posted on 2005-03-08
11
Medium Priority
?
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 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