以下是要使用的數據結構
//用到的協議數據結構
PIPOptionInfo = ^TIPOptionInfo; // IP 頭選項
TIPOptionInfo = packed record
TTL: Byte;//存活時間
TOS: Byte;//Type of Service
Flags: Byte;//標志
OptionsSize: Byte;//選項長度
OptionsData: PChar;//選項數據
end;
PIcmpEchoReply = ^TIcmpEchoReply;
TIcmpEchoReply = packed record // ICMP 返回信息
Address: DWORD;//IP地址
Status: DWORD;//狀態
RTT: DWORD;
DataSize: Word;//數據長度
Reserved: Word;//保留
Data: Pointer;//數據
Options: TIPOptionInfo;//選項區
end;
//動態庫中的函數聲明
TIcmpCreateFile = function: THandle; stdcall; //創建ICMP句柄
TIcmpCloseHandle = function(IcmpHandle: THandle): Boolean; stdcall; //關閉ICMP句柄
TIcmpSendEcho = function(IcmpHandle:THandle; DestinationAddress:DWORD;
RequestData:Pointer; RequestSize:Word; RequestOptions:PIPOptionInfo;
ReplyBuffer:Pointer; ReplySize:DWord; Timeout:DWord):DWord; stdcall;//發送ICMP探測數據報
//要用到的變量聲明
hICMPDll
wsaData:TWSADATA;
ICMPCreateFile:TICMPCreateFile;
IcmpCloseHandle:TIcmpCloseHandle;
IcmpSendEcho:TIcmpSendEcho;
//destip:要探測的遠程地址
procedure f_CheckOnline(destip:string);
var
IPOpt:TIPOptionInfo;// 發包的 IP 選項
IPAddr:DWORD;
pReqData
pIPE:PIcmpEchoReply;// ICMP Echo 回復緩沖區
FSize: DWORD;
MyString:string;
FTimeOut:DWORD;
BufferSize:DWORD;
i:integer;
begin
hICMPdll := LoadLibrary(
if hICMPDll<>NULL then
begin
WSAStartup($
@ICMPCreateFile := GetProcAddress(hICMPdll
@IcmpCloseHandle := GetProcAddress(hICMPdll
@IcmpSendEcho := GetProcAddress(hICMPdll
hICMP := IcmpCreateFile; //創建 icmp句柄
IPAddr:= inet_addr(PChar(destip)); //取要探測的遠端主機ip地址
FSize :=
BufferSize := SizeOf(TICMPEchoReply) + FSize;
GetMem(pRevData
GetMem(pIPE
FillChar(pIPE^
pIPE^
MyString :=
pReqData := PChar(MyString);
FillChar(IPOpt
IPOpt
FTimeOut :=
i:=IcmpSendEcho(hICMP
FreeMem(pRevData);
FreeMem(pIPE);
IcmpCloseHandle(hicmp);
FreeLibrary(hICMPdll);//釋放動態庫
WSAcleanup();//清理協議棧
end;
end;
From:http://tw.wingwit.com/Article/program/Delphi/201311/24701.html