Add Ping without raw sockets


Windows supports an Internet Control Message Protocol (ICMP) to determine whether or not a particular host is available. ICMP is a network layer protocol that delivers flow control, error messages, routing, and other data between Internet hosts. ICMP is primarily used by application developers for a network ping.

A ping is the process of sending an echo message to an IP address and reading the reply to verify a connection between TCP/IP hosts. If you are writing new application will be better to use the Winsock 2 raw sockets support, implemented in Indy, for example. Please note, however, that for Windows NT and Windows 2000 implementations, Raw Sockets are subject to security checks and are accessible only to members of the administrator's group. Icmp.dll provides functionality that allows developers to write Internet ping applications on Windows systems without Winsock 2 support.

Note that the Winsock 1.1 WSAStartup function must be called prior to using the functions exposed by ICMP.DLL. If you do not do this, the first call to IcmpSendEcho will fail with error 10091 (WSASYSNOTREADY).

  1. unit Ping;
  2.  
  3. interface
  4. uses
  5.   Windows, SysUtils, Classes;
  6.  
  7. type
  8.   TSunB = packed record
  9.     s_b1, s_b2, s_b3, s_b4: byte;
  10.   end;
  11.  
  12.   TSunW = packed record
  13.     s_w1, s_w2: word;
  14.   end;
  15.  
  16.   PIPAddr = ^TIPAddr;
  17.   TIPAddr = record
  18.     case integer of
  19.       0: (S_un_b: TSunB);
  20.       1: (S_un_w: TSunW);
  21.       2: (S_addr: longword);
  22.   end;
  23.  
  24.  IPAddr = TIPAddr;
  25.  
  26. function IcmpCreateFile : THandle; stdcall; external 'icmp.dll';
  27. function IcmpCloseHandle (icmpHandle : THandle) : boolean; stdcall;
  28. external 'icmp.dll'
  29. function IcmpSendEcho (IcmpHandle : THandle; DestinationAddress : IPAddr;
  30.     RequestData : Pointer; RequestSize : Smallint;
  31.     RequestOptions : pointer;
  32.     ReplyBuffer : Pointer;
  33.     ReplySize : DWORD;
  34.     Timeout : DWORD) : DWORD; stdcall; external 'icmp.dll';
  35.  
  36.  
  37. function Ping(InetAddress : string) : boolean;
  38.  
  39. implementation
  40.  
  41. uses
  42.   WinSock;
  43.  
  44. function Fetch(var AInput: string;
  45. const ADelim: string = ' ';
  46. const ADelete: Boolean = true)
  47.  : string;
  48. var
  49.   iPos: Integer;
  50. begin
  51.   if ADelim = #0 then begin
  52.     // AnsiPos does not work with #0
  53.     iPos := Pos(ADelim, AInput);
  54.   end else begin
  55.     iPos := Pos(ADelim, AInput);
  56.   end;
  57.   if iPos = 0 then begin
  58.     Result := AInput;
  59.     if ADelete then begin
  60.       AInput := '';
  61.     end;
  62.   end else begin
  63.     result := Copy(AInput, 1, iPos - 1);
  64.     if ADelete then begin
  65.       Delete(AInput, 1, iPos + Length(ADelim) - 1);
  66.     end;
  67.   end;
  68. end;
  69.  
  70. procedure TranslateStringToTInAddr(AIP: string; var AInAddr);
  71. var
  72.   phe: PHostEnt;
  73.   pac: PChar;
  74.   GInitData: TWSAData;
  75. begin
  76.   WSAStartup($101, GInitData);
  77.   try
  78.     phe := GetHostByName(PChar(AIP));
  79.     if Assigned(phe) then
  80.     begin
  81.       pac := phe^.h_addr_list^;
  82.       if Assigned(pac) then
  83.       begin
  84.         with TIPAddr(AInAddr).S_un_b do begin
  85.           s_b1 := Byte(pac[0]);
  86.           s_b2 := Byte(pac[1]);
  87.           s_b3 := Byte(pac[2]);
  88.           s_b4 := Byte(pac[3]);
  89.         end;
  90.       end
  91.       else
  92.       begin
  93.         raise Exception.Create('Error getting IP from HostName');
  94.       end;
  95.     end
  96.     else
  97.     begin
  98.       raise Exception.Create('Error getting HostName');
  99.     end;
  100.   except
  101.     FillChar(AInAddr, SizeOf(AInAddr), #0);
  102.   end;
  103.   WSACleanup;
  104. end;
  105.  
  106. function Ping(InetAddress : string) : boolean;
  107. var
  108.  Handle : THandle;
  109.  InAddr : IPAddr;
  110.  DW : DWORD;
  111.  rep : array[1..128] of byte;
  112. begin
  113.   result := false;
  114.   Handle := IcmpCreateFile;
  115.   if Handle = INVALID_HANDLE_VALUE then
  116.    Exit;
  117.   TranslateStringToTInAddr(InetAddress, InAddr);
  118.   DW := IcmpSendEcho(Handle, InAddr, nil, 0, nil, @rep, 128, 0);
  119.   Result := (DW  0);
  120.   IcmpCloseHandle(Handle);
  121. end;
  122.  
  123. end.

Related Discussions
  • HOW TO DO PING? (2001-01-10 14:28:57)
    You can use the ECHO protocol in which PING is based. Take a look to the TNMEcho component in FastNet page of Delphi component Palette.
  • MAKING A CHAT PROGRAM WITH TRICHEDIT... (2001-01-22 02:42:32)
    The TStrings of the RichEdit don't show the formating paragraphs. But when you save the RichEdit text to a stream (instead of a file) you will...
  • HOW TO SEND OUTPUT TO PRINTER DIRECTLY (2001-01-29 18:29:50)
    Hi Use this code to send Escape code to my POS printer but you can modify the code alittle to fit youre need. type TPassThroughData = record...
  • HOW CAN A MAKE SMALL DELPHI APPLICATIONS? (2001-01-31 06:49:09)
    To make the application smaller, and without a form you can do these things: 1. Open your project (or create a new) 2. Choose remove file from...
  • HOW TO TRANSFER FILES SECURELY? (2001-02-09 09:22:47)
    There are NO delphi components that handle SecureFTP. You could write your own FTP Server and FTP Client based upon the Indy code and implement...
  • PING PING ? (2001-02-09 02:31:37)
    To recreate this example, you will need to create a new blank Delphi application. Place a TEdit, 2 TLabels, 3 TButtons, and a TNMEcho on the...
  • IRC CLIENT (2001-02-23 19:08:01)
    Search for Active IRC component.Here is a few explanation about it. ActiveIRC Documentation Welcome to the documentation for the ActiveIRC...
  • DETECTING ADO MSACCESS CHANGES TO DATABASE BY OTHER USERS (2001-03-03 05:00:13)
    actually ms access is not a real rdbms especially not for a networked multiuser environment but most of the time that decision is out of bounds...
  • HOW TO PRINT FORMATTED STRINGS/MEMO FIELDS ? (2001-03-20 08:33:15)
    Search www.tamaracka.com for "Peter and Below and Raw" Peter has posted many code snippets to print direct to the printer from code. You may be...
  • HOW COULD I CONVERT A MP3 FILE TO WAVE AND WAVE TO MP3 (2001-03-21 15:47:13)
    How To Convert MP3 Files To WAV Files by Simon Lau - S.Lau@cs.ucl.ac.uk There may be some times when you need to convert an MP3 file back...
Latest News
Submit News Form Past News
Latest Forum Entries