【问题标题】:Return string array from dll function [duplicate]从dll函数返回字符串数组[重复]
【发布时间】:2015-04-09 11:57:38
【问题描述】:

如何从 C++ dll 返回一个字符串数组,然后该函数将被 Delphi 应用程序调用。

我试过了:

C++ DLL

#include <windows.h>
#include <vector>
#include <string>

using namespace std;

extern "C"
{
    __declspec( dllexport ) void arrayStr(vector<string> s)
    {

        s.push_back("111");
        s.push_back("222");
        s.push_back("333");
    }

}

德尔福

procedure arrayStr(StrMem : TStringList); cdecl; external 'arrayStr.dll';
...
var
  StrMem : TStringList;
  i : integer;
begin
  StrMem := TStringList.Create;
  arrayStr(StrMem);
  for i := 0 to StrMem.Count-1 do
  begin
    ShowMessage(StrMem[i]);
  end;
  StrMem.Free;
end;

【问题讨论】:

    标签: c++ delphi delphi-7


    【解决方案1】:

    TStringList (Delphi) 与 C++ STL 容器不兼容。

    您应该执行以下操作:

    C/C++端:

    void __stdcall Func(char **strings, int count);
    

    德尔福方面:

    type PPAnsiChar = ^PAnsiChar;
    procedure Func(ArrayOfStrings: PPAnsiChar; CountOfArray: Integer); stdcall;
    

    【讨论】:

    • Delphi中的动态数组和char**不兼容,最好加一个长度参数和一个指针或者固定大小的数组
    • @Andrew:如果我是你,我会调整答案。
    • 需要进行更改
    • 不,这行不通。继续尝试。或者也许最好放弃,删除这个,让用户在欺骗中阅读优秀的答案
    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2015-01-02
    • 2016-02-15
    • 2015-03-04
    相关资源
    最近更新 更多