我本机上有多网卡(分配了3个IP:2个固定IP,1个局网IP).如果编程实现获得本机的多个IP?(我只能取得一个)(200分)

  • 主题发起人 主题发起人 淡淡的笑
  • 开始时间 开始时间

淡淡的笑

Unregistered / Unconfirmed
GUEST, unregistred user!
我本机上有多网卡(分配了3个IP:2个固定IP,1个局网IP).如果编程实现获得本机的多个IP?(我只能取得一个)<br><br>// 此function获得 192.168.0.1<br><br>function TForm1.LookupIP: String;<br>type<br>&nbsp; &nbsp; TaPInAddr = array [0..10] of PInAddr;<br>&nbsp; &nbsp; PaPInAddr = ^TaPInAddr;<br>var<br>&nbsp; &nbsp; phe &nbsp;: PHostEnt;<br>&nbsp; &nbsp; pptr : PaPInAddr;<br>&nbsp; &nbsp; Buffer : array [0..63] of char;<br>&nbsp; &nbsp; I &nbsp; &nbsp;: Integer;<br>&nbsp; &nbsp; GInitData &nbsp; &nbsp; &nbsp;: TWSADATA;<br><br>begin<br>&nbsp; &nbsp; WSAStartup($101, GInitData);<br>&nbsp; &nbsp; Result := '';<br>&nbsp; &nbsp; GetHostName(Buffer, SizeOf(Buffer));<br>&nbsp; &nbsp; phe :=GetHostByName(buffer);<br>&nbsp; &nbsp; if phe = nil then Exit;<br>&nbsp; &nbsp; pptr := PaPInAddr(Phe^.h_addr_list);<br>&nbsp; &nbsp; I := 0;<br>&nbsp; &nbsp; while pptr^ &lt;&gt; nil do begin<br>&nbsp; &nbsp; &nbsp; result:=StrPas(inet_ntoa(pptr^^));<br>&nbsp; &nbsp; &nbsp; Inc(I);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; WSACleanup;<br>end;<br><br>// 此function获得 211.163.117.xxx<br><br>function TForm1.GetHostAddress: string;<br>var<br>&nbsp; wd: TWSAData;<br>&nbsp; phe: PHostEnt;<br>begin<br>&nbsp; if 0 &lt;&gt; WSAStartup(MakeWord(2, 0), wd) then<br>&nbsp; &nbsp; raise Exception.Create('初始化 WinSock 失败!')<br>&nbsp; else<br>&nbsp; try<br>&nbsp; &nbsp; SetLength(Result, 255);<br>&nbsp; &nbsp; if 0 &lt;&gt; gethostname(PChar(Result), 255) then<br>&nbsp; &nbsp; &nbsp; raise Exception.Create('无法取得本计算机名称!');<br>&nbsp; &nbsp; SetLength(Result, StrLen(PChar(Result)));<br>&nbsp; &nbsp; phe := gethostbyname(PChar(Result));<br>&nbsp; &nbsp; if nil = phe then<br>&nbsp; &nbsp; &nbsp; raise Exception.Create('无法解析本计算机名称到IP地址!');<br>&nbsp; &nbsp; //听资料说修改此处代码可适用于多个IP情况<br>&nbsp; &nbsp; Result := inet_ntoa(PInAddr(phe.h_addr^)^);<br>&nbsp; finally<br>&nbsp; &nbsp; WSACleanup;<br>&nbsp; end;<br>end;<br><br>如题目,要如何取得所有IP(我是固定IP)? 请指教.谢谢!!!
 
function TForm1.LookupIP: String;<br>改成 TStrings如何?<br>那样就可以用<br>...<br>&nbsp; &nbsp; while pptr^ &lt;&gt; nil do begin<br>&nbsp; &nbsp; &nbsp; result.add(inet_ntoa(pptr^^));<br>&nbsp; &nbsp; &nbsp; Inc(I);<br>&nbsp; &nbsp; end;<br>...<br>应该可以吧
 
转贴:<br><br>The following function returns all IP addresses on the local machine,<br>separated by newline. So e. g. you can assign the result string to the<br>Text property of any TStrings object.<br><br>uses Winsock;<br><br>function GetLocalIPs: String;<br>type PPInAddr= ^PInAddr;<br>var<br>&nbsp; wsaData: TWSAData;<br>&nbsp; HostInfo: PHostEnt;<br>&nbsp; HostName: Array[0..255] of Char;<br>&nbsp; Addr: PPInAddr;<br>begin<br>&nbsp; Result:='';<br>&nbsp; if WSAStartup($0101, wsaData)&lt;&gt;0 then exit;<br>&nbsp; try<br>&nbsp; &nbsp; if gethostname(HostName, SizeOf(HostName)) &lt;&gt; 0 then exit;<br>&nbsp; &nbsp; HostInfo:= gethostbyname(HostName);<br>&nbsp; &nbsp; if HostInfo=nil then Exit;<br>&nbsp; &nbsp; Addr:=Pointer(HostInfo^.h_addr_list);<br>&nbsp; &nbsp; if (Addr=nil) or (Addr^=nil) then exit;<br>&nbsp; &nbsp; Result:=StrPas(inet_ntoa(Addr^^));<br>&nbsp; &nbsp; inc(Addr);<br>&nbsp; &nbsp; while Addr^&lt;&gt;nil do begin<br>&nbsp; &nbsp; &nbsp; Result:=Result+^M^J+StrPas(inet_ntoa(Addr^^));<br>&nbsp; &nbsp; &nbsp; inc(Addr);<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; WSACleanup;<br>&nbsp; end;<br>end;<br>
 
