如何用Delphi在程序中取得本机的IP地址(0分)

  • 主题发起人 主题发起人 yhfang
  • 开始时间 开始时间
来自:liuly 时间:00-3-23 21:29:46 ID:204584 <br>以下这个程序可以得到本机IP地址和计算机名:<br><br>uses Winsock;<br><br>... ...<br>&nbsp;<br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br>&nbsp; wVersionRequested : WORD;<br>&nbsp; wsaData : TWSAData;<br>begin<br>&nbsp; {Start up WinSock}<br>&nbsp; wVersionRequested := MAKEWORD(1, 1);<br>&nbsp; WSAStartup(wVersionRequested, wsaData);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; p : PHostEnt;<br>&nbsp; s : array[0..128] of char;<br>&nbsp; p2 : pchar;<br>begin<br>&nbsp; {获得计算机名}<br>&nbsp; GetHostName(@s, 128);<br>&nbsp; p := GetHostByName(@s);<br>&nbsp; Memo1.Lines.Add(p^.h_Name);<br>&nbsp; {获得本机的ip地址}<br>&nbsp; p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);<br>&nbsp; Memo1.Lines.Add(p2);<br>end;<br><br><br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br>&nbsp; {Shut down WinSock}<br>&nbsp; WSACleanup;<br>end;<br><br>&nbsp;<br>&nbsp;<br>
 
http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=204486
 
刚买了本书,正好有这么一段程序,虽然没全看懂,也把它贴上来 :)<br>procedure TForm1.Button1Click(Sender: TObject);<br>var Ip,IpStr:string; &nbsp;ch:array[1..32]of char; &nbsp;i:integer;<br>&nbsp; &nbsp; WSData:TWSAData; &nbsp;MyHost:PHostEnt;<br>begin<br>&nbsp; &nbsp;if WSAstartup(2,WSData)&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowMessage('没有成功返回!');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Halt(2);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; if getHostName(@ch[1],32)&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowMessage('没有成功返回!');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Halt(3);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp; ShowMessage('没有成功返回!');<br>&nbsp; &nbsp; &nbsp; Halt(3);<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;MyHost:=GetHostByName(@ch[1]);<br>&nbsp; &nbsp;if MyHost=NIL then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowMessage('没有成功返回!');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Halt(4);<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for i:=1 to 4 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ip:=inttostr(Ord(MyHost.h_addr^[i-1]));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowMessage('IP分段地址为:'+Ip);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IPStr:=IPStr+Ip;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if i&lt;4 then IPStr:=IPStr+'.'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else &nbsp;ShowMessage('IP地址为:'+IPStr);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>end;
 
program get_ip;<br>uses<br>&nbsp; winsock,sysutils;<br>VAR<br>&nbsp; ch : ARRAY[1..32] OF Char;<br>&nbsp; i : Integer;<br>&nbsp; WSData: TWSAData;<br>&nbsp; MyHost: PHostEnt;<br>begin<br>&nbsp; IF WSAstartup(2,wsdata)&lt;&gt;0 THEN<br>&nbsp; &nbsp; BEGIN<br>&nbsp; &nbsp; &nbsp; Writeln('can''t start Winsock: Error ',WSAGetLastError);<br>&nbsp; &nbsp; &nbsp; Halt(2);<br>&nbsp; &nbsp; END;<br>&nbsp; try<br>&nbsp; &nbsp; IF getHostName(@ch[1],32)&lt;&gt;0 THEN<br>&nbsp; &nbsp; &nbsp; BEGIN<br>&nbsp; &nbsp; &nbsp; &nbsp; Writeln('getHostName failed');<br>&nbsp; &nbsp; &nbsp; &nbsp; Halt(3);<br>&nbsp; &nbsp; &nbsp; END;<br>&nbsp; except<br>&nbsp; &nbsp; Writeln('getHostName failed');<br>&nbsp; &nbsp; halt(3);<br>&nbsp; end;<br>&nbsp; MyHost:=GetHostByName(@ch[1]);<br>&nbsp; IF MyHost=NIL THEN<br>&nbsp; &nbsp; BEGIN<br>&nbsp; &nbsp; &nbsp; Writeln(GetHostName('+StrPas(@ch[1])+') failed : Error<br>'+IntToStr(WSAGetLastError));<br>&nbsp; &nbsp; &nbsp; Halt(4);<br>&nbsp; &nbsp; END<br>&nbsp; ELSE<br>&nbsp; &nbsp; BEGIN<br>&nbsp; &nbsp; &nbsp; &nbsp; Write('address ');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FOR i:=1 TO 4 DO<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BEGIN<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Write(Ord(MyHost.h_addr^[i-1]));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IF i&lt;4 THEN<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; write('.')<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ELSE<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writeln;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; END;<br>&nbsp; &nbsp;END;<br>end.
 
非常好就0分 :-(
 
后退
顶部