取得本机的IP地址函数

function GetHostIP: string;
{*******************************************************
描述:取得本机的IP地址函数
*******************************************************
}
var
  ch: 
array[1..32of Char;
  i: Integer;
  WSData: TWSAData;
  MyHost: PHostEnt;
  IP: string;
begin
  IP :
= '';
  
if WSAstartup(2, wsdata) <> 0 then
    Result :
= '0.0.0.0';

  try
    
if getHostName(@ch[1], 32<> 0 then
      Result :
= '0.0.0.0';
  except
    Result :
= '0.0.0.0';
  
end;

  MyHost :
= GetHostByName(@ch[1]);
  
if MyHost <> nil then
  
begin
    
for i := 1 to 4 do
    
begin
      IP :
= IP + inttostr(Ord(MyHost.h_addr^[i - 1]));
      
if i < 4 then
        IP :
= IP + '.'
    
end;
  
end;
  Result :
= IP;
end;
posted @ 2008-08-05 12:44  威尼斯的夏天  阅读(290)  评论(0编辑  收藏  举报