判断工作组中能否找到该计算机,能,开机,不能,没有开机unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;type PnetResourceArr=^TNetResource; TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; ListBox1: TListBox; Edit2: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } procedure GetUserList(fServer:string;List:TStrings); end;var Form1: TForm1;implementation{$R *.dfm} procedure TForm1.GetUserList(fServer: string; List: TStrings); VarNetResource : TNetResource;Buf : Pointer;Count,BufSize,Res : DWord;Ind : Integer;lphEnum : THandle;Temp : PNetResourceArr;Begin List.Clear;GetMem(Buf, 8192);TryFillChar(NetResource, SizeOf(NetResource), 0);NetResource.lpRemoteName := @fServer[1];NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;NetResource.dwScope := RESOURCETYPE_DISK;Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);If Res <> 0 Then Exit;While True DoBeginCount := $FFFFFFFF;BufSize := 8192;Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);If Res = ERROR_NO_MORE_ITEMS Then Exit;If (Res <> 0) then Exit;Temp := PNetResourceArr(Buf);For Ind := 0 to Count - 1 doBeginList.Add(Temp^.lpRemoteName + 2); { Add all the network usernames to List StringList }Inc(Temp);End;End;Res := WNetCloseEnum(lphEnum);If Res <> 0 Then Raise Exception(Res);// Result := True;FinallyFreeMem(Buf);End;End; procedure TForm1.Button1Click(Sender: TObject); begin form1.GetUserList(form1.Edit1.Text,form1.ListBox1.Items);//edit1.text用来指定计算机所在的工作组 if pos(form1.Edit2.Text,form1.ListBox1.Items.Text)>0 then//edit2.text用来指定要查看是否开机的计算机名 showmessage('已经开机') else showmessage('没有开机'); end;end.
用GetHostByName三句搞定 procedure TForm1.Button1Click(Sender: TObject); var s : PHostEnt ; begin s := GetHostByName('Dev01'); ShowMessage(s.h_name);end; 别忘了加WinSock单元
winexec('cmd /k ping tjf>c:\aa.txt',SW_HIDE);//tjf是机器名称 fileopen('c:\aa.txt',FILE_SHARE_READ or FILE_SHARE_WRITE); memo1.Lines.LoadFromFile('c:\aa.txt');即可。
看这方法怎么样!局域网判断对方IP //uses winsock function testip(IP:string):string; var WSAData:TWSADATA; Addr:DWORD; begin WSAStartup(2, WSAData); Addr:=inet_addr(PChar(IP)); if gethostbyaddr(@Addr,sizeof(Addr),PF_INET)=nil then result:=IP+'没连接网络' else result:=ip+'已经连接'; WSACleanup(); end;
如果对方用ipx/spx协议而不是TCP/IP呢?
给你一个简单的代码,是利用 command的net view命令来达到的: var host,temp:string; size:integer; f:file of byte; begin temp:='c:\test.txt'; if fileexists(temp) then deletefile(temp); host:=edit1.text; winexec(pchar('command.com /C net view \\'+host+' >'+temp),sw_hide); while not fileexists(temp) do sleep(2000); try AssignFile(f,temp); Reset(f); size := FileSize(f); finally closefile(f); end; if size=0 then showmessage(edit1.text+'目前没有上网!');
ping 就有一个返回值的,可以判断是否在线的。回去给你看看!
这是在d6下写的一个函数: function TfrmMain.ping(HostName: string): boolean; begin Result:= False; with IdIcmpClient1 do begin Host:=HostName; try Ping; IF IdIcmpClient1.ReplyStatus.ReplyStatusType<>rsTimeOut Then Result:= true; except end; end; end;
通过ping来实现,能ping到就是开机了 unit UnitPing;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, OutlookBtn, StdCtrls, TB97, Winsock; ////////////////////////////////////////////////////// // TPing Copyright (C) BaoMin 1999 // // Author's Email:[email protected] // // Copyright remains BaoMin, do not remove // // any Copyright notices. // ////////////////////////////////////////////////////// type DWORD=LongWord; THandle=LongWord; PIPOptionInformation = ^TIPOptionInformation; TIPOptionInformation = record TTL: Byte; TOS: Byte; Flags: Byte; OptionsSize: Byte; OptionsData: PChar; end;PIcmpEchoReply = ^TIcmpEchoReply; TIcmpEchoReply = record Address: DWORD; Status: DWORD; RTT: DWORD; DataSize:Word; Reserved: Word; Data: Pointer; Options: TIPOptionInformation; end;function IcmpCreateFile():THandle;stdcall external 'ICMP.dll'; function IcmpCloseHandle(Handle:THandle):Boolean;stdcall external 'ICMP.dll'; function IcmpSendEcho(Handle:THandle;DestAddr:DWORD; RequestData: Pointer;RequestSize: Word;RequestOptions: PIPOptionInformation; ReplyBuffer: Pointer;ReplySize: DWORD;Timeout: DWORD): DWORD;stdcall external 'ICMP.dll'; procedure ValidCheck(); procedure FreeWinsock(); function Ping(IPAddr:String;TimeOut:Word):String;Const { Exception Message } SInitFailed = 'Winsock version error'; SInvalidAddr = 'Invalid IP Address'; SNoResponse = 'No Response'; STimeOut = 'Request TimeOut';type TFormPing = class(TForm) EditAddr: TEdit97; Label1: TLabel; BtnPing: TOutlookBtn; Label2: TLabel; MemoResult: TMemo; procedure BtnPingClick(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;var FormPing: TFormPing; hICMP:THandle; implementation {$R *.DFM} procedure ValidCheck(); var WSAData:TWSAData; begin //initiates use of WS2_32.DLL if (WSAStartup(MAKEWORD(2,0),WSAData)<>0) then raise Exception.Create(SInitFailed); hIcmp:=IcmpCreateFile(); if hICMP=INVALID_HANDLE_VALUE then raise Exception.Create('Create ICMP Failed'); end; procedure FreeWinsock(); begin IcmpCloseHandle(hIcmp); WSACleanUP; end;function Ping(IPAddr:String;TimeOut:Word):String; var IPOpt:TIPOptionInformation;// IP Options for packet to send FIPAddress:DWORD; pReqData,pRevData:PChar; pIPE:PIcmpEchoReply;// ICMP Echo reply buffer FSize: DWORD; MyString:string; FTimeOut:DWORD; BufferSize:DWORD; temp:Integer; pIPAddr:Pchar; begin //get ip GetMem(pIPAddr,Length(IPAddr)+1); ZeroMemory(pIPAddr,Length(IPAddr)+1); StrPCopy(pIPAddr,IPAddr); //calc FIPAddress := inet_addr(pIPAddr); //free it FreeMem(pIPAddr); //valid check if FIPAddress=INADDR_NONE then begin result:=SInvalidAddr;//Exit exit; end; // WSAAsyncGetHostByAddr() //package size FSize := 40; BufferSize := SizeOf(TICMPEchoReply) + FSize; GetMem(pRevData,FSize); GetMem(pIPE,BufferSize); //prepare data FillChar(pIPE^, SizeOf(pIPE^), 0); pIPE^.Data := pRevData; MyString := 'Ping Digital Data'; pReqData := PChar(MyString); FillChar(IPOpt, Sizeof(IPOpt), 0); //max delieve geteway IPOpt.TTL := 64; //time out FTimeOut := TimeOut; //go!!! temp:=IcmpSendEcho(hICMP,//dll handle FIPAddress,//target pReqData,//data Length(MyString),//data length @IPOpt,//addree of ping option pIPE,// BufferSize,//pack size FTimeOut);//timeout value //check result if temp=0 then begin Result:='Ping Addr:'+IPAddr+' '+SNoResponse; exit; end; if pReqData^ = pIPE^.Options.OptionsData^ then begin //show result Result:=('Reply from:'+PChar(IPAddr) + ' ' +'bytes:'+IntToStr(pIPE^.DataSize) + ' ' +'tims:'+IntToStr(pIPE^.RTT)+ 'ms ' +'TTL:'+intToStr(pIPE^.Options.TTL)); end; //clear memory FreeMem(pRevData); FreeMem(pIPE); end;procedure TFormPing.BtnPingClick(Sender: TObject); var pingresult:string; begin //version check and init ValidCheck(); //update view pingresult:=Ping(EditAddr.Text,500); MemoResult.Lines.add(pingresult); //clear FreeWinsock(); end;procedure TFormPing.FormCreate(Sender: TObject); begin //update view MemoResult.Font.Color:=clHighlightText; MemoResult.Font.Name:='Terminal'; MemoResult.Font.Size:=10; MemoResult.Color:= clNone; end;end.
使用API实在太冗长麻烦,其实Dephi自带的indy client控件组的Idicmpclient控件封装了ping,包括了返回参数,非常容易搞定IdIcmpClient1.Host:='133.56.9.71' IdIcmpClient1.Ping; if IdIcmpClient1.ReplyStatus.FromIpAddress='133.56.9.71' then begin showmessage('133.56.9.71已经开机') end; 至少给个50分啊
it myping;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Winsock;type
DWORD=LongWord;
THandle=LongWord;
PIPOptionInformation = ^TIPOptionInformation;
TIPOptionInformation =
record
TTL: Byte;
TOS: Byte;
Flags: Byte;
OptionsSize: Byte;
OptionsData: PChar;
end;PIcmpEchoReply = ^TIcmpEchoReply;
TIcmpEchoReply =
record
Address: DWORD;
Status: DWORD;
RTT: DWORD;
DataSize:Word;
Reserved: Word;
Data: Pointer;
Options: TIPOptionInformation;
end;function IcmpCreateFile():THandle;stdcall external 'ICMP.dll';
function IcmpCloseHandle(Handle:THandle):Boolean;stdcall external 'ICMP.dll';
function IcmpSendEcho(Handle:THandle;DestAddr:DWORD;
RequestData: Pointer;RequestSize: Word;RequestOptions: PIPOptionInformation;
ReplyBuffer: Pointer;ReplySize: DWORD;Timeout: DWORD): DWORD;stdcall external 'ICMP.dll';
procedure ValidCheck();
procedure FreeWinsock();
function Ping(IPAddr:String;TimeOut:Word):String;Const
{ Exception Message }
SInitFailed = 'Winsock version error';
SInvalidAddr = 'Invalid IP Address';
SNoResponse = 'No Response';
STimeOut = 'Request TimeOut';type
TForm1 = class(TForm)
Button1: TButton;
MemoResult: TMemo;
Label1: TLabel;
Label2: TLabel;
Editaddr: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
hICMP:THandle;implementation{$R *.DFM}
procedure ValidCheck();
var
WSAData:TWSAData;
begin
//initiates use of WS2_32.DLL
if (WSAStartup(MAKEWORD(2,0),WSAData)<>0) then
raise Exception.Create(SInitFailed);
hIcmp:=IcmpCreateFile();
if hICMP=INVALID_HANDLE_VALUE then
raise Exception.Create('Create ICMP Failed');
end;
procedure FreeWinsock();
begin
IcmpCloseHandle(hIcmp);
WSACleanUP;
end;function Ping(IPAddr:String;TimeOut:Word):String;
var
IPOpt:TIPOptionInformation;// IP Options for packet to send
FIPAddress:DWORD;
pReqData,pRevData:PChar;
pIPE:PIcmpEchoReply;// ICMP Echo reply buffer
FSize: DWORD;
MyString:string;
FTimeOut:DWORD;
BufferSize:DWORD;
temp:Integer;
pIPAddr:Pchar;
begin
//get ip
GetMem(pIPAddr,Length(IPAddr)+1);
ZeroMemory(pIPAddr,Length(IPAddr)+1);
StrPCopy(pIPAddr,IPAddr);
//calc
FIPAddress := inet_addr(pIPAddr);
//free it
FreeMem(pIPAddr);
//valid check
if FIPAddress=INADDR_NONE then
begin
result:=SInvalidAddr;//Exit
exit;
end;
// WSAAsyncGetHostByAddr()
//package size
FSize := 40;
BufferSize := SizeOf(TICMPEchoReply) + FSize;
GetMem(pRevData,FSize);
GetMem(pIPE,BufferSize);
//prepare data
FillChar(pIPE^, SizeOf(pIPE^), 0);
pIPE^.Data := pRevData;
MyString := 'Ping Digital Data';
pReqData := PChar(MyString);
FillChar(IPOpt, Sizeof(IPOpt), 0);
//max delieve geteway
IPOpt.TTL := 64;
//time out
FTimeOut := TimeOut;
//go!!!
temp:=IcmpSendEcho(hICMP,//dll handle
FIPAddress,//target
pReqData,//data
Length(MyString),//data length
@IPOpt,//addree of ping option
pIPE,//
BufferSize,//pack size
FTimeOut);//timeout value
//check result
if temp=0 then
begin
Result:='Ping Addr:'+IPAddr+' '+SNoResponse;
exit;
end;
if pReqData^ = pIPE^.Options.OptionsData^ then
begin
//show result
Result:=('Reply from:'+PChar(IPAddr) + ' '
+'bytes:'+IntToStr(pIPE^.DataSize) + ' '
+'tims:'+IntToStr(pIPE^.RTT)+ 'ms '
+'TTL:'+intToStr(pIPE^.Options.TTL));
end;
//clear memory
FreeMem(pRevData);
FreeMem(pIPE);
end;
呵呵,麻烦了点。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,StdCtrls;type
PnetResourceArr=^TNetResource;
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
ListBox1: TListBox;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure GetUserList(fServer:string;List:TStrings);
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.GetUserList(fServer: string; List: TStrings);
VarNetResource : TNetResource;Buf : Pointer;Count,BufSize,Res : DWord;Ind : Integer;lphEnum : THandle;Temp : PNetResourceArr;Begin
List.Clear;GetMem(Buf, 8192);TryFillChar(NetResource, SizeOf(NetResource), 0);NetResource.lpRemoteName := @fServer[1];NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;NetResource.dwScope := RESOURCETYPE_DISK;Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);If Res <> 0 Then Exit;While True DoBeginCount := $FFFFFFFF;BufSize := 8192;Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);If Res = ERROR_NO_MORE_ITEMS Then Exit;If (Res <> 0) then Exit;Temp := PNetResourceArr(Buf);For Ind := 0 to Count - 1 doBeginList.Add(Temp^.lpRemoteName + 2); { Add all the network usernames to List StringList }Inc(Temp);End;End;Res := WNetCloseEnum(lphEnum);If Res <> 0 Then Raise Exception(Res);// Result := True;FinallyFreeMem(Buf);End;End;
procedure TForm1.Button1Click(Sender: TObject);
begin
form1.GetUserList(form1.Edit1.Text,form1.ListBox1.Items);//edit1.text用来指定计算机所在的工作组
if pos(form1.Edit2.Text,form1.ListBox1.Items.Text)>0 then//edit2.text用来指定要查看是否开机的计算机名
showmessage('已经开机')
else
showmessage('没有开机');
end;end.
procedure TForm1.Button1Click(Sender: TObject);
var s : PHostEnt ;
begin
s := GetHostByName('Dev01');
ShowMessage(s.h_name);end;
别忘了加WinSock单元
fileopen('c:\aa.txt',FILE_SHARE_READ or FILE_SHARE_WRITE);
memo1.Lines.LoadFromFile('c:\aa.txt');即可。
function testip(IP:string):string;
var
WSAData:TWSADATA;
Addr:DWORD;
begin
WSAStartup(2, WSAData);
Addr:=inet_addr(PChar(IP));
if gethostbyaddr(@Addr,sizeof(Addr),PF_INET)=nil then
result:=IP+'没连接网络'
else result:=ip+'已经连接';
WSACleanup();
end;
var
host,temp:string;
size:integer;
f:file of byte;
begin
temp:='c:\test.txt';
if fileexists(temp) then
deletefile(temp);
host:=edit1.text;
winexec(pchar('command.com /C net view \\'+host+' >'+temp),sw_hide);
while not fileexists(temp) do
sleep(2000);
try
AssignFile(f,temp);
Reset(f);
size := FileSize(f);
finally
closefile(f);
end;
if size=0 then
showmessage(edit1.text+'目前没有上网!');
function TfrmMain.ping(HostName: string): boolean;
begin
Result:= False;
with IdIcmpClient1 do
begin
Host:=HostName;
try
Ping;
IF IdIcmpClient1.ReplyStatus.ReplyStatusType<>rsTimeOut Then
Result:= true;
except
end;
end;
end;
unit UnitPing;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
OutlookBtn, StdCtrls, TB97, Winsock;
//////////////////////////////////////////////////////
// TPing Copyright (C) BaoMin 1999 //
// Author's Email:[email protected] //
// Copyright remains BaoMin, do not remove //
// any Copyright notices. //
//////////////////////////////////////////////////////
type
DWORD=LongWord;
THandle=LongWord;
PIPOptionInformation = ^TIPOptionInformation;
TIPOptionInformation =
record
TTL: Byte;
TOS: Byte;
Flags: Byte;
OptionsSize: Byte;
OptionsData: PChar;
end;PIcmpEchoReply = ^TIcmpEchoReply;
TIcmpEchoReply =
record
Address: DWORD;
Status: DWORD;
RTT: DWORD;
DataSize:Word;
Reserved: Word;
Data: Pointer;
Options: TIPOptionInformation;
end;function IcmpCreateFile():THandle;stdcall external 'ICMP.dll';
function IcmpCloseHandle(Handle:THandle):Boolean;stdcall external 'ICMP.dll';
function IcmpSendEcho(Handle:THandle;DestAddr:DWORD;
RequestData: Pointer;RequestSize: Word;RequestOptions: PIPOptionInformation;
ReplyBuffer: Pointer;ReplySize: DWORD;Timeout: DWORD): DWORD;stdcall external 'ICMP.dll';
procedure ValidCheck();
procedure FreeWinsock();
function Ping(IPAddr:String;TimeOut:Word):String;Const
{ Exception Message }
SInitFailed = 'Winsock version error';
SInvalidAddr = 'Invalid IP Address';
SNoResponse = 'No Response';
STimeOut = 'Request TimeOut';type
TFormPing = class(TForm)
EditAddr: TEdit97;
Label1: TLabel;
BtnPing: TOutlookBtn;
Label2: TLabel;
MemoResult: TMemo;
procedure BtnPingClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
FormPing: TFormPing;
hICMP:THandle;
implementation
{$R *.DFM}
procedure ValidCheck();
var
WSAData:TWSAData;
begin
//initiates use of WS2_32.DLL
if (WSAStartup(MAKEWORD(2,0),WSAData)<>0) then
raise Exception.Create(SInitFailed);
hIcmp:=IcmpCreateFile();
if hICMP=INVALID_HANDLE_VALUE then
raise Exception.Create('Create ICMP Failed');
end;
procedure FreeWinsock();
begin
IcmpCloseHandle(hIcmp);
WSACleanUP;
end;function Ping(IPAddr:String;TimeOut:Word):String;
var
IPOpt:TIPOptionInformation;// IP Options for packet to send
FIPAddress:DWORD;
pReqData,pRevData:PChar;
pIPE:PIcmpEchoReply;// ICMP Echo reply buffer
FSize: DWORD;
MyString:string;
FTimeOut:DWORD;
BufferSize:DWORD;
temp:Integer;
pIPAddr:Pchar;
begin
//get ip
GetMem(pIPAddr,Length(IPAddr)+1);
ZeroMemory(pIPAddr,Length(IPAddr)+1);
StrPCopy(pIPAddr,IPAddr);
//calc
FIPAddress := inet_addr(pIPAddr);
//free it
FreeMem(pIPAddr);
//valid check
if FIPAddress=INADDR_NONE then
begin
result:=SInvalidAddr;//Exit
exit;
end;
// WSAAsyncGetHostByAddr()
//package size
FSize := 40;
BufferSize := SizeOf(TICMPEchoReply) + FSize;
GetMem(pRevData,FSize);
GetMem(pIPE,BufferSize);
//prepare data
FillChar(pIPE^, SizeOf(pIPE^), 0);
pIPE^.Data := pRevData;
MyString := 'Ping Digital Data';
pReqData := PChar(MyString);
FillChar(IPOpt, Sizeof(IPOpt), 0);
//max delieve geteway
IPOpt.TTL := 64;
//time out
FTimeOut := TimeOut;
//go!!!
temp:=IcmpSendEcho(hICMP,//dll handle
FIPAddress,//target
pReqData,//data
Length(MyString),//data length
@IPOpt,//addree of ping option
pIPE,//
BufferSize,//pack size
FTimeOut);//timeout value
//check result
if temp=0 then
begin
Result:='Ping Addr:'+IPAddr+' '+SNoResponse;
exit;
end;
if pReqData^ = pIPE^.Options.OptionsData^ then
begin
//show result
Result:=('Reply from:'+PChar(IPAddr) + ' '
+'bytes:'+IntToStr(pIPE^.DataSize) + ' '
+'tims:'+IntToStr(pIPE^.RTT)+ 'ms '
+'TTL:'+intToStr(pIPE^.Options.TTL));
end;
//clear memory
FreeMem(pRevData);
FreeMem(pIPE);
end;procedure TFormPing.BtnPingClick(Sender: TObject);
var
pingresult:string;
begin
//version check and init
ValidCheck();
//update view
pingresult:=Ping(EditAddr.Text,500);
MemoResult.Lines.add(pingresult);
//clear
FreeWinsock();
end;procedure TFormPing.FormCreate(Sender: TObject);
begin
//update view
MemoResult.Font.Color:=clHighlightText;
MemoResult.Font.Name:='Terminal';
MemoResult.Font.Size:=10;
MemoResult.Color:= clNone;
end;end.
unit ping;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, winsock, StdCtrls;type
PIPOptionInformation = ^TIPOptionInformation;
TIPOptionInformation = packed record
TTL: Byte;
TOS: Byte;
Flags: Byte;
OptionsSize: Byte;
OptionsData: PChar;
end;
type PIcmpEchoReply = ^TIcmpEchoReply;
TIcmpEchoReply = packed record
Address: DWORD;
Status: DWORD;
RTT: DWORD;
DataSize:Word;
Reserved: Word;
Data: Pointer;
Options: TIPOptionInformation;
end;
TIcmpCreateFile = function: THandle; stdcall;
TIcmpCloseHandle = function(IcmpHandle: THandle): Boolean; stdcall;
TIcmpSendEcho = function(IcmpHandle:THandle;
DestinationAddress:DWORD;
RequestData: Pointer;
RequestSize: Word;
RequestOptions: PIPOptionInformation;
ReplyBuffer: Pointer;
ReplySize: DWord;
Timeout: DWord): DWord; stdcall;type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Edit1: TEdit;
Button1: TButton;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
hICMP: THANDLE;
IcmpCreateFile : TIcmpCreateFile;
IcmpCloseHandle:TIcmpCloseHandle;
IcmpSendEcho: TIcmpSendEcho;
line:integer; public
{ Public declarations }
hICMPdll: HMODULE;
end;var
Form1: TForm1;
implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);
begin
hICMPdll := LoadLibrary('icmp.dll');
@ICMPCreateFile:= GetProcAddress(hICMPdll, 'IcmpCreateFile');
@IcmpCloseHandle := GetProcAddress(hICMPdll, 'IcmpCloseHandle');
@IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');
hICMP := IcmpCreateFile;
StringGrid1.Cells[0,0]:=' ';
StringGrid1.Cells[1,0]:='·µ»ØµØÖ·';
StringGrid1.cells[2,0]:='·µ»ØÊý¾Ý°ü´óС';
StringGrid1.Cells[3,0]:='RTT(Round-Trip-Time)';
line:=1;
end;procedure TForm1.Button1Click(Sender: TObject);
var
IPOpt:TIPOptionInformation;
FIPAddress:DWORD;
pReqData,pRevData:PChar;
pIPE:PIcmpEchoReply;
FSize: DWORD;
MyString:string;
FTimeOut:DWORD;
BufferSize:DWORD;
begin
if Edit1.Text <> '' then
begin
FIPAddress:=inet_addr(PChar(Edit1.Text));
if Fipaddress=INADDR_NONE then
Messagebox(self.handle,'µØÖ·ÎÞЧ','Ping32',64)
else
begin
FSize:=80;
BufferSize:=SizeOf(TICMPEchoReply)+FSize;
GetMem(pRevData,FSize);
GetMem(pIPE,BufferSize);
FillChar(pIPE^, SizeOf(pIPE^), 0);
pIPE^.Data := pRevData;
MyString := 'Argen Ping32 Sending Message.';
pReqData := PChar(MyString);
FillChar(IPOpt, Sizeof(IPOpt), 0);
IPOpt.TTL:= 64;
FTimeOut :=500;
IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString),@IPOpt, pIPE,
BufferSize, FTimeOut);
try
try
if pReqData^ = pIPE^.Options.OptionsData^ then
with StringGrid1 do
begin
if line>1 then rowcount:=line+1;
cells[0,line]:=inttoStr(line);
cells[1,line]:=Edit1.Text;
cells[2,line]:=inttoStr(pIPE^.DataSize);
cells[3,line]:=IntToStr(pIPE^.RTT);
row:=rowcount-1;
line:=line+1;
end;
except
Messagebox(self.handle,'Ä¿±ê²»¿Éµ½','Ping32',64)
end;
finally
FreeMem(pRevData);
FreeMem(pIPE);
end;
end;
end;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
icmpclosehandle(hicmp);
freelibrary(hicmpdll);
end;end.
IdIcmpClient1.Ping;
if IdIcmpClient1.ReplyStatus.FromIpAddress='133.56.9.71' then
begin
showmessage('133.56.9.71已经开机')
end;
至少给个50分啊