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

时间:2021-09-06 19:47:28

I have Delphi 2010 built DLL with two methods:

我用两种方法构建了Delphi 2010 DLL:

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') .

它们每个都返回结果:= PChar('Test')。

My C++\CLI code

我的c++ / CLI的代码

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);

由LoadLibrary和GetProcAddress函数初始化。用法:TestFoo1(0,0)和TestFoo2(0,0,0);

Both works in Release mode.
But in Debug mode Foo2 is being aborted.

两者都在发布模式下工作。但是在调试模式下,Foo2将被中止。

Please advise what is wrong.

请告知哪里出了问题。

1 个解决方案

#1


4  

Most likely you have calling convention mismatch. Change the stdcall in the Delphi to cdecl to match your C++/CLI code.

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

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.

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

#1


4  

Most likely you have calling convention mismatch. Change the stdcall in the Delphi to cdecl to match your C++/CLI code.

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

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.

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