我現在正在經歷一些非常奇怪的事情。 當我從C++傳遞一個結構爲Delphi DLL作爲參數時,一切正常。 但是,只要我想要收到一條記錄,我就會得到錯誤的值或異常。 我停用記錄的對齊方式,以便傳遞它們應該可以工作!這裏是代碼!將記錄作爲函數結果從Delphi DLL傳遞到C++
德爾福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++調用:
#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();
}
}
我有不知道爲什麼通過這個記錄作爲參數的作品,但不是作爲一個函數的結果!?
任何人可以幫助我`
感謝
PS:正如我所說的,C++和Delphi的顯示,記錄爲8個字節大。
你是否真的在Delphi函數中設置了返回值? – Angew 2013-04-20 10:16:31
Result.Nr1:= 1; Result.Nr2:= 201;這應該做 – Henry 2013-04-20 10:16:59
對不起,當我最後一次與Delphi交互時,'Result'仍然使用了函數標識符IIRR。 – Angew 2013-04-20 10:19:32