dll - How to translate C++ char** to Delphi? -


i have c++ dll:

int __stdcall hello(int numfiles, char **infiles) 

and don't know how translate char **. tried:

function hello(numfiles: integer; infiles: ppchar): integer; stdcall; external 'dll2.dll'; 

and then:

    files: array of pchar; begin    setlength(files, 2);    files[0] := pchar('siema.jpg');    files[1] := pchar('siema2.jpg');     c := hello(2, @files[0]); 

but "access violation"

on face of it, given information in question, code seems fine. however, rookie mistake, made many beginners interop, believe function's signature enough define interface. not. instance, parameter of type char** used many different things. so, specify information must specify semantics of parameters, not syntax.

given

int __stdcall hello(int numfiles, char **infiles) 

let assume following:

  • the return value error code, 0 indicating success.
  • the first parameter number of files.
  • the second parameter array of files, of length specified first parameter. each element of array c string, pointer null-terminated array of 8 bit characters.

with assumptions in place, write code this:

function hello(numfiles: integer; infiles: ppansichar): integer; stdcall;   external 'dll2.dll'; 

the function called this:

var   files: array of pansichar;   retval: integer; .... setlength(files, 2); files[0] := pansichar('siema.jpg'); files[1] := pansichar('siema2.jpg'); retval := hello(length(files), ppansichar(files)); if retval <> 0   ... handle error 

you might prefer write final parameter @files[0] if prefer. prefer cast because allows me pass empty array when range checking enabled.

note used pansichar match char*. pchar type alias either pansichar or pwidechar depending on delphi version. better explicit.

of course, assumptions may wrong. should confirm them consulting library's documentation.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -