function Fetch(
var AInput:
string;
const ADelim:
string = '
';
const ADelete: Boolean = true):
string;
var
iPos: Integer;
begin
if ADelim = #0
then begin
// AnsiPos does not work with #0
iPos := Pos(ADelim, AInput);
end else begin
iPos := Pos(ADelim, AInput);
end;
if iPos = 0
then begin
Result := AInput;
if ADelete
then begin
AInput := '
';
end;
end else begin
result := Copy(AInput, 1, iPos - 1);
if ADelete
then begin
Delete(AInput, 1, iPos + Length(ADelim) - 1);
end;
end;
end;
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;
function Ping(InetAddress :
string) : boolean;
var
Handle : THandle;
InAddr : IPAddr;
DW : DWORD;
PingBuf:
array[0..31]
of char;
Reply : ICMP_ECHO_REPLY;
begin
result := false;
Handle := IcmpCreateFile;
if Handle = INVALID_HANDLE_VALUE
then
Exit;
TranslateStringToTInAddr(InetAddress, InAddr);
Reply.Data := @pingBuf;
Reply.DataSize := 32;
DW := IcmpSendEcho(
Handle, InAddr, @PingBuf, 32,
nil, @reply, SizeOf(icmp_echo_reply) + 32 , 1000);
//Die 1000 gibt den TimeOut an
if DW <> 0
then
Result:= true;
IcmpCloseHandle(
Handle);
end;