views:

108

answers:

1

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?

+3  A: 

"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;
Mason Wheeler
size_t should be Cardinal
Henri Gourvest
Shouldn't BufferSize be 'var' too?
shunty
Delphi-syntax requires the array to be defined as separate type: "type TIntArray10 = Array [0..9] of integer;" then "function my_c_function(cnames : TIntArray10 ...".
Ville Krumlinde
Yep, you guys are all right. Fixed it. Sorry about that; it was late at night when I wrote this.
Mason Wheeler
Perfect.. Thanks everyone..!Much appreciated.....