从 C++\CLI 使用许多参数调用 Delphi DLL

发布于 2024-12-10 11:48:23 字数 654 浏览 4 评论 0原文

我用 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 LoadLibraryGetProcAddress 函数中。 用法: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猛虎独行 2024-12-17 11:48:23

很可能您的调用约定不匹配。将 Delphi 中的 stdcall 更改为 cdecl 以匹配您的 C++/CLI 代码。

顺便说一句,如果您尝试从 DLL 返回一个不是存储在数据段只读内存中的文字的值,则需要小心字符串的生命周期。但这不是这里的问题,因为 PChar('Test') 与 DLL 具有相同的生命周期。

Most likely you have calling convention mismatch. Change the stdcall in the Delphi to cdecl 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文