可以用API得到本机IP吗?(100分)

  • 主题发起人 主题发起人 hksoobe
  • 开始时间 开始时间
H

hksoobe

Unregistered / Unconfirmed
GUEST, unregistred user!
知道的请告诉我一下!可以详细点吗?
 
新建一Project1,在form1中添加控件statusbar <br><br>1(Win32页)和控件IdIPWatch1(Indy Misc页)。接着配置好控件属性:在statusbar1的panels中添加0-TStatusPanel和1-TStatusPanel两项;IdIPWatch1的HistoryEnabled的属性设为False。 <br><br>编写代码:在TForm1.FormCreate(Sender: TObject)中加入以下代码: <br><br>statusbar1.Panels[1].Text:=IdIPWatch1.LocalIP;
 
大哥我要API.这我知道的.
 
api是不晓得,但可以这样去得到<br><br>&nbsp;function getipaftvpn():string;<br>&nbsp;var Buf: array[0..MAX_PATH] of Char;<br>&nbsp; &nbsp; Path,Cmd,S:string;<br>&nbsp; &nbsp; F:TextFile;<br>&nbsp; &nbsp; i:integer;<br>&nbsp; &nbsp; tmp:Tstringlist;<br>&nbsp; &nbsp; a,b:integer;<br>&nbsp; &nbsp; tmp2:string;<br>&nbsp;begin<br><br>&nbsp; GetWindowsDirectory(Buf,MAX_PATH);<br>&nbsp;if string(Buf)[Length(string(Buf))]&lt;&gt;'/' then<br>&nbsp; &nbsp; Path:=string(Buf)+'/'<br>&nbsp; else<br>&nbsp; &nbsp; &nbsp;Path:= string(Buf);<br><br>&nbsp;if FileExists(path+'VPNIP.txt') then DeleteFile(path+'VPNIP.txt');<br>&nbsp;Cmd:='command.com /c ipconfig /all &gt; '+path+'VPNIP.txt';<br>&nbsp;//Winexec(pchar(Cmd),sw_hide);<br>&nbsp;WinExecAndWait32(pchar(cmd),sw_hide);<br>&nbsp;while not FileExists(path+'VPNIP.txt') do<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;Sleep(50);<br>&nbsp; &nbsp;end;<br>&nbsp;if FileExists(path+'VPNIP.txt') then Sleep(100);<br>&nbsp; &nbsp;try<br>&nbsp; &nbsp;tmp:=Tstringlist.Create;<br>&nbsp; &nbsp;tmp.LoadFromFile(Path+'Vpnip.txt');<br>&nbsp; &nbsp; for a:=0 to tmp.Count -1 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;if pos('网络加速连接',tmp.Strings[a])&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for b:=a to tmp.Count -1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tmp2:=tmp.Strings;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if tmp2=' &nbsp; &nbsp; &nbsp; &nbsp;IP Address. . . . . . . . . . . . : ' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Continue;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if pos('IP Address. . . . . . . . . . . . :',tmp2)&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tmp2:=Trim(tmp2);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i:=pos(':',tmp2);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Delete(tmp2,1,i);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=Trim(tmp2);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp;except<br>&nbsp; &nbsp; Result:='';<br>&nbsp; &nbsp;tmp.Free;<br>&nbsp; &nbsp;exit;<br>&nbsp; &nbsp;end;<br><br><br>end;
 
uses WinSock<br><br>function LocalIP: string;<br>type<br>&nbsp; TaPInAddr = array[0..10] of PInAddr;<br>&nbsp; PaPInAddr = ^TaPInAddr;<br>var<br>&nbsp; phe: PHostEnt;<br>&nbsp; pptr: PaPInAddr;<br>&nbsp; Buffer: array[0..63] of char;<br>&nbsp; I: Integer;<br>&nbsp; GInitData: TWSADATA;<br><br>begin<br>&nbsp; WSAStartup($101, GInitData);<br>&nbsp; Result := '';<br>&nbsp; GetHostName(Buffer, SizeOf(Buffer));<br>&nbsp; //取得计算机名<br>&nbsp; phe := GetHostByName(buffer);<br>&nbsp; //取得计算机IP<br>&nbsp; if phe = nil then<br>&nbsp; &nbsp; Exit;<br>&nbsp; pptr := PaPInAddr(Phe^.h_addr_list);<br>&nbsp; I := 0;<br>&nbsp; while pptr^ &lt;&gt; nil do<br>&nbsp; begin<br>&nbsp; &nbsp; result := StrPas(inet_ntoa(pptr^^));<br>&nbsp; &nbsp; //返回结果<br>&nbsp; &nbsp; Inc(I);<br>&nbsp; end;<br>&nbsp; WSACleanup;<br>end;
 
chatop,这个我也写过一个类似的,bahamut8348你用的是WinSock的方法吗?我来试试不知道行不行,那分就你们两个平分吧!
 
多人接受答案了。
 
bahamut8348你那个方法只能够得到只有一个网卡或没有装虚拟网卡的机器上用。
 
后退
顶部