?
Solved

c++ dll function from delphi

Posted on 2003-03-06
11
Medium Priority
?
229 Views
Last Modified: 2010-04-04
Hi everybody,

I have a c++ dll. (Not the source).
I have a c++ program wich uses the dll like this:

#include <Plus1/Plus2.h>
void main()
{
     const char* c;
     c = Plus1::GetBetraege();
     
How can I call the function GetBetraege from a Delphi 5 program?

Axel
0
Comment
Question by:duke63
[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
  • 3
  • 2
  • 2
  • +4
11 Comments
 
LVL 2

Expert Comment

by:Tvigep
ID: 8081018
procedure DoSomething; external 'MYLIB.DLL';
or
function SomeFunction(S: string): string; external 'strlib.dll';

so in your case that would be

procedure GetBetraege; external 'PLUS2.DLL';

if the name of the dll is Plus2.dll. you can collect external declarations into a separate unit, if you need more functions or procedures from the dll.

HTH,
Stef
0
 
LVL 1

Expert Comment

by:SimesA
ID: 8081373

interface
  function GetBetraege: pchar; cdecl;

implementation
  function GetBetraege; external 'PLUS2.DLL';
0
 
LVL 3

Expert Comment

by:smurff
ID: 8082216
Tviqep,

You should not use strings if your passing to C dlls, well to be honest I dont use strings if I passing to any dll, I always use PChars

Regards
Smurff
0
Independent Software Vendors: 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!

 
LVL 9

Expert Comment

by:mocarts
ID: 8084379
this looks like class method invocation...
you must find out exact name of exported function. it could look like @Plus1@GetBetraege$qqsv if compilde with BCC32 or even *better*
(hack the DLL and find exact name :)

 function Plus1_GetBetraege: pchar; cdecl; external 'YOUR.DLL' name '@Plus1@GetBetraege$qqsv';

you must be very careful with results of this function as memory will be allocated by dll and you must, as soon as possible, copy result to your own var (but it's not a guarantee from access violations)
0
 
LVL 2

Expert Comment

by:BorlandMan
ID: 8085594
Also, that 'name mangling' of function names from a c++ dll is not the same for all compilers - that is, there is no standard. Borland does it one way and microsoft does it another.

hth
J
0
 
LVL 2

Accepted Solution

by:
Tvigep earned 400 total points
ID: 8086549
Smurff,

You're right. It should be PChars or AnsiString. Funny thing is, I use D6 and I copied this example from the Help ....

Little oversight Borland?

Regards,
Stef

PS. If you want to know the exact names of all exported functions in the DLL, I always use Dependency Walker. Think I downloaded it once from the Microsoft website, don't remember where but it's pretty good.
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 8086732
www.dependencywalker.com
(turn off 'undecorate c++ functions' to see the real exported name)
0
 
LVL 3

Expert Comment

by:smurff
ID: 8087533
Tvigep,

>Little oversight Borland?
 :) typical Its probably an example using sharemem I cant see ive not got delphi where I am :( <snif>

regards
Smurff


0
 
LVL 2

Expert Comment

by:Tvigep
ID: 8087615
I think it is Smurff. If I simply go to Delphi's help, search for 'external' or 'dll', I end up with a topic called 'Calling dynamically loadable libraries'. At the end of that topic, it says 'For more information about external declarations, see External declarations.' and if you look there, you'll find the example with the Strings. And in neither of those two topics sharemem is mentioned, which it should be!

So I think in reviewing the help (with D6 Personal Edition!!) they overlooked this.

Regards,
Stef

PS. Thanks Lee for the URL. Pretty obvious really (can you say Duh!)
0
 

Author Comment

by:duke63
ID: 8088666
thanks to all,
my problem wa to find the correct function name to declare.
So Dependency Walker was exactly what I needed.
Hope its ok to accept tvigep as the answer?


0
 
LVL 9

Expert Comment

by:mocarts
ID: 8088761
of course I'm a bit angry, because I opened other people eyes to the right way :)
I use simple console file manager (Far.exe) to find internal things of exe's, dll's etc.
btw your q wasn't about name of function to call.. ;)
also this q helped me to learn a little bit more about C/C++ (about name mangling I knew before, but about, how to export whole class, never :)
so I collected points already :)

wbr, mo.
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…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

777 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