unit NetworkUnit; interface uses Windows, SysUtils, WinSock; type TNetworkInfo = record HostName: String; IPAddress: String; end; function GetMachineNetworkInfo: TNetworkInfo; function GetHostName(Ip : String) : String; function GetIpAddress(HostName : String) : String; function GetNetUser : Ansistring; type THostInfo = record HostName: String; IPAddress: String; AnotherNames: array of String; end; function GetHostInfo(NetworkName: String): THostInfo; implementation {------------------------------- // 自マシンのマシン名とIPアドレスを取得する 備考: uses WinSockが必要 履歴: 2003/11/12 //--▼----------------------▽--} function GetMachineNetworkInfo: TNetworkInfo; var wsaData : TWsaData; hostName : array [0..255] of AnsiChar; 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 若干簡単になるように修正 2008/02/06 inet_addrとWSAStartupの行を入れ替えた //--▼----------------------▽--} function GetHostName(Ip : String) : String; var PH : PHostEnt; InAddr: TInAddr; WSADATA : TWSADATA; begin Result := ''; if Ip = '' then exit; WSAStartup(MakeWord(1,1) , WSADATA); //MakeWord(1,1)=$0101 InAddr.S_addr := inet_addr(PAnsiChar(Ip)); 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(PAnsiChar(HostName)); if PH = nil then Exit; InAddr := PInAddr(ph^.h_addr_list^)^; Result := inet_ntoa(InAddr); WSACleanup; end; //--△----------------------▲-- {------------------------------- // ログイン中のユーザー名を取得する 備考: uses SysUtils 履歴: 2008/02/01 //--▼----------------------▽--} function GetNetUser : Ansistring; var dwI : DWord; begin dwI := MAX_PATH; SetLength (Result, dwI + 1); if WNetGetUser (Nil, PChar (Result), dwI) = NO_ERROR then SetLength (Result, StrLen (PChar (Result))) else SetLength (Result, 0) end; //--△----------------------▲-- {------------------------------- // host名かIPアドレスからhost情報を求める 引数: host名かIPアドレスを渡す 戻り値: ホスト名/IPアドレス/ホスト別名 ホスト別名は文字列の動的配列で戻るようになる 備考: uses Winsockが必要 履歴: 2008/02/06 //--▼----------------------▽--} function GetHostInfo(NetworkName: String): THostInfo; type PPChar=^PChar; var PH : PHostEnt; InAddr: TInAddr; WSADATA : TWSADATA; ppc: PPChar; begin Result.HostName := ''; Result.IPAddress := ''; SetLength(Result.AnotherNames, 0); if NetworkName='' then Exit; WSAStartup(MakeWord(1,1) , WSADATA); InAddr.S_addr := inet_addr(PAnsiChar(NetworkName)); if InAddr.S_addr = INADDR_NONE then begin PH := gethostbyname(PAnsiChar(NetworkName)); end else begin PH := gethostbyaddr(@(InAddr.S_addr), 4, AF_INET); end; Result.HostName := PH^.h_name; Result.IPAddress := inet_ntoa(PInAddr(PH^.h_addr_list^)^); ppc:=PPChar(PH^.h_aliases); while assigned(ppc^) do begin SetLength(Result.AnotherNames, Length(Result.AnotherNames)+1); Result.AnotherNames[Length(Result.AnotherNames)-1] := ppc^; inc(ppc); end; WSACleanUp; end; //--△----------------------▲-- end.