Board index » delphi » MS VC++ DLL called from Delphi 2.0

MS VC++ DLL called from Delphi 2.0

Ok,
I've looked at this a while and it is not clear to me exactly how I
must declare the external procedure in Delphi 2.0 so that I correctly
link with a DLL function that I have previously written with Visual
C++ 4.0.

The MS VC++ help led me to declare the function as such:

unsigned short __declspec(dllexport) stdcall FunctionName (unsigned
short arg1, unsigned char *arg2 );

According to MS VC++ help, this will generate an export in the DLL
with a prepended underscore and a postfix of '@' followed by the
number of bytes of parameters - like so:  _FunctionName@6.  Sure
enough, examining the DLL indicates that the function is exported as
stated.

However, Delphi 2.0 help, indicates that I can specify the calling
convention as register, pascal, cdecl, and stdcall.  But I just can't
seem to get the dynamic link to work.

The function is declared in Delphi 2.0 as:

procedure FunctionNameI(arg1: integer; arg2: PChar); stdcall; external
'MYDLL.DLL';

I have replaced 'stdcall' with all the other calling conventions and
still no luck.  I have also changed the way it was exported in C++
(had to keep '__declspec(dllexport)' however).

Can anyone direct me to what I am doing wrong, or recommend a change
to either the Delphi declaration or the VC++ definition?

Many thanks in advance
Bill

 

Re:MS VC++ DLL called from Delphi 2.0


Quote
Bill Milam wrote:
> Ok,
> I've looked at this a while and it is not clear to me exactly how I
> must declare the external procedure in Delphi 2.0 so that I correctly
> link with a DLL function that I have previously written with Visual
> C++ 4.0.

Use the following:

procedure PROCNAME(Params); stdcall; external 'MYLIB.DLL' name   '_PROCNAME@16';

where the @16 is replaced by @numbytes of params and MYLIB.DLL is the name of your
DLL.

That works for me...

Peter -><-

Re:MS VC++ DLL called from Delphi 2.0


Quote
In article <4ngjus$...@news.icanect.net>, bmi...@icanect.net (Bill Milam) wrote:
> Ok,
> I've looked at this a while and it is not clear to me exactly how I
> must declare the external procedure in Delphi 2.0 so that I correctly
> link with a DLL function that I have previously written with Visual
> C++ 4.0.

> The MS VC++ help led me to declare the function as such:

> unsigned short __declspec(dllexport) stdcall FunctionName (unsigned
> short arg1, unsigned char *arg2 );
> The function is declared in Delphi 2.0 as:

> procedure FunctionNameI(arg1: integer; arg2: PChar); stdcall; external
> 'MYDLL.DLL';

The only change you need is to add the following text to your C++
declaration:

extern "C"

This tells C++ not do do the name mangling that interfers with the Delphi
symbol binding.

I've done this in production using 'cdecl' calling sequence, but I'm
sure the principles are the same.

Hope this helps.

--
Glenn Chambers
gcham...@bright.net
Toledo, OH

Re:MS VC++ DLL called from Delphi 2.0


Thanks to input from others via Email, I have solved the problem.

First off, the example declaration and reference were hand-typed, not
copied directly from the application/DLL so there was at least one
typo - sorry, I should have mentioned that it was not an exact copy of
the code.

As it finally turned out, I was able to link to and communicate with
the DLL function by using the __cdecl calling convention.  I had tried
cdecl (note the missing leading underscores) but that did not work.
It seems that cdecl and __cdecl are handled differently by the
compiler, even though I found no such indication in the Visual C++
help.

By replacing the stdcall C++ definition to __cdecl, and replacing the
stdcall reference in Delphi to cdecl, it worked properly.  I did get a
pointer to a possible problem with differences in parameter sizes, but
that did not seem to cause a problem, even though it really should be
consistent.

I learned that just because two things look similar, that doesn't
gaurantee that they are treated similar by a compiler: cdecl IS
different than __cdecl.

Thanks for all your help,
Bill

Quote
bmi...@icanect.net (Bill Milam) wrote:
>Ok,
>I've looked at this a while and it is not clear to me exactly how I
>must declare the external procedure in Delphi 2.0 so that I correctly
>link with a DLL function that I have previously written with Visual
>C++ 4.0.
>The MS VC++ help led me to declare the function as such:
>unsigned short __declspec(dllexport) stdcall FunctionName (unsigned
>short arg1, unsigned char *arg2 );
>According to MS VC++ help, this will generate an export in the DLL
>with a prepended underscore and a postfix of '@' followed by the
>number of bytes of parameters - like so:  _FunctionName@6.  Sure
>enough, examining the DLL indicates that the function is exported as
>stated.
>However, Delphi 2.0 help, indicates that I can specify the calling
>convention as register, pascal, cdecl, and stdcall.  But I just can't
>seem to get the dynamic link to work.
>The function is declared in Delphi 2.0 as:
>procedure FunctionNameI(arg1: integer; arg2: PChar); stdcall; external
>'MYDLL.DLL';
>I have replaced 'stdcall' with all the other calling conventions and
>still no luck.  I have also changed the way it was exported in C++
>(had to keep '__declspec(dllexport)' however).
>Can anyone direct me to what I am doing wrong, or recommend a change
>to either the Delphi declaration or the VC++ definition?
>Many thanks in advance
>Bill

Other Threads