Board index » delphi » Calling a Delphi DLL from a C DLL

Calling a Delphi DLL from a C DLL

Hello.

I want to call a function in a Delphi DLL from a little DLL write in C
(ANSI C, not C++). I don't work with C but a bit with Delphi, I'm learning
it for my work. I found something near to my problem but is for C++. What
is the syntax to include the function in the C DLL? Thanks a lot.

 

Re:Calling a Delphi DLL from a C DLL


On Tue, 20 Nov 2001 17:20:33 +0000 (UTC), Angel Galan

Quote
<aga...@ci.ub.es> wrote:
>Hello.

>I want to call a function in a Delphi DLL from a little DLL write in C
>(ANSI C, not C++). I don't work with C but a bit with Delphi, I'm learning
>it for my work. I found something near to my problem but is for C++. What
>is the syntax to include the function in the C DLL? Thanks a lot.

Just went through the process of writing a demo CBuilder program to
access a DLL I'd written in Delphi5.

My main problem was writing the C header file which defined the calls
to the Delphi DLL. Had it pretty much right but not quite causing an
AV because the interface wasn't exactly correct.

I was browsing a CD from the Australian BorCon and noticed a mention
of a -jph switch on the Delphi compiler to have it generate the header
file.

The Delphi compiler didn't list such a switch but it worked anyway.

DCC32 -jph dllinterface.pas

created a dllinterface.hpp header file.

Quite a useful feature for anyone interfacing C to Delphi code. Pity
it isn't mentioned on the DCC32 help screen.

Michael

Re:Calling a Delphi DLL from a C DLL


Since we are talking C not C++ life is a little easier. You don't have to
consider forcing C linkage with the extern "C" directive

Two steps...
1. Declare the functions in your DLL as external ie.
extern void MyFunc1(int iSomeParameter);

2. Extract a library file from the dll and include it in your C project.
Your C compiler will normally provide a command line utility to do this.
eg  implib myimport.lib mydll.dll

Things to watch....
a) the Delphi dll code declares the exported functions to be of type cdecl.
This will match the default passing convention of the C compiler.
b) Delphi doesn't prepend an underscore to the function names as is required
by C. The import utility will either add the underscore automatically, or
the library extraction utility will provide a switch to enable such
behaviour.

Dave

Quote
Angel Galan <aga...@ci.ub.es> wrote in message

news:Xns915FBA92DCA10agalanciubes@193.145.223.123...
Quote
> Hello.

> I want to call a function in a Delphi DLL from a little DLL write in C
> (ANSI C, not C++). I don't work with C but a bit with Delphi, I'm learning
> it for my work. I found something near to my problem but is for C++. What
> is the syntax to include the function in the C DLL? Thanks a lot.

Re:Calling a Delphi DLL from a C DLL


Quote
Michael Phillips <mphil...@pcug.org.au> wrote in message

news:vcplvtstmosnp88u9bpl4ofct3aosjkrj8@4ax.com...

Quote
> Quite a useful feature for anyone interfacing C to Delphi code. Pity
> it isn't mentioned on the DCC32 help screen.

I've just spent the last few days wrestling with this....... thanks for the
tip. As a small side track, have you had any problems of the following
nature.

I've got a well tested Delphi dll which normally works with a pile of old
CVI spaghetti C. I extracted a tidy part of the C code into C++ Builder with
no problems. I then bundled this into a  new dll with the intention of being
able to access this nicely excised bit of the legacy code from the new Dephi
front-end we are working on. Now it all works just fine, but when called
from Delphi, Memproof shows diabolical things to be happening. In
particular, the original dll called via the new C++ dll is being marked up
as the culprit. Things like modules(unknown) failing to load and so on, then
the odd bit of memory not being deallocated etc. Interestingly, it does this
without any functions being called. Simply attaching and detaching from the
main process is sufficient!!

Dave

Other Threads