unit Mains;
interface
uses
Windows, Forms, winsock, StdCtrls, Classes,
Sysutils,Controls, ComCtrls, Buttons, Dialogs;
type
PIPOptionInformation= ^TIPOptionInformation;
TIPOptionInformation=packed record
TTL : Byte;
Tos : byte;
Flags : byte;
OptionsSize : Byte;
OptionsData : PChar;
end;
PIcmpEchoReply = ^TIcmpEchoReply;
TIcmpEchoReply=packed record
Address
Word;
Status
Word;
RTT : DWord;
DataSize:word;
Reserved:word;
Data
ointer;
Options : TIPOptionInformation;
end;
TIcmpCreateFile = function:THandle;stdcall;
TIcmpCloseHandle = function(IcmpHandle:THandle):Boolean;stdcall;
TIcmpSendEcho=function(Icmphandle:THandle;destinationAddress:dword;
RequestData
ointer; RequestSize:word;
RequestOptions
IPOptionInformation;
ReplyBuffer
ointer;ReplySize
word; TimeOut
word)
Word;stdcall;
TMainScreen = class(TForm)
StatusShow: TStatusBar;
BitBtn1: TBitBtn;
PingEdit: TEdit;
Label1: TLabel;
StatusText: TMemo;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
HIcmp : THandle;
IcmpCreateFile : TIcmpCreateFile;
IcmpCloseHandle: TIcmpCloseHandle;
ICmpSendEcho : TIcmpSendEcho;
hIcmpDll : HModule; // Load the icmp.dll stuff
public
end;
var
MainScreen : TMainScreen;
implementation
{$R *.dfm}
procedure TMainScreen.FormCreate(Sender: TObject);
var
WSAData : TWSAData;
begin
hICMPdll := LoadLibrary('Icmp.dll');
@ICMPCreateFile := GetProcAddress(hICMPdll, 'IcmpCreateFile');
@IcmpCloseHandle := GetProcAddress(hICMPdll, 'IcmpCloseHandle');
IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');
hICMP := IcmpCreateFile;
StatusShow.Panels[1].Text := '';
StatusShow.Panels[1].Text := '目的IP地址 字节数 返回时间(毫秒)';
//接下来,就要进行如下所示的Ping操作的实际编程过程了。
StatusText.Lines.Clear;
end;
procedure TMainScreen.BitBtn1Click(Sender: TObject);
var
IPOpt:TIPOptionInformation;// IP Options for packet to send
FIPAddress
WORD;
pReqData,pRevData
Char;
pIPE
IcmpEchoReply;// ICMP Echo reply buffer
FSize: DWORD;
MyString:string;
FTimeOut
WORD;
BufferSize
WORD;
i : Integer;
begin
StatusText.Text := '';
if PingEdit.Text<>'' then
begin
try
FIPAddress := inet_addr(PChar(PingEdit.Text));
FSize := 200;
BufferSize := SizeOf(TICMPEchoReply) + FSize;
GetMem(pRevData,FSize);
GetMem(pIPE,BufferSize);
FillChar(pIPE^, SizeOf(pIPE^), 0);
pIPE^.Data := pRevData;
MyString := 'Hello,World,dddddddddddddddddddd'
+ 'dddddddddddddddddddddddddddddddd';
pReqData := PChar(MyString);
FillChar(IPOpt, Sizeof(IPOpt), 0);
IPOpt.TTL := 64;
FTimeOut := 4000;
try
for I := 0 to 4 do
begin
IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString),
@IPOpt, pIPE, BufferSize, FTimeOut);
if pReqData^ = pIPE^.Options.OptionsData^ then
begin
StatusText.Lines.Add(
'Replay from ' + PChar(PingEdit.Text) + ' : bytes=' +
IntToStr(pIPE^.DataSize) + ' timeout=' + Trim(Format('%8.0f',[FTimeOut/1000])) +
'ms RTT='+IntToStr(pIPE^.RTT));
end;
end;
except
MessageDlg('Host is not existing.', mtConfirmation,[mbok],0);
end;
finally
FreeMem(pRevData);
FreeMem(pIPE);
end;
end;
end;
procedure TMainScreen.FormDestroy(Sender: TObject);
begin
FreeLibrary(hIcmpDll);
end;
end.