source: branches/2/UPing.pas

Last change on this file was 1, checked in by george, 17 years ago

Přidáno: Verze 2.10 do vývojové větve 2.

  • Property svn:executable set to *
File size: 2.7 KB
Line 
1unit UPing;
2
3interface
4
5uses
6 Windows, SysUtils, Classes;
7
8type
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
27function IcmpCreateFile : THandle; stdcall; external 'icmp.dll';
28function IcmpCloseHandle (icmpHandle : THandle) : boolean; stdcall; external 'icmp.dll'
29function 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
37function Ping(InetAddress : string; TimeOut: Integer = 200) : boolean;
38
39implementation
40
41uses
42 WinSock;
43
44function Fetch(var AInput: string; const ADelim: string = ' '; const ADelete: Boolean = true)
45 : string;
46var
47 iPos: Integer;
48begin
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;
66end;
67
68procedure TranslateStringToTInAddr(AIP: string; var AInAddr);
69var
70 phe: PHostEnt;
71 pac: PChar;
72 GInitData: TWSAData;
73begin
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;
102end;
103
104function Ping(InetAddress : string; TimeOut: Integer = 200) : boolean;
105var
106 Handle : THandle;
107 InAddr : IPAddr;
108 DW : DWORD;
109 rep : array[1..128] of byte;
110begin
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);
119end;
120
121end.
Note: See TracBrowser for help on using the repository browser.