www.pudn.com > 2007717233531.rar > Common.pas


unit Common; 
 
interface 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, ExtCtrls, StdCtrls, EDecode, winsock, WinInet; 
function CheckHostToIP(sHost: string): string; 
function CheckIsIpAddr(Name: string): Boolean; 
implementation 
 
function HostToIP(Name: string; var Ip: string): Boolean; 
var 
  wsdata: TWSAData; 
  hostName: array[0..255] of char; 
  hostEnt: PHostEnt; 
  addr: PChar; 
begin 
  WSAStartup($0101, wsdata); 
  try 
    gethostname(hostName, sizeof(hostName)); 
    StrPCopy(hostName, Name); 
    hostEnt := gethostbyname(hostName); 
    if Assigned(hostEnt) then 
      if Assigned(hostEnt^.h_addr_list) then begin 
        addr := hostEnt^.h_addr_list^; 
        if Assigned(addr) then begin 
          Ip := Format('%d.%d.%d.%d', [byte(addr[0]), 
            byte(addr[1]), byte(addr[2]), byte(addr[3])]); 
          Result := True; 
        end 
        else 
          Result := False; 
      end 
      else 
        Result := False 
    else begin 
      Result := False; 
    end; 
  finally 
    WSACleanup; 
  end 
 
end; 
 
function CheckIsIpAddr(Name: string): Boolean; 
var 
  PStr: char; 
  Temp: PChar; 
  I: integer; 
begin 
  Result := True; 
  if Length(Name) <= 15 then begin 
    for I := 0 to Length(Name) do begin 
      Temp := PChar(copy(Name, I, 1)); 
      PStr := Temp^; 
      if not (PStr in ['0'..'9', '.']) then begin 
        Result := False; 
        break 
      end; 
    end; 
  end else Result := False; 
end; 
 
function CheckHostToIP(sHost: string): string; 
var 
  sIpAddr: string; 
begin 
  if HostToIP(sHost, sIpAddr) then Result := sIpAddr 
  else Result := '127.0.0.1'; 
end; 
 
end.