1 调用时type TTest=function(Instr1,Instr2:PChar; retMsg:PChar):Integer;stdcall;缺少Var标识。
2 D7的Pchar是PAnsiChar XE后是PWidechar。
不多说了
Library TestDll;
Uses
System.SysUtils,
System.Classes;
Function Test(Instr1, Instr2: PAnsiChar; Var retMsg: PAnsiChar): Integer; Stdcall;
Var
str1, str2: String;
Begin
str1 := String(Instr1);
str2 := String(Instr2);
//retMsg := PAnsiChar(ansiString(str1 + ‘-‘ + str2));
StrCopy(retMsg, PAnsiChar(ansiString(str1 + ‘-‘ + str2)));
Result := 1;
End;
Exports
Test;
End.
Type
TTest = Function(Instr1, Instr2: PAnsiChar; Var retMsg: PAnsiChar): Integer; Stdcall;
Var
lhandle: THandle;
ltest: TTest;
retMsg: PAnsiChar;
Begin
lhandle := LoadLibrary(‘TestDll.dll‘);
If lhandle > 32 Then
Begin
@ltest := GetProcAddress(lhandle, ‘Test‘);
GetMem(retMsg, 256);
If ltest(PChar(Edit1.Text), PChar(Edit2.Text), retMsg) = 1 Then
Begin
ShowMessage(‘调用成功,返回Msg:‘ + retMsg);
End;
FreeMem(retMsg);
End;
Type
TTest = Function(Instr1, Instr2: PAnsiChar; Var retMsg: PAnsiChar): Integer; Stdcall;
Var
lhandle: THandle;
ltest: TTest;
retMsg: PAnsiChar;
Begin
lhandle := LoadLibrary(‘TestDll.dll‘);
If lhandle > 32 Then
Begin
@ltest := GetProcAddress(lhandle, ‘Test‘);
GetMem(retMsg, 256);
If ltest(PAnsiChar(Ansistring(Edit1.Text)), PAnsiChar(Ansistring(Edit2.Text)), retMsg) = 1 Then
Begin
ShowMessage(‘调用成功,,返回Msg:‘ + retMsg);
End;
FreeMem(retMsg);
End;