Board index » delphi » Importing DLL functions returning a struct in Delphi 2.0

Importing DLL functions returning a struct in Delphi 2.0

I have a problem importing a DLL function to Delphi 2.0
The function is

POINT EXPORT_API FAR PASCAL ptMVGetSize(LPMV lpMv);

I wrote the following Delphi line

function ptMVGetSize(mv: PMV); TPoint; stdcall;

It didn't work. It seems that Delphi treats structures to be
returned from a function differently than C/C++.

Does anybody know how to make it working.

Best regards,

Jaakko
--
Jaakko Salmenius        | Helsinki University of Technology
salme...@niksula.hut.fi        | The believable ski heroes, at least in my opinion,
phone +358 0 512 0306   | are the ones who ski the extreme and watch Star Trek.

 

Re:Importing DLL functions returning a struct in Delphi 2.0


Quote
Jaakko J Salmenius wrote:

> I have a problem importing a DLL function to Delphi 2.0
> The function is

> POINT EXPORT_API FAR PASCAL ptMVGetSize(LPMV lpMv);

> I wrote the following Delphi line

> function ptMVGetSize(mv: PMV); TPoint; stdcall;

> It didn't work. It seems that Delphi treats structures to be
> returned from a function differently than C/C++.

> Does anybody know how to make it working.

Nobody agrees on how to return small structs. If your DLL is written
in MS C++ for instance, it won't work with Borland products.
Another such incompatibility is the returning of floating points.
You might be able, with some assembly and heavy debugging (if you
don't have the source to the DLL) to figure out how the DLL
compiler passes the struct, and then convert that to whatever
Delphi expects...

Good luck! :*)

M.

--
Martin Larsson, author of several unknown utilities for DOS and Windows.
mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin
X.400:G=martin;S=larsson;O=delfi-data;P=msmail;A=telemax;C=no

Re:Importing DLL functions returning a struct in Delphi 2.0


Quote
Jaakko J Salmenius wrote:

> I have a problem importing a DLL function to Delphi 2.0
> The function is

> POINT EXPORT_API FAR PASCAL ptMVGetSize(LPMV lpMv);

> I wrote the following Delphi line

> function ptMVGetSize(mv: PMV); TPoint; stdcall;

> It didn't work. It seems that Delphi treats structures to be
> returned from a function differently than C/C++.

> Does anybody know how to make it working.

As commented before, it's an incompatibility between different
compiler makers, not languages.
The following answer is from an article by Ron Burk in Windows
Developer's Journal, July 1996.
The problem is the return of structs. Large structs are returned
by having the caller allocate space on the stack (for the struct),
and then pushing a pointer to that space as an extra parameter to
the called function. Unfortunately (for you) Borland thinks this
is slow for small structs (which is true), so if the entire struct
is less than 8 bytes, it will be returned in EDX:EAX (4 bytes and
DX:AX in 16 bit). Microsoft doesn't agree, and uses the 'add-extra-
parameter' method always. Assuming your DLL is compiled with a
Microsoft compiler, do the following:

{ Declare the DLL function as such: }
procedure ptMVGetSize(mv: PMV, var p); stdcall; { name, index ... }

then call it as such:

var
  tp : TPoint;
begin
  ptMVGetSize(mv, tp);
end;

Hope this helps.

M.

--
Martin Larsson, author of several unknown utilities for DOS and Windows.
mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin
X.400:G=martin;S=larsson;O=delfi-data;P=msmail;A=telemax;C=no

Other Threads