c++ dll function from delphi

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
duke63Asked:
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.

TvigepCommented:
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
SimesACommented:

interface
  function GetBetraege: pchar; cdecl;

implementation
  function GetBetraege; external 'PLUS2.DLL';
0
smurffCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

mocartsCommented:
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
BorlandManCommented:
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
TvigepCommented:
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

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
Lee_NoverCommented:
www.dependencywalker.com
(turn off 'undecorate c++ functions' to see the real exported name)
0
smurffCommented:
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
TvigepCommented:
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
duke63Author Commented:
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
mocartsCommented:
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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.