Passing record as a function result from Delphi DLL to C++ -


i experiencing strange things right now. when passing struct c++ delphi dll parameter works fine. however, want receive record result either wrong values or exceptions. deactivated alignment of record passing them should work! heres code!

delphi dll:

tsimplerecord = packed record   nr1 : integer;   nr2 : integer; end;  //...  function ttest() : tsimplerecord; cdecl; begin   result.nr1 := 1;   result.nr2 := 201;   showmessage(inttostr(sizeof(result))); end; 

c++ call :

#pragma pack(1) struct tsimplerecord {     int nr1;     int nr2; };  //...      typedef tsimplerecord (__cdecl testfunc)(void);     testfunc* function;     hinstance hinstlibrary = loadlibrary("reactions.dll");     if (hinstlibrary)     {         function = (testfunc*)getprocaddress(hinstlibrary, "ttest");         if (function)         {             tsimplerecord result = {0};             result = function();             printf("%d - %d - %d", sizeof(result), result.nr1, result.nr2);             cin.get();         }     } 

i have got no idea why passing record parameter works not result of function!?

can me?`

thanks

ps: said, both c++ , delphi show record 8 bytes large.

some compilers return struct types (possibly depending on size) in registers, others add hidden parameter result should stored. unfortunately, looks you're dealing 2 compilers not agree on how return these.

you should able avoid problem explicitly using out parameter instead.

procedure ttest(out result: tsimplerecord); cdecl; begin   result.nr1 := 1;   result.nr2 := 201; end; 

do not forget update c++ code accordingly.

rudy velthuis has written this:

this showed me abcvar struct returned in registers edx:eax (edx top 32 bits, , eax lower ones). not delphi records @ all, not records of size. delphi treats such return types var parameters, , not return (so function procedure).

[...]

the type delphi returns edx:eax combination int64.

which suggests alternative way avoid problem is

function ttest() : int64; cdecl; begin   tsimplerecord(result).nr1 := 1;   tsimplerecord(result).nr2 := 201; end; 

note delphi allows such type punning in situations behaviour undefined in c++.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -