博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delphi的ping源程序
阅读量:6183 次
发布时间:2019-06-21

本文共 3057 字,大约阅读时间需要 10 分钟。

unit ping;

 

interface

 

uses

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,

Dialogs,winsock,

  StdCtrls, Grids;

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;RequestDa

ta: Pointer;RequestSize: Word;RequestOptions: PIPOptionInformation;ReplyBuffer

: Pointer;ReplySize: DWord;Timeout: DWord): DWord; stdcall;

type

Tfrmping = class(TForm)

    echogrid: TStringGrid;

    ipaddr: TEdit;

    Label1: TLabel;

    ping: TButton;

procedure FormCreate(Sender: TObject);

    procedure pingClick(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

private

hICMP: THANDLE;

IcmpCreateFile : TIcmpCreateFile;

IcmpCloseHandle:TIcmpCloseHandle;

IcmpSendEcho: TIcmpSendEcho;

line:integer;

{ Private declarations }

public

hICMPdll: HMODULE;{ Public declarations }

end;

 

var

  frmping: Tfrmping;

 

implementation

 

{$R *.DFM}

 

procedure Tfrmping.FormCreate(Sender: TObject);

begin

hICMPdll := LoadLibrary('icmp.dll');

@ICMPCreateFile:= GetProcAddress(hICMPdll, 'IcmpCreateFile');

@IcmpCloseHandle := GetProcAddress(hICMPdll, 'IcmpCloseHandle');

@IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');

hICMP := IcmpCreateFile;

echogrid.Cells[0,0]:='返回地址';

echogrid.cells[1,0]:='返回数据包大小';

echogrid.Cells[2,0]:='状态';

echogrid.Cells[3,0]:='RTT(Round-Trip-Time)';

line:=1;

end;

 

procedure Tfrmping.pingClick(Sender: TObject);

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;

begin

if iPaddr.Text <> '' then

begin

FIPAddress:=inet_addr(PChar(ipaddr.Text));

if Fipaddress=INADDR_NONE then Messagebox(self.handle,'地址无效

','Ping32',64)

else

begin

FSize:=40;

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 :=10000;

IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString),@IPOpt, pIPE,

BufferSize, FTimeOut);

try

try

if pReqData^ = pIPE^.Options.OptionsData^ then

with echogrid do

begin

if line>1 then rowcount:=line+1;

cells[0,line]:=ipaddr.Text;

cells[1,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 Tfrmping.FormDestroy(Sender: TObject);

begin

icmpclosehandle(hicmp);

freelibrary(hicmpdll);

end;

 

end.

转载地址:http://wvsda.baihongyu.com/

你可能感兴趣的文章
开启并配置Citrix Xenserver的SNMP服务
查看>>
华为网络设备上常用的安全技术(三)
查看>>
oracle函数
查看>>
Git教程及问题解析
查看>>
添加网络打印机
查看>>
【MongoDB】4、MongoDB的两个小东东:GridFS和mapreduce
查看>>
用图形工具管理Server Core上的账号和组图文教程
查看>>
抽象类
查看>>
《Java程序员面试宝典》学习笔记(设计模式部分)
查看>>
致我那痛苦的肚子
查看>>
[转载] 故宫第一集 肇建紫禁城(下)
查看>>
HTTP请求报文和HTTP响应报文【转载】
查看>>
Android启动画面实现
查看>>
memcached linux安装并启动memcached
查看>>
redis状态与性能监控
查看>>
Linux的Apache 服务
查看>>
Linux实用工具之GPG
查看>>
RabbitMQ学习总结(一)——基础概念详细介绍
查看>>
导入EXCEL时的日期转换
查看>>
Dubbo学习总结(3)——Dubbo-Admin管理平台和Zookeeper注册中心的搭建
查看>>