我有一个带有这个函数签名的 DLL :
function KP_RecuperarDadosFabricante(EnderecoIp, ChaveAcesso,
Porta: string; Return: PAnsiChar): Integer; stdcall; external 'Key.dll';
这是 dll 手册中的示例 VB.NET 代码:
Dim ret As Integer
Dim ptrStr As IntPtr
Dim DadosFab As String
ptrStr = System.Runtime.InteropServices.Marshal.AllocHGlobal(256)
ret = KP_RecuperarDadosFabricante("192.168.1.201", "0", "4370", ptrStr)
DadosFab = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(ptrStr)
System.Runtime.InteropServices.Marshal.FreeHGlobal(ptrStr)
这是我的德尔福代码:
procedure TForm1.BitBtn1Click(Sender: TObject);
var
Return: string;
pReturn: PAnsiChar;
begin
GetMem(pReturn, 512);
FillChar(pReturn^, 512, 0);
Memo1.Text:='';
if KP_RecuperarDadosFabricante("192.168.1.201", "0", "4370", pReturn) = 1 then
begin
Return := StrPas(pReturn);
ShowMessage(Return);
end
else
begin
ShowMessage('Error');
end;
end;
此代码在 Delphi 7 中运行良好,但在 Delphi XE 中不起作用。它总是返回“错误”。如何使此代码在Delphi XE中工作?