Beranda > Delphi > # Getting your local IP address or host name.

# Getting your local IP address or host name.

Use this function to get your local IP address or host name.uses Winsock;

type
  IP_HostName = (_IP, _HOSTNAME);

function GetLocalIPAddressOrHostName(IP_Or_HostName: IP_HostName): string;
var
  p : PHostEnt;
  s : array[0..128] of char;
  p2 : pchar;
  wVersionRequested : WORD;
  wsaData : TWSAData;
begin
  {Start up WinSock}
  wVersionRequested := MAKEWORD(1, 1);
  WSAStartup(wVersionRequested, wsaData);
  {Get the computer name}
  GetHostName(@s, 128);
  p := GetHostByName(@s);
  if IP_Or_HostName = _HOSTNAME then
    Result := p^.h_Name
  else
    begin
      {Get the IpAddress}
      p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
      Result := p2;
    end;
  {Shut down WinSock}
  WSACleanup;
end;

Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan komentar