unit NetworkUnit; interface uses Windows, WinSock; type TNetworkInfo = record HostName: String; IPAddress: String; end; function GetMachineNetworkInfo: TNetworkInfo; function GetHostName(Ip : String) : String; function GetIpAddress(HostName : String) : String; implementation {------------------------------- // 自マシンのマシン名とIPアドレスを取得する 備考: uses WinSockが必要 履歴: 2003/11/12 //------------------------------} function GetMachineNetworkInfo: TNetworkInfo; var wsaData : TWsaData; hostName : array [0..255] of char; host : PHostEnt; in_addr : TInAddr; begin WSAStartup($101, wsaData); WinSock.gethostname(hostName,sizeof(hostName)); host := gethostbyname(hostName); in_addr :=PInAddr(PInAddr(host^.h_addr_list)^)^; Result.HostName :=String(hostName); Result.IPAddress :=String(inet_ntoa(in_addr)); WSACleanup; end; //------------------------------ {------------------------------- // IPアドレスからhost名を求める 備考: uses Winsockが必要 履歴: 2003/11/12 2006/04/04 若干簡単になるように修正 //------------------------------} function GetHostName(Ip : String) : String; var PH : PHostEnt; InAddr: TInAddr; WSADATA : TWSADATA; begin Result := ''; if Ip = '' then exit; InAddr.S_addr := inet_addr(PChar(Ip)); WSAStartup(MakeWord(1,1) , WSADATA); //MakeWord(1,1)=$0101 PH := gethostbyaddr(@InAddr,4,PF_INET); if PH = nil then Exit; Result := string(ph^.h_name); WSACleanup; end; //------------------------------ {------------------------------- // host名からIPアドレスを求める 備考: uses Winsockが必要 履歴: 2006/04/04 //------------------------------} function GetIpAddress(HostName : String) : String; var PH : PHostEnt; InAddr: TInAddr; WSADATA : TWSADATA; begin Result := ''; if HostName = '' then exit; WSAStartup(MakeWord(1,1), WSADATA); PH := gethostbyname(PChar(HostName)); if PH = nil then Exit; InAddr := PInAddr(ph^.h_addr_list^)^; Result := inet_ntoa(InAddr); WSACleanup; end; //------------------------------ end.