I am experiencing some very strange things right now. When I am passing a struct from C++ to a Delphi DLL as a parameter everything works fine. However, as soon as I want to receive a record as a result I either get wrong values or exceptions. I deactivated the alignment of the record so that passing them should work! Heres the 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 this record as a parameter works but not as a result of a function!?
Can anybody help me?`
Thanks
PS: As I said, both C++ and Delphi show that the record is 8 bytes large.