Ping source program of Delphi

Source: Internet
Author: User

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]: = 'Return address ';

Echogrid. cells []: = 'Return packet size ';

Echogrid. cells [2, 0]: = 'status ';

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, 'the address is invalid.

', '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, 'destination cannot reach ', 'ping32', 64)

End;

Finally

Freemem (prevdata );

Freemem (PIPE );

End;

End;

End;

End;

Procedure tfrmping. formdestroy (Sender: tobject );

Begin

Icmpclosehandle (hicmp );

Freelibrary (hicmpdll );

End;

End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.