When I try to use the program whith a C++Builder made dll, I have an
access violation error 0000000.
Whith a dll made with Delphi the program works.
I tried all the calling conventions in my Delphi program and none works
with the C++Builder dll.
Could you help me please,
Thanks,
best regards.
Here is the code :
//****************** C++ Builder DLL **************
extern "C" __declspec(dllexport) int somme(int,int);
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*
lpReserved)
{
return 1;
}
int somme(int a,int b)
{
return a+b;
}
//************** Delphi prog calling DLLs **************
var
Somme:function(i,j:integer):integer; stdcall;
// i've tried all the calling conventions cdecl, stdcall, register ...
hDLL:THandle;
NomDll :String;
procedure TForm1.ButtonChargerClick(Sender: TObject);
begin
NomDll:='Project1.dll';
hDLL:=LoadLibrary(PChar(NomDll));
if hDLL=0 then ShowMessage('erreur');
somme:=GetProcAddress(hDLL,'somme');
end;
procedure TForm1.ButtonUtiliserClick(Sender: TObject);
begin
Label1.Caption:=IntToStr(somme(2,2));
end;
//****************** Delphi DLL *******************
function add(a : integer; b : integer):integer;stdcall;
implementation
function add(a : integer; b : integer):integer;
begin
result:=a+b;
end; exports add name 'somme';
In your example code an error message is displayed when the dll is not
loaded. Is this error message displayed? If it is, then the application is
probably not finding your dll and you should not call the next line
(GetProcAddress).
--
Prolix Webmaster
http://www.prolix.be
The best newsgroup archive available
"TheCat" <the...@free.fr> wrote in message news:3C5A7C5F...@free.fr...
somme:=GetProcAddress(hDLL,'somme');
after the call somme=nil. 'somme' is not found in the DLL.
That's why I have an access violation.
Scaning the dll with an utility, I have seen that 'somme' was replaced
by '_somme' in the DLL.
I think that it is also related to the calling conventions.
Someone can help me on this subject ?
How can I force BCB to export the correct name ?
Thanks,
regards.
> extern "C" __declspec(dllexport) int somme(int,int);
> int somme(int a,int b)
> {
> return a+b;
> }
> procedure TForm1.ButtonChargerClick(Sender: TObject);
> begin
> NomDll:='Project1.dll';
> hDLL:=LoadLibrary(PChar(NomDll));
> if hDLL=0 then ShowMessage('erreur');
> somme:=GetProcAddress(hDLL,'somme');
if hDLL = 0 then
ShowMessage('erreur')
else
begin
somme := GetProcAddress(hDLL, 'somme');
if not Assigned(somme) then
begin
somme := GetProcAddress(hDLL, '_somme');
if not Assigned(somme) then
ShowMessage('erreur de chargement somme ou _somme');
end;
end;
Of course you could use TDUMP to find out which of both names is
actually exported, and make your code a little simpler <g>.
> end;
>
> procedure TForm1.ButtonUtiliserClick(Sender: TObject);
> begin
> Label1.Caption:=IntToStr(somme(2,2));
> end;
If "somme" is not the valid export name (it could be "_somme", for
instance), then GetProcAddress wil return nil, and calling somme (which
is nil) will result in an Access Violation (AV).
--
Rudy Velthuis (TeamB)
But how can I be sure that it always be functionname or _functionname ?
Is there a way to choose the exported name, as with Delphi ?
Je vois que tu parles francais : donc Merci :)
TheCat
> But how can I be sure that it always be functionname or _functionname ?
> Is there a way to choose the exported name, as with Delphi ?
You could use a .def file for your C++ dll, or have a look at BCB's
options:
Project / Options / Advanced Compiler / Output / Generate underscores
This is checked by default, IIRC. You could uncheck it.
> Je vois que tu parles francais : donc Merci :)
Seulement un très petit peu.
--
Rudy Velthuis (TeamB)
> How can I force BCB to export the correct name ?
Use a .def file.
--
Rudy Velthuis (TeamB)
Thanks again !
TheCat
> I don't understand why c++ add the underscores ? just to make the code
> more un-readeable ? :-P
I don't kow. I know that it is a practice that many C compilers use.
--
Rudy Velthuis (TeamB)
> In article <3C5AD628...@free.fr>, TheCat says...
>
> > I don't understand why c++ add the underscores ? just to make the code
> > more un-readeable ? :-P
>
> I don't kow. I know that it is a practice that many C compilers use.
OK, I asked around. Perhaps it was done on older compilers to
distinguish pascal calling convention (all uppercase, no leading
underscore) from the standard cdecl calling convention (leading
undserscore, case sensitive). It seems that most older compilers did it
that way, and current compilers (except VC++) still do it for
compatibility.
--
Rudy Velthuis (TeamB)