unit
Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, WinSock, StdCtrls;
type
TForm1 =
class
(TForm)
Button1: TButton;
Memo1: TMemo;
procedure
Button1Click(Sender: TObject);
private
public
end
;
var
Form1: TForm1;
type
ip_option_information =
packed
record
Ttl :
byte
;
Tos :
byte
;
Flags :
byte
;
OptionsSize :
byte
;
OptionsData :
Pointer
;
end
;
icmp_echo_reply =
packed
record
Address : u_long;
Status : u_long;
RTTime : u_long;
DataSize : u_short;
Reserved : u_short;
Data :
Pointer
;
Options : ip_option_information;
end
;
PIPINFO = ^ip_option_information;
PVOID =
Pointer
;
function
IcmpCreateFile() : THandle; stdcall; external
'ICMP.DLL'
name
'IcmpCreateFile'
;
function
IcmpCloseHandle(IcmpHandle : THandle) : BOOL; stdcall; external
'ICMP.DLL'
name
'IcmpCloseHandle'
;
function
IcmpSendEcho(
IcmpHandle : THandle;
DestAddress : u_long;
RequestData : PVOID;
RequestSize :
Word
;
RequestOptns : PIPINFO;
ReplyBuffer : PVOID;
ReplySize : DWORD;
Timeout : DWORD
) : DWORD; stdcall; external
'ICMP.DLL'
name
'IcmpSendEcho'
;
implementation
{$R *.dfm}
procedure
TForm1
.
Button1Click(Sender: TObject);
var
hIP : THandle;
pingBuffer :
array
[
0..31
]
of
Char
;
pIpe : ^icmp_echo_reply;
pHostEn : PHostEnt;
wVersionRequested :
WORD
;
lwsaData : WSAData;
error : DWORD;
destAddress : In_Addr;
begin
hIP := IcmpCreateFile();
GetMem( pIpe,
sizeof(icmp_echo_reply) + sizeof(pingBuffer));
pIpe
.
Data := @pingBuffer;
pIpe
.
DataSize := sizeof(pingBuffer);
wVersionRequested := MakeWord(
1
,
1
);
error := WSAStartup(wVersionRequested,lwsaData);
if
(error <>
0
)
then
begin
Memo1
.
SetTextBuf(
'Error in call to '
+
'WSAStartup().'
);
Memo1
.
Lines
.
Add(
'Error code: '
+IntToStr(error));
Exit;
end
;
pHostEn := gethostbyname(
'ya.ru'
);
error := GetLastError();
if
(error <>
0
)
then
begin
Memo1
.
SetTextBuf(
'Error in call to'
+
'gethostbyname().'
);
Memo1
.
Lines
.
Add(
'Error code: '
+IntToStr(error));
Exit;
end
;
destAddress := PInAddr(pHostEn^.h_addr_list^)^;
Memo1
.
Lines
.
Add(
'Pinging '
+
pHostEn^.h_name+
' ['
+
inet_ntoa(destAddress)+
'] '
+
' with '
+
IntToStr(sizeof(pingBuffer)) +
' bytes of data:'
);
IcmpSendEcho(hIP,
destAddress
.
S_addr,
@pingBuffer,
sizeof(pingBuffer),
Nil
,
pIpe,
sizeof(icmp_echo_reply) + sizeof(pingBuffer),
5000
);
error := GetLastError();
if
(error <>
0
)
then
begin
Memo1
.
SetTextBuf(
'Error in call to '
+
'IcmpSendEcho()'
);
Memo1
.
Lines
.
Add(
'Error code: '
+IntToStr(error));
Exit;
end
;
Memo1
.
Lines
.
Add(
'Reply from '
+
IntToStr(LoByte(LoWord(pIpe^.Address)))+
'.'
+
IntToStr(HiByte(LoWord(pIpe^.Address)))+
'.'
+
IntToStr(LoByte(HiWord(pIpe^.Address)))+
'.'
+
IntToStr(HiByte(HiWord(pIpe^.Address))));
Memo1
.
Lines
.
Add(
'Reply time: '
+IntToStr(pIpe
.
RTTime)+
' ms'
);
IcmpCloseHandle(hIP);
WSACleanup();
FreeMem(pIpe);
end
;
end
.