call c dll from delphi with strings -
i trying call dll delphi xe5.
i have spent day or googling things "call c dll delphi" , found number of pages nothing helped me.
i have received examples of how call dll in vb:
declare function ixcommand lib "ixxdll.dll" (byval command string, byval mailbox string) integer ... sub command1_click () dim command string * 135 dim mailbox string * 135 command = "move:a,1000" ixcommand( command, mailbox) end sub
also calling dll in vc 6.0:
#include "stdafx.h" #include "windows.h" #include "stdio.h" #include "string.h" typedef uint (callback* lpfnixdllfunc)(char *ixstr, char *mbstr); int main(int argc, char* argv[]) { hinstance hdll; // handle dll lpfnixdllfunc lpfnixdllfunc; // function pointer hdll = loadlibrary( "ixxdll.dll" ); if (hdll == null) // fails load indexer lpt { printf("can't open ixxdll.dll\n"); exit(1); } else // success opening dll - dll function pointer { lpfnixdllfunc = (lpfnixdllfunc)getprocaddress(hdll, "ixcommand"); } printf( "type indexer lpt command , press <enter> send\n" ); printf( "type \"exit\" , press <enter> quit\n\n" ); while( 1 ) { char ix_str[135]; // string sent indexer lpt char mailbox_str[135]; // results call indexer lpt gets( ix_str ); // string console if( _stricmp( ix_str, "exit" ) == 0 ) // leave program if "exit" break; lpfnixdllfunc( ix_str, mailbox_str ); // otherwise call indexer lpt printf( "%s\n\n", mailbox_str ); // print results } freelibrary( hdll ); return 0; }
a complication have noticed need define size of memory allocation before calling dll, shown above.
the dll takes command in first argument , returns result text in second argument.
here delphi code have generated try , call dll. know dll loads because has splash screen shows. no error generated when call dll. see used arrays allocate space , assigned locations pchar variables. not have header file original dll, nor source code. see declared external function using stdcall have tried cdecl no change.
the problem: information returned in arg2 not expected text string string of translates non-english characters (looks chinese).
i guessing not sending dll correct variable types.
the question: can me formulate declaration of external function , use correctly, text strings desired?
see below:
function ixcommand (command : pchar; mailbox : pchar) : integer; stdcall; external 'ixxdll.dll'; ... procedure tfrmxyz.btn1click(sender: tobject); var localresult : integer; arg1, arg2 : pchar; arraystrcmd : array[0..134] of char; arraystrmbx : array[0..134] of char; begin arraystrcmd := 'accel?:a' + #0; arraystrmbx := ' ' + #0; arg1 := @arraystrcmd; arg2 := @arraystrmbx; localresult := ixcommand(arg1, arg2); end;
the problem character encodings. in delphi 2009+, pchar
alias pwidechar
, dll using ansi character strings instead, need use pansichar
instead of pchar
.
try this:
function ixcommand (command : pansichar; mailbox : pansichar) : integer; stdcall; external 'ixxdll.dll'; ... procedure tfrmxyz.btn1click(sender: tobject); var localresult : integer; arraystrcmd : array[0..134] of ansichar; arraystrmbx : array[0..134] of ansichar; begin arraystrcmd := 'accel?:a' + #0; localresult := ixcommand(arraystrcmd, arraystrmbx); end;
alternatively:
function ixcommand (command : pansichar; mailbox : pansichar) : integer; stdcall; external 'ixxdll.dll'; ... procedure tfrmxyz.btn1click(sender: tobject); var localresult : integer; arraystrcmd, arraystrmbx : ansistring; begin setlength(arraystrcmd, 135); strpcopy(arraystrcmd, 'accel?:a'); setlength(arraystrmbx, 135); localresult := ixcommand(pansichar(arg1), pansichar(arg2)); end;
Comments
Post a Comment