Mega Code Archive

 
Categories / Delphi / Examples
 

How to get local ip address

How to get local IP address uses Winsock; function GetLocalIP: string; var wsaData: TWSAData; addr: TSockAddrIn; Phe: PHostEnt; szHostName: array[0..128] of Char; begin Result := ''; if WSAStartup($101, WSAData) <> 0 then Exit; try if GetHostName(szHostName, 128) <> SOCKET_ERROR then begin Phe := GetHostByName(szHostName); if Assigned(Phe) then begin addr.sin_addr.S_addr := longint(plongint(Phe^.h_addr_list^)^); Result := inet_ntoa(addr.sin_addr); end; end; finally WSACleanup; end; end;