Delphi获取计算机名称及IP地址

function GetComputerName:String; //获取计算机名称
var
  wVersionRequested : WORD;
  wsaData : TWSAData;
  p : PHostEnt; s : array[0..128] of char;
begin
  try
    wVersionRequested := MAKEWORD(1, 1); //创建 WinSock
    WSAStartup(wVersionRequested, wsaData); //创建 WinSock
    GetHostName(@s,128);
    p:=GetHostByName(@s);
    Result:=p^.h_Name;
  finally
    WSACleanup; //释放 WinSock
  end;
end;

function GetHostIP:String; //获取IP
var
   wVersionRequested : WORD;
   wsaData : TWSAData;
   p : PHostEnt; s : array[0..128] of char; p2 : pchar;
begin
   try
      wVersionRequested := MAKEWORD(1, 1); //创建 WinSock
      WSAStartup(wVersionRequested, wsaData); //创建 WinSock
      GetHostName(@s,128);
      p:=GetHostByName(@s);
      p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
      Result:= P2;
   finally
      WSACleanup; //释放 WinSock
   end;
end;

你可能感兴趣的:(function,Delphi)