需要在C++中调用Delphi DLL的简单演示
我不能很好地使用 C++,但现在我需要构建一个调用 Delphi DLL 并将字符串传递给 DLL 并返回新字符串的函数。
这是我的 Delphi DLL 代码:
library testdll;
uses
System.Classes,Winapi.Windows,System.SysUtils;
{$R *.res}
function hello(name : PWideChar):PWideChar;
var
rs:PWideChar;
begin
rs:=PWideChar('Hello '+rs);
Result:=rs;
end;
exports
hello;
begin
end.
任何人都可以帮助我用 C++ 创建简单的代码来调用 hello 函数并获取结果,感谢帮助。
i don't work well with C++ but now i need build a function that call Delphi DLL and pass a string to DLL and get return new string.
here is my Delphi DLL code:
library testdll;
uses
System.Classes,Winapi.Windows,System.SysUtils;
{$R *.res}
function hello(name : PWideChar):PWideChar;
var
rs:PWideChar;
begin
rs:=PWideChar('Hello '+rs);
Result:=rs;
end;
exports
hello;
begin
end.
Anyone can help me create simple code in C++ to call and get result form hello function, thank for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试将 PWideChar 连接到字符串文字并将其作为另一个 PWideChar 返回。那不会按原样工作。无论如何,您不应该返回 PWideChar。这会导致内存管理的噩梦。更好的设计是让调用者将缓冲区传递到 DLL 中进行填充,例如:
然后,给定这个 C++ 声明::
您可以根据需要以各种不同的方式调用它:
同一种代码可以如果您需要在 Delphi 中使用相同的 DLL,可以很容易地转换为 Pascal:
You are trying to concat a PWideChar to a String literal and return it as another PWideChar. That will not work as-is. You should not be returning a PWideChar anyway. That leads to memory management nightmares. A better design is to let the caller pass a buffer into the DLL to fill in instead, eg:
Then, given this C++ declaration::
You can call it all kinds of different ways, depending on your needs:
The same kind of code can be translated to Pascal very easily if you ever need to use the same DLL in Delphi:
更新 事实证明,Delphi 对
WideString
返回值使用了非标准调用约定。所以下面的代码将不起作用。基本概念是合理的,但您需要返回BSTR
或使用WideString
类型的out
参数。更多详细信息请参见:为什么 WideString 不能用作互操作的函数返回值?只要调用者知道要分配多大的缓冲区,Remy 的方法就很好。另一种方法是在 DLL 中分配内存并让调用者释放内存。仅当双方使用相同的分配器时这才有效。共享分配器的一个示例是 COM 分配器,COM
BSTR
当然使用它。在 Delphi 中,BSTR
映射到 WideString,这为我们提供了以下方法。Delphi
C++
显然,在这个简单的示例中,连接字符串所需的缓冲区大小很容易计算。但如果 DLL 中的实际函数更复杂,那么这种方法的优势就更明显。
Update It turns out that Delphi uses a non-standard calling convention for
WideString
return values. So the code below won't work. The basic concept is sound but you need to returnBSTR
or use anout
parameter of typeWideString
. More details here: Why can a WideString not be used as a function return value for interop?Remy's approach is good so long as the caller knows how big a buffer to allocate. An alternative approach is to allocate memory in the DLL and have the caller free the memory. This only works if both parties use the same allocator. An example of a shared allocator is the COM allocator and COM
BSTR
of course uses this. In Delphi aBSTR
maps to WideString which gives us the following approach.Delphi
C++
Obviously in this simple example, the required buffer size for the concatenated string is simple to calculate. But if the actual function in the DLL was more complex then this approach would become more obviously advantageous.