【发布时间】:2013-09-12 10:03:15
【问题描述】:
我正在使用 delphi-xe3 中的 windows API 从这里实现 Ping 功能 (http://delphi.about.com/od/internetintranet/l/aa081503a.htm)。
我在使用以下函数时遇到问题。它显示错误类型 Pansichar 和 Pwidechar 不兼容。我将 Pchar 替换为 PAnsichar 现在它显示异常 '从主机名获取 IP 时出错'。
我正在用 localhost 测试它。
请指导什么是正确的转换。
const ADP_IP = '127.0.0.1';
procedure TranslateStringToTInAddr(AIP: string; var AInAddr);
var
phe: PHostEnt;
pac: PChar;
GInitData: TWSAData;
begin
WSAStartup($101, GInitData);
try
phe := GetHostByName(PChar(AIP));
if Assigned(phe) then
begin
pac := phe^.h_addr_list^;
if Assigned(pac) then
begin
with TIPAddr(AInAddr).S_un_b do begin
s_b1 := Byte(pac[0]);
s_b2 := Byte(pac[1]);
s_b3 := Byte(pac[2]);
s_b4 := Byte(pac[3]);
end;
end
else
begin
raise Exception.Create('Error getting IP from HostName');
end;
end
else
begin
raise Exception.Create('Error getting HostName');
end;
except
FillChar(AInAddr, SizeOf(AInAddr), #0);
end;
WSACleanup;
end;
【问题讨论】:
标签: delphi delphi-xe3 indy10