2011-10-17 40 views
7

我有德爾福2010內置DLL有兩種方法:從C++ CLI調用DLL德爾福與許多參數

function Foo1(a, b: Integer):PChar; export; stdcall; 
function Foo2(a, b, c:Integer):PChar; export; stdcall; 

exports Foo1, Foo2; 

他們每個人都返回Result := PChar('Test')

我的C++ \ CLI代碼

在頭

typedef const wchar_t* (*pFUNC1)(int a, int b); 
pFUNC1 TestFoo1; 

typedef const wchar_t* (*pFUNC2)(int a, int b, int c); 
pFUNC2 TestFoo2; 

初始化由LoadLibraryGetProcAddress功能。 用法:TestFoo1(0,0)TestFoo2(0,0,0);

兩者都在發佈模式下工作。
但在調試模式下Foo2正在中止。

請告知什麼是錯的。

回答

4

最有可能你有調用約定不匹配。將Delphi中的stdcall更改爲cdecl以匹配您的C++/CLI代碼。另外,如果您試圖從DLL中返回一個不是存儲在數據段的只讀內存中的字面值的值,則需要注意字符串的生命週期。但這不是問題,因爲PChar('Test')與DLL有相同的生命週期。

+0

賓果!有用。我使用StrAlloc,StrPCopy,StrDispose進行內存使用。 – RredCat 2011-10-17 13:47:01