user296191
user296191

Reputation: 405

How do I declare this dll method in Delphi?

I have a function in a DLL (the c header is):

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

I've tried

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';

But it crashes when I try to call the function.

But the following C# import works fine...

[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);

Any ideas?

Is is the way I need to initialise the Buffer and BufferSize in Delphi?

Upvotes: 2

Views: 1400

Answers (1)

Mason Wheeler
Mason Wheeler

Reputation: 84570

"Array of" is a Delphi type, and is not compatible with C arrays or pointers. Try this and see if it works:

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;

Upvotes: 3

Related Questions