Ver Mensaje Individual
  #6  
Antiguo 22-05-2016
Avatar de escafandra
[escafandra] escafandra is offline
Miembro Premium
 
Registrado: nov 2007
Posts: 2.197
Reputación: 20
escafandra Tiene un aura espectacularescafandra Tiene un aura espectacular
Te muestro una Unit simple para hacer ping por código. Te permite direcciones en formato IP o Web:

Código Delphi [-]
unit UPing;

interface

uses
  Windows, WinSock;

function IcmpCreateFile: Integer; stdcall external 'iphlpapi'
function IcmpSendEcho(Handle, Address: Integer; RequestData: PChar; RequestSize: Word; RequestOptions, ReplyBuffer: PChar; ReplySize, TimeOut: Cardinal): Cardinal; stdcall external 'iphlpapi';
function IcmpCloseHandle(IcmpHandle: Cardinal): boolean; stdcall external 'iphlpapi'
function Ping(Addr: PCHAR; Rep: integer = 3): boolean;

type
ICMP_OPTION_INFORMATION =  packed record
   Ttl:           u_char;
   Tos:           u_char;
   Flags:         u_char;
   OptionsSize:   u_char;
   OptionsData:   Pointer;
end;
PICMP_OPTION_INFORMATION=  ^ICMP_OPTION_INFORMATION;

ICMP_ECHO_REPLY = packed record
   Address:       Cardinal;
   Status:        Cardinal;
   RoundTripTime: Cardinal;
   DataSize:      Word;
   Reserved:      Word;
   Data:          Pointer;
   Options:       ICMP_OPTION_INFORMATION; 
end;
PICMPP_ECHO_REPLY = ^ICMP_ECHO_REPLY;

implementation

function Ping(Addr: PCHAR; Rep: integer = 3): boolean;
var
  WSA: TWSAData;
  hIcmpFile: Cardinal;
  Reply: ICMP_ECHO_REPLY;
  He: Phostent;
begin
  Result:= false;

  // Inicializar WinSock
  if WSAStartup(MAKEWORD(1, 1), WSA) <> 0 then exit;

  //Obtener IP de Addr
  He:= gethostbyname(Addr);
  if He = nil then exit;

  // Envia Ping
  hIcmpFile:= IcmpCreateFile;
  repeat
    Result:= IcmpSendEcho(hIcmpFile, PULONG(He.h_addr_list^)^, 0, 0, 0, PCHAR(@Reply), sizeof(ICMP_ECHO_REPLY), 1000) <> 0;
    Result:= Result and (Reply.Status = 0); // Error en Ping el Host no lo recibe....
    dec(Rep);
  until Result or (Rep = 0);

  // Cerrar...
  IcmpCloseHandle(hIcmpFile);
  WSACleanup;
end;

end.

Saludos.
Responder Con Cita