Re: problem using delphi dll in vc++

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Ryanivanka wrote:

so I wonder if compiler make that call using "fastcall" not
"stdcall",and the registers and stack were wrong. but,as I said before
"stdcall" is used in its ".h". Is that enough to convert a delphi
calling convention to a c++ way? or what I should do to convert
".h",".lib" or".dll"?

In a function call both sides have to agree about the "protocol"
details. It's not enough to add __stdcall to the .h file, you also have
to do it on the Delphi side. For example,

procedure SayHi; stdcall;
begin
ShowMessage("Hi");
end;

This would be declared as void __stdcall SayHi(); in the .h file. If you
miss the stdcall from either side, it won't work. Optionally, you could
use cdecl on both sides, Delphi can handle that. As Cholo said, don't
use fastcall, it's not compatible across compilers.

In addition to making the calling conventions compatible, you also have
to make sure that the argument sizes and alignments match. For example,
Borland's enumerations have the size of 1 byte by default, while in VC++
an enum is treated as an integer (4 bytes). String arguments must be
passed as PChar/char*. Some Delphi floating point types aren't
compatible with VC++ floating point types. Delphi functions are able to
return types not allowed in C/C++ (such as arrays). If you allocate
memory in Delphi, you must delete it from Delphi, too.

I think you should show us your C and Delphi declarations, and we should
be able to compare them for you.

Delphi classes must be flattened into C-style functions before you can
use them from VC++, or they can be wrapped into COM objects.

Tom
.



Relevant Pages

  • Re: about getting dir and casting
    ... convention (The default for Delphi). ... It'd either use Stdcall (If the writer of the library wanted to make it as easy to use as possible), ...
    (comp.lang.pascal.delphi.misc)
  • Re: Using a delphi dll in MS VC++
    ... I can't find a calling convention in MS docs that pushes from ... He said it was the closest calling convention, not that it was a perfect ... If the DLL was really meant to be used from languages other than Delphi, ... the author should have used __stdcall (stdcall -- without the underscores ...
    (borland.public.delphi.nativeapi)
  • Re: Calling a Delphi DLL
    ... In Delphi you should... ... Use "stdcall" calling convention (By default Delphi use its own calling ... Cholo Lennon ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Pointers, DLL Header files and lots of exceptions?
    ... > When choosing a calling convention, Delphi uses the last one specified. ... > In a Win32 program, cdecl is mentioned before stdcall, so Delphi uses ... enteracts with the serial port to capture data for a specific device. ... Any tutorials online that specifically discuss CPU window ...
    (comp.lang.pascal.delphi.misc)
  • Re: Records to DLL
    ... So define the records as packed on the Delphi ... > equivalent to force the same packing to be used. ... function Init: Integer; stdcall; ... If I remove __stdcall I can call ...
    (borland.public.delphi.language.objectpascal)