如何在Delphi中声明这个dll方法?

时间:2010-08-19 01:20:06

标签: delphi dllimport

我在DLL中有一个函数(c头是):

int my_c_function(
    const int cnames[10], const char *delim, size_t delim_size,
    char *buffer, size_t *buffersize);

我试过

function my_c_function(cnames: Array of integer;
                       const delim : PAnsiChar;
                       delim_size : integer;
                       buffer : array of Byte;
                       BufferSize: integer): integer; cdecl;

implementation

function my_c_function(cnames: Array of integer;
                       const delim : PAnsiChar;
                       delim_size : integer;
                       buffer : array of Byte;
                       BufferSize: integer): integer; cdecl; external 'mydll.dll';

但是当我尝试调用该函数时它会崩溃。

但是下面的C#导入工作正常......

[DllImport("mydll.dll", CharSet = CharSet.Ansi,CallingConvention =CallingConvention.Cdecl)]
public static extern int my_c_function(int[] cnames, string delim, int  delim_size, StringBuilder buffer, ref  int BufferSize);

有什么想法吗?

我需要在Delphi中初始化Buffer和BufferSize吗?

1 个答案:

答案 0 :(得分:3)

“Array of”是Delphi类型,与C数组或指针不兼容。试试这个,看它是否有效:

type
  TIntArray10 = Array [0..9] of integer;

function my_c_function(cnames: TIntArray10;
                       const delim : PAnsiChar;
                       delim_size : cardinal;
                       buffer : PByte;
                       var BufferSize: cardinal): integer; cdecl;