Friday, May 25, 2012

HOW TO RESOLVE A HOST NAME

uses
  Winsock;
...
// The IP number will be returned in string format in the sIP parameter
function HostToIP(sHost: string; var sIP: string): Boolean;
var
   aHostName: array[0..255] of Char;
   pcAddr : PChar;
   HostEnt : PHostEnt;
   wsData : TWSAData;
begin
   WSAStartup($0101, wsData);
   try
     GetHostName(aHostName, SizeOf(aHostName));
     StrPCopy(aHostName, sHost);
     hostEnt := GetHostByName(aHostName);
     if Assigned(HostEnt) then
       if Assigned(HostEnt^.H_Addr_List) then 
       begin
         pcAddr := HostEnt^.H_Addr_List^;
         if Assigned(pcAddr) then 
         begin
           sIP := Format('%d.%d.%d.%d', [Byte(pcAddr[0]), Byte(pcAddr[1]), Byte(pcAddr[2]), Byte(pcAddr[3])]);
           Result := True;
         end else
           Result := False;
       end else
         Result := False
     else 
    begin
      Result := False;
    end;
  finally
    WSACleanup;
   end;
end;

0 comments:

Post a Comment

 
Related Posts Plugin for WordPress, Blogger...