Calling Delphi DLL from C++

bugroger
bugroger used Ask the Experts™
on
Hi,

how can i call a DLL,
which is written in delphi,
in C++?

Bug
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
1) LoadLibrary()
2) GerProcAddress()
3) Call the function.
Commented:
thats right, but have an eye on the calling convention.
otherwise errors with the stack can emerge.
delphi uses "register" as standard, which is somewhat comparable to "_fastcall" in c.
you can declare the routines you like to use as "cdelc" in delphi for example, so they will match the standard calling convention in c.
another way is to declare the method in delphi as "stdcall" and than give the pointertype your function retrieved with getprocaddress also the calling model "_stdcall". "stdcall" is the standard for windows api and com or ole, so if you use it as default, you're always safe. also, stdcall is known to the most programming languages, visual basic for example.
example:

-delphi

function y(x integer): double; stdcall;

exports y;

-c

typedef _stdcall double _y(int x);
typedef _y* y;

int h;
y z;
h=LoadLibrary("mydll.dll");
z = (y)GetProcAddress("y");
//call to y like
z(5);

you can replace stdcall by the calling convention of your preference

Author

Commented:
Thx drnick,

just what i have needed!
now it works :-)

Bug

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial