Article View: borland.public.cppbuilder.nativeapi
Article #2847Re: Calling Pascal and C dll from the same application
From: Remy Lebeau (TeamB)
Date: Fri, 01 Dec 2006 18:11
Date: Fri, 01 Dec 2006 18:11
68 lines
3255 bytes
3255 bytes
"Renald Yazmir" <renald@partmaker.com> wrote in message
news:4570372f$1@newsgroups.borland.com...
> double a;
You are not initializing the variable. If the DLL function fails to load,
you will be returning a random value back to the caller of your C function.
> typedef double FAR PASCAL (* p)(void);
You should always write your DLLs to export functions using the __stdcall
calling convention. That is the only calling convention that is supported
across multiple programming languages equally. Delphi and C++Builder both
support __stdcall. Despite its name, the PASCAL macro (which resolves to
__pascal) is *not* the same calling convention that Delphi actually uses!
Delphi uses __fastcall instead by default. So if you do not declare any
calling convention in the Delphi code, your DLL function will be exported
using __fastcall, but your C code is then using __pascal instead. The two
are not compatible.
> In dll I declare export function:
> extern "C" double __declspec(dllexport) do_something_in_dll(void);
Use the __stdcall calling convention:
extern "C" double __declspec(dllexport) __stdcall
do_something_in_dll(void);
> In my calling appication I kill "FAR PASCAL "
You should not kill the call convention altogether. Simply replace PASCAL
with WINAPI (or __stdcall directly):
double do_something(void)
{
double a = 0.0;
typedef double __stdcall (* p)(void);
p fun_name;
fun_name = (p)GetProcAddress(hLib, "do_something_in_dll");
if(fun_name != NULL)
a = (fun_name)();
return a;
}
> Works fine!!!
As it should be. When you wrote the DLL in C, the calling conventions used
in both the DLL and the application matched. That was not the case when you
wrote the DLL in Delphi. Calling conventions must always match in order to
work properly.
> My goal now is to be able to use both Pascal and C dlls with the same
> calling application.
You have to write your Pascal DLLs in such a way that they are compatible
with other languages.
> Therefore I want to preserve old style calling convention with FAR PASCAL:
That is the wrong thing to do. You need to use __stdcall instead if you
want your Delphi DLLs to be usable in non-Pascal environments.
Gambit
Technical Details
Message-ID:
<45707143$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4570372f$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,f406291c6c807ecb X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <4570372f$1@newsgroups.borland.com> Subject: Re: Calling Pascal and C dll from the same application Date: Fri, 1 Dec 2006 10:11:14 -0800 Lines: 70 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 69.169.4.232 Message-ID: <45707143$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1164996931 69.169.4.232 (1 Dec 2006 10:15:31 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail