| 1 | unit UPing;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Windows, SysUtils, Classes;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 | TSunB = packed record
|
|---|
| 10 | s_b1, s_b2, s_b3, s_b4: byte;
|
|---|
| 11 | end;
|
|---|
| 12 |
|
|---|
| 13 | TSunW = packed record
|
|---|
| 14 | s_w1, s_w2: word;
|
|---|
| 15 | end;
|
|---|
| 16 |
|
|---|
| 17 | PIPAddr = ^TIPAddr;
|
|---|
| 18 | TIPAddr = record
|
|---|
| 19 | case integer of
|
|---|
| 20 | 0: (S_un_b: TSunB);
|
|---|
| 21 | 1: (S_un_w: TSunW);
|
|---|
| 22 | 2: (S_addr: longword);
|
|---|
| 23 | end;
|
|---|
| 24 |
|
|---|
| 25 | IPAddr = TIPAddr;
|
|---|
| 26 |
|
|---|
| 27 | function IcmpCreateFile : THandle; stdcall; external 'icmp.dll';
|
|---|
| 28 | function IcmpCloseHandle (icmpHandle : THandle) : boolean; stdcall; external 'icmp.dll'
|
|---|
| 29 | function IcmpSendEcho (IcmpHandle : THandle; DestinationAddress : IPAddr;
|
|---|
| 30 | RequestData : Pointer; RequestSize : Smallint;
|
|---|
| 31 | RequestOptions : pointer;
|
|---|
| 32 | ReplyBuffer : Pointer;
|
|---|
| 33 | ReplySize : DWORD;
|
|---|
| 34 | Timeout : DWORD) : DWORD; stdcall; external 'icmp.dll';
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | function Ping(InetAddress : string; TimeOut: Integer = 200) : boolean;
|
|---|
| 38 |
|
|---|
| 39 | implementation
|
|---|
| 40 |
|
|---|
| 41 | uses
|
|---|
| 42 | WinSock;
|
|---|
| 43 |
|
|---|
| 44 | function Fetch(var AInput: string; const ADelim: string = ' '; const ADelete: Boolean = true)
|
|---|
| 45 | : string;
|
|---|
| 46 | var
|
|---|
| 47 | iPos: Integer;
|
|---|
| 48 | begin
|
|---|
| 49 | if ADelim = #0 then begin
|
|---|
| 50 | // AnsiPos does not work with #0
|
|---|
| 51 | iPos := Pos(ADelim, AInput);
|
|---|
| 52 | end else begin
|
|---|
| 53 | iPos := Pos(ADelim, AInput);
|
|---|
| 54 | end;
|
|---|
| 55 | if iPos = 0 then begin
|
|---|
| 56 | Result := AInput;
|
|---|
| 57 | if ADelete then begin
|
|---|
| 58 | AInput := '';
|
|---|
| 59 | end;
|
|---|
| 60 | end else begin
|
|---|
| 61 | result := Copy(AInput, 1, iPos - 1);
|
|---|
| 62 | if ADelete then begin
|
|---|
| 63 | Delete(AInput, 1, iPos + Length(ADelim) - 1);
|
|---|
| 64 | end;
|
|---|
| 65 | end;
|
|---|
| 66 | end;
|
|---|
| 67 |
|
|---|
| 68 | procedure TranslateStringToTInAddr(AIP: string; var AInAddr);
|
|---|
| 69 | var
|
|---|
| 70 | phe: PHostEnt;
|
|---|
| 71 | pac: PChar;
|
|---|
| 72 | GInitData: TWSAData;
|
|---|
| 73 | begin
|
|---|
| 74 | WSAStartup($101, GInitData);
|
|---|
| 75 | try
|
|---|
| 76 | phe := GetHostByName(PChar(AIP));
|
|---|
| 77 | if Assigned(phe) then
|
|---|
| 78 | begin
|
|---|
| 79 | pac := phe^.h_addr_list^;
|
|---|
| 80 | if Assigned(pac) then
|
|---|
| 81 | begin
|
|---|
| 82 | with TIPAddr(AInAddr).S_un_b do begin
|
|---|
| 83 | s_b1 := Byte(pac[0]);
|
|---|
| 84 | s_b2 := Byte(pac[1]);
|
|---|
| 85 | s_b3 := Byte(pac[2]);
|
|---|
| 86 | s_b4 := Byte(pac[3]);
|
|---|
| 87 | end;
|
|---|
| 88 | end
|
|---|
| 89 | else
|
|---|
| 90 | begin
|
|---|
| 91 | raise Exception.Create('Error getting IP from HostName');
|
|---|
| 92 | end;
|
|---|
| 93 | end
|
|---|
| 94 | else
|
|---|
| 95 | begin
|
|---|
| 96 | raise Exception.Create('Error getting HostName');
|
|---|
| 97 | end;
|
|---|
| 98 | except
|
|---|
| 99 | FillChar(AInAddr, SizeOf(AInAddr), #0);
|
|---|
| 100 | end;
|
|---|
| 101 | WSACleanup;
|
|---|
| 102 | end;
|
|---|
| 103 |
|
|---|
| 104 | function Ping(InetAddress : string; TimeOut: Integer = 200) : boolean;
|
|---|
| 105 | var
|
|---|
| 106 | Handle : THandle;
|
|---|
| 107 | InAddr : IPAddr;
|
|---|
| 108 | DW : DWORD;
|
|---|
| 109 | rep : array[1..128] of byte;
|
|---|
| 110 | begin
|
|---|
| 111 | result := false;
|
|---|
| 112 | Handle := IcmpCreateFile;
|
|---|
| 113 | if Handle = INVALID_HANDLE_VALUE then
|
|---|
| 114 | Exit;
|
|---|
| 115 | TranslateStringToTInAddr(InetAddress, InAddr);
|
|---|
| 116 | DW := IcmpSendEcho(Handle, InAddr, nil, 0, nil, @rep, 128, Timeout);
|
|---|
| 117 | Result := (DW <> 0);
|
|---|
| 118 | IcmpCloseHandle(Handle);
|
|---|
| 119 | end;
|
|---|
| 120 |
|
|---|
| 121 | end.
|
|---|