Calling DLLs with pointers in Delphi -
i new delphi. have dll following exported function in it:
bool __stdcall myfunction(char * name, int * index)
this code calls dll function in c++ works perfectly:
typedef void (winapi * myfunction_t)(char *, int *); void main() { hmodule mydll = loadlibrary(l"c:\\mydll.dll"); myfunction_t myfunction = (myfunction_t)getprocaddress(mydll, "myfunction"); int index = 0; myfunction("mystring", &index); }
i need same in delphi. here code, not working (myfunction gets called index variable doesn't receive appropriate value). code excerpt please ignore disorder. input appreciated!
type tmyfunction= function(name: pchar; var index_ptr: integer): boolean; stdcall; var fmyfunction : tmyfunction; : integer; h: thandle; begin result := 0; h := loadlibrary('c:\\mydll.dll'); fmyfunction := getprocaddress(h, 'myfunction'); if @fmyfunction <> nil begin fmyfunction('mystring', i); result := i; end; freelibrary(h); end;
first of assuming using c linkage extern "c"
in case function defined in c++ translation unit.
if using delphi 2009 or later, need aware pchar pointer null-terminated wide character string.
to interop ansi c function need use:
type tmyfunction= function(name: pansichar; var index: integer): boolean; stdcall;
the c bool
type best mapped longbool
since it's not quite same delphi boolean
:
type tmyfunction= function(name: pansichar; var index: integer): longbool; stdcall;
you don't need escape \
in strings can write:
h := loadlibrary('c:\mydll.dll');
you ought check errors on call loadlibrary and, technically, h
hmodule
rather thandle
, although won't cause problems.
idiomatic delphi write:
if assigned(fmyfunction) fmyfunction('mystring', result);
basically looks reasonable me i'm suspicious of character width.
hope helps.
Comments
Post a Comment