Home > Back-end >  DELPHI call DLL problem, C LPSTR * what replacement type, please advice.
DELPHI call DLL problem, C LPSTR * what replacement type, please advice.

Time:09-28

DLL prototype:

Void OperateDisk (LPSTR strInputInfo, LPSTR * strOutputInfo)

I generate the Delphi interface

Procedure OpenXX (strInputInfo: pchar; Var strOutputInfo: pPAnsiChar); Cdecl; External 'OPEN_DLL. DLL';

Call the test code:
Var
S1: pchar;
S2: pPAnsiChar;
The begin

S1:='12345678'.
OpenXX (s1, s2);
StrValue:=strpas (@ s2);
ShowMessage (' length: '+ IntToStr (StrLen (@ s2)));
Showmessage (StrValue);
The end;

After the compilation is normal, but call execution will address errors,
Please expert advice, thank you!

CodePudding user response:

LPSTR * corresponding PPAnsiChar is right, also can be PLPSTR, Windows unit statement.
Void OperateDisk (LPSTR strInputInfo, LPSTR * strOutputInfo); Should be translated into
The following two kinds of any kind will do.
Procedure OperateDisk (strInputInfo: LPSTR. Var strOutputInfo: LPSTR); Cdecl; External 'OPEN_DLL. DLL';
Procedure OperateDisk (strInputInfo: LPSTR. StrOutputInfo: PLPSTR); Cdecl; External 'OPEN_DLL. DLL';
Another is cdecl or stdcall agreed the original poster to reconfirm
Two types of statement calling difference is:
Procedure OperateDisk (strInputInfo: LPSTR. Var strOutputInfo: LPSTR); Cdecl; External 'OPEN_DLL. DLL';
.
Var
S1: LPSTR.
S2: LPSTR.
The begin

S1:='12345678'.
OperateDisk (s1, s2);
Showmessage (StrPas (s2));
The end;


Procedure OperateDisk (strInputInfo: LPSTR. StrOutputInfo: PLPSTR); Cdecl; External 'OPEN_DLL. DLL';
.
Var
S1: LPSTR.
S2: LPSTR.
The begin

S1:='12345678'.
OperateDisk (s1, @ s2);
Showmessage (StrPas (s2));
The end;

Or add a phrase "
Procedure OperateDisk (strInputInfo: LPSTR. Var strOutputInfo: LPSTR); Cdecl; The phrase "; External 'OPEN_DLL. DLL';
Procedure OperateDisk (strInputInfo: LPSTR. StrOutputInfo: PLPSTR); Cdecl; The phrase "; External 'OPEN_DLL. DLL';
This can be in two ways, will have the same function call

CodePudding user response:

@ wr960204
Thank you for your advice, but according to your method, after the execution, the return value, but all of the code after the execution is to address an exception!
Are there any other reasons? Is a pointer not stated length or destruction caused by the
Can add my QQ: 46513544, help! Thank you very much!
According to the
Stdcall seems to also not line!

CodePudding user response:

Use PChar see

CodePudding user response:

reference hongss reply: 3/f
with PChar see


Also not line, after the execution is also address wrong!

CodePudding user response:

Var
S1: LPSTR.
s2: an array of Char [0.. 99];


So try

CodePudding user response:

Var
S1: pchar;
S2: pPAnsiChar;
The begin

S1:='12345678'.
OpenXX (s1, s2);
StrValue:=strpas (s2 ^);

CodePudding user response:

refer to 6th floor sz_haitao response:
var
S1: pchar;
S2: pPAnsiChar;
The begin

S1:='12345678'.
OpenXX (s1, s2);
StrValue:=strpas (s2 ^);


StrValue:=strpas (s2 ^); The abnormal directly address

CodePudding user response:

refer to 7th floor XXXXXXXX response:
Quote: refer to the sixth floor sz_haitao response:

Var
S1: pchar;
S2: pPAnsiChar;
The begin

S1:='12345678'.
OpenXX (s1, s2);
StrValue:=strpas (s2 ^);


StrValue:=strpas (s2 ^); That directly address the abnormal


S2 is ppansichar, that s2 is pansichar ^

CodePudding user response:

Try the procedure OpenXX (strInputInfo: pchar; Var strOutputInfo: PAnsiChar); Cdecl; External 'OPEN_DLL. DLL';
Cdecl or stdcall yourself adjustable

CodePudding user response:

Procedure OpenXX (strInputInfo: pchar; Var strOutputInfo: pchar); Cdecl; External 'OPEN_DLL. DLL';

StrOutputInfo used strInputInfo GETMEM allocate some memory such as 1024204 8
Then use the StrPCopy (), flow of StrPas
The last FreeMem
  • Related