从 C++\CLI 使用许多参数调用 Delphi DLL
我用 Delphi 2010 用两种方法构建了 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 代码
在 header
typedef const wchar_t* (*pFUNC1)(int a, int b);
pFUNC1 TestFoo1;
typedef const wchar_t* (*pFUNC2)(int a, int b, int c);
pFUNC2 TestFoo2;
Initialize by LoadLibrary
和 GetProcAddress
函数中。 用法:TestFoo1(0,0)
和 TestFoo2(0,0,0)
;
两者都在发布模式下工作。
但在调试模式下 Foo2 被中止。
有什么问题请指教。
I have Delphi 2010 built DLL with two methods:
function Foo1(a, b: Integer):PChar; export; stdcall;
function Foo2(a, b, c:Integer):PChar; export; stdcall;
exports Foo1, Foo2;
Each of them returns Result := PChar('Test')
.
My C++\CLI code
in header
typedef const wchar_t* (*pFUNC1)(int a, int b);
pFUNC1 TestFoo1;
typedef const wchar_t* (*pFUNC2)(int a, int b, int c);
pFUNC2 TestFoo2;
Initialize by LoadLibrary
and GetProcAddress
functions.
Usage: TestFoo1(0,0)
and TestFoo2(0,0,0)
;
Both works in Release mode.
But in Debug mode Foo2 is being aborted.
Please advise what is wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很可能您的调用约定不匹配。将 Delphi 中的
stdcall
更改为cdecl
以匹配您的 C++/CLI 代码。顺便说一句,如果您尝试从 DLL 返回一个不是存储在数据段只读内存中的文字的值,则需要小心字符串的生命周期。但这不是这里的问题,因为
PChar('Test')
与 DLL 具有相同的生命周期。Most likely you have calling convention mismatch. Change the
stdcall
in the Delphi tocdecl
to match your C++/CLI code.As an aside, you will need to be careful with the lifetime of your strings if ever you attempt to return a value from the DLL that is not a literal stored in read-only memory in the data segment. But that's not the problem here because
PChar('Test')
has the same lifetime as the DLL.