c++ - 在 Delphi 6 中使用 C++ DLL

标签 c++ delphi winapi dll delphi-6

我必须在 Delphi 6 中使用来自外部 C++ DLL 的编码函数。 以下是提供的声明:

long <Function Name> (char *Data, long &Apply, char *ReturnVal, long &Size)

Data 是输入值,Apply 是 bool 值(默认:FALSE),ReturnVal 是 DLL 的返回值,Size 是 ReturnVal 的长度。

为了在 Delphi 6 中使用它,我编写了以下代码:

implementation
   const
     EncoderDLL = '<DLL NAME>';
     FunctionName = 'FUNCTION NAME';
   var
      _TEST : function(const Data : PChar; Apply : PInteger;stOutput : Pchar;
          iSize : PInteger) : integer; stdcall;
   .....
   .....
   var
     stInput,stOutput : string;
     iLength,i1,iResult : integer;
     hnd : THandle;
   begin
     iLength := 0;
     i1 := 0;
     stInput := Trim(edtInput.Text);
     hnd := SafeLoadLibrary(EncoderDLL);
     if hnd > 0 then
     begin
       @_TEST := GetProcAddress(hnd,FunctionName);
       if @_TEST <> nil then
       begin
         iResult := _TEST(PChar(stInput),@i1,PChar(StOutput),@iLength); // ERROR
       end;
     end;
     FreeLibrary(hnd);
  end;

我在注释为“错误”的行中遇到访问冲突

如果我将函数声明中的 PChar 替换为 string,则访问冲突不会出现在同一行。它是在释放库时出现的。此外,iLength 参数的值已正确填充。

任何人都可以提供解决此问题的指示。

最佳答案

  1. 您确定 stdcall 约定吗?如果 C++ 文本中没有明确说明(如 WINAPI、__stdcall 等),则使用 cdecl。

  2. 'const' before Data is not needed

  3. 为StOutput分配内存

关于c++ - 在 Delphi 6 中使用 C++ DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12744947/

相关文章:

Delphi SAPI 文本转语音

c# - CreateFile 方法中的 dwFlagsAndAttributes = 0 是什么意思?

c++ - log(n) std::find_if 可能吗?

c++ - 文件映射对象和文件对象可以互换使用吗?

Windows 8 上的 MySQL 和 Zeoslib : None of the dynamic libraries can be found or not loadable (libmysql. dll)

delphi - 带位图的简单组合框

c++ - Boost::spirit(经典)基元与自定义解析器

c++ - "double"的操作和 C 中的优化

c# - 将击键发送到嵌入在网络浏览器控件中的 Flash/Silverlight 应用程序(已完成)

c++ - 从 ShellExecute 获取 PID