ahxia,你怎么和我想的一样。
 
谢两位.试过之后马上给分.
 
&nbsp; 这是我写的获取第一个网卡上的所有IP地址,你只要稍家改动即可获得所有网卡的IP地址<br><br>Function gGetAllIP:TStringList;<br>const IPAddress_Length = 100; //最多只取5个IP<br>var<br>&nbsp; nret:integer;<br>&nbsp; hk: HKEY;<br>&nbsp; RootKey: HKEY;<br>&nbsp; SubKey: String;<br>&nbsp; IPDataBuf,MaskDataBuf,TBuf: Array [0 .. IPAddress_Length] Of Char;<br>&nbsp; ValueType, ValueSize: DWORD;<br>&nbsp; IpAddress,SubNetMask:String;<br>&nbsp; NetCardType:string;<br>&nbsp; i,m,j,k:integer;<br>&nbsp; IPList:TStringList;<br>begin<br>&nbsp; //读取本机IP地址<br>&nbsp; &nbsp;RootKey:=HKEY_LOCAL_MACHINE;<br>&nbsp; &nbsp;SubKey := 'SOFTWARE/Microsoft/Windows NT/CurrentVersion/NetworkCards/1';<br>&nbsp; &nbsp;nRet := RegOpenKeyEx(RootKey, PChar(SubKey), 0, KEY_READ, hk);<br>&nbsp; &nbsp;If (nRet &lt;&gt; ERROR_SUCCESS) Then<br>&nbsp; &nbsp;Begin<br>&nbsp; &nbsp; &nbsp;Application.MessageBox('不能打开注册表: NetworkCards/1 ','', MB_ICONINFORMATION or MB_OK);<br>&nbsp; &nbsp; &nbsp;RegCloseKey(hk);<br>&nbsp; &nbsp; &nbsp;Result := nil;<br>&nbsp; &nbsp; &nbsp;HALT;<br>&nbsp; &nbsp;End;<br>&nbsp; &nbsp;ValueType := REG_SZ;<br>&nbsp; &nbsp;ValueSize := IPAddress_Length;<br>&nbsp; &nbsp;nret := RegQueryValueEx(hk, 'ServiceName', Nil, @ValueType, PBYTE(@IPDataBuf), @ValueSize);<br>&nbsp; &nbsp;If (nRet &lt;&gt; ERROR_SUCCESS) Then<br>&nbsp; &nbsp;Begin<br>&nbsp; &nbsp; &nbsp;Application.MessageBox('查询注册表ServiceName出错!','',MB_ICONINFORMATION or MB_OK);<br>&nbsp; &nbsp; &nbsp;RegCloseKey(hk);<br>&nbsp; &nbsp; &nbsp;Result := nil;<br>&nbsp; &nbsp; &nbsp;HALT;<br>&nbsp; &nbsp;End;<br>&nbsp; &nbsp;RegCloseKey(hk);<br>&nbsp; &nbsp;NetCardType:= IPDataBuf; //网卡类型<br>&nbsp; &nbsp;ValueType:=REG_BINARY;<br>&nbsp; &nbsp;ValueSize:=IPAddress_Length;<br>&nbsp; &nbsp;ZeroMemory(@IPDataBuf,ValueSize);<br>&nbsp; &nbsp;SubKey := 'SYSTEM/CurrentControlSet/Services/' + NetCardType + '/Parameters/Tcpip';<br>&nbsp; &nbsp;nRet := RegOpenKeyEx(RootKey, PChar(SubKey), 0, KEY_READ, hk);<br>&nbsp; &nbsp;If (nRet &lt;&gt; ERROR_SUCCESS) Then<br>&nbsp; &nbsp;Begin<br>&nbsp; &nbsp; &nbsp;Application.MessageBox('不能打开注册表: Tcpip ','智能网业务管理系统安装',MB_ICONINFORMATION or MB_OK);<br>&nbsp; &nbsp; &nbsp;Result := nil;<br>&nbsp; &nbsp; &nbsp;HALT;<br>&nbsp; &nbsp;End;<br>&nbsp; &nbsp;nret := RegQueryValueEx(hk, 'IPAddress', Nil, @ValueType, PBYTE(@IPDataBuf), @ValueSize);<br>&nbsp; &nbsp;If (nRet &lt;&gt; ERROR_SUCCESS) Then<br>&nbsp; &nbsp;Begin<br>&nbsp; &nbsp; &nbsp;Application.MessageBox('查询注册表IpAddress出错!','智能网业务管理系统安装',MB_ICONINFORMATION or MB_OK);<br>&nbsp; &nbsp; &nbsp;RegCloseKey(hk);<br>&nbsp; &nbsp; &nbsp;Result := nil;<br>&nbsp; &nbsp; &nbsp;HALT;<br>&nbsp; &nbsp;End;<br>&nbsp; &nbsp;RegCloseKey(hk);<br><br>&nbsp; &nbsp;IPList:=TStringList.Create;<br>&nbsp; &nbsp;ZeroMemory(@TBuf,ValueSize);<br>&nbsp; &nbsp;k:=0;<br>&nbsp; &nbsp;for i:=0 to ValueSize-1 do<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; TBuf[k]:=IPDataBuf;<br>&nbsp; &nbsp; &nbsp; k:=k+1;<br>&nbsp; &nbsp; &nbsp; if Ord(IPDataBuf)=0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IPList.Add(TBuf);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ZeroMemory(@TBuf,ValueSize);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Ord(IPDataBuf[i+1])=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=i;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;Result:=IPList;<br>end;
 
多人接受答案了。
 
后退
顶部