【问题标题】:How to call C++ DLL in Delphi with string parameters?如何在 Delphi 中使用字符串参数调用 C++ DLL?
【发布时间】:2014-03-04 10:23:18
【问题描述】:

我的 Delphi 应用程序正在从 C++ DLL 调用一个函数,该函数应该返回这样的字符串。

C++ DLL

__declspec( dllexport ) void sample(char* str1, char* str2)
{
    strcpy(str1, "123");
    strcpy(str2, "abc");
}

德尔福

procedure sample(Str1, Str2: pchar); cdecl; external 'cpp.dll';
var
  buf1 : Pchar;
  buf2 : Pchar;
begin
  sample(@buf1, @buf2);
  //display buf1 and buf2
  //ShowMessage(buf1); //it display random ascii characters 
end;

这样做的正确方法是什么?

【问题讨论】:

  • 不太清楚是什么问题?

标签: c++ delphi


【解决方案1】:

您需要为要写入的 C++ 代码分配内存。例如:

var
  buf1, buf2: array [0..255] of Char;
begin
  sample(buf1, buf2);
end;

您还应该重新设计接口以接受缓冲区的长度,从而允许 DLL 代码避免缓冲区溢出。

【讨论】:

    猜你喜欢
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 2023-01-07
    • 1970-01-01
    相关资源
    最近更新 更多