본문 바로가기

Development/Delphi

로컬 IP 가져오기

function TForm1.GetLocalAddress:string;
var wsaData : TWSAData;
  slocal : array[0..255] of AnsiChar;
  phos : PHostEnt;
  addr : PChar;
begin
  Result := '';
  if (WSAStartup(MAKEWORD(1, 1), wsaData) <> 0) then exit;
  if (gethostname(slocal, sizeof(slocal)) = SOCKET_ERROR) then exit;
  phos := gethostbyname(slocal);
  if phos = nil then exit;
  addr := PChar(phos.h_addr_list^);
  Result := inet_ntoa(PInAddr(addr)^);
  WSACleanup();
end;