用哪个api函数可得到本机IP地址!(40分)

  • 主题发起人 主题发起人 杨虎
  • 开始时间 开始时间

杨虎

Unregistered / Unconfirmed
GUEST, unregistred user!
如问题!<br>抱歉我没分了!
 
我是这样读的:<br><br>procedure TForm1.ReadCurrentClick(Sender: TObject);<br>var FIniFile : TRegistry;<br>&nbsp; &nbsp; pathstr : string;<br>begin<br>&nbsp; FIniFile := TRegistry.Create();<br>&nbsp; FIniFile.RootKey := HKEY_LOCAL_MACHINE;<br>&nbsp; pathstr := '/System/CurrentControlSet/Services/';<br>&nbsp; FIniFile.OpenKey(pathstr+'Class/NetTrans/0000', false);<br>&nbsp; EDIT1.TEXT := FIniFile.READString('IPAddress');<br>&nbsp; FiniFile.free;<br>end;<br>
 
function my_ip_address:longint;<br>const<br>&nbsp; bufsize=255;<br>var<br>&nbsp; buf: pointer;<br>&nbsp; RemoteHost : PHostEnt; (* No, don't free it! *)<br><br>begin<br>&nbsp; buf:=NIL;<br>&nbsp; try<br>&nbsp; &nbsp; getmem(buf,bufsize);<br>&nbsp; &nbsp; winsock.gethostname(buf,bufsize); &nbsp; (* this one maybe without domain *)<br>&nbsp; &nbsp; RemoteHost:=Winsock.GetHostByName(buf);<br>&nbsp; &nbsp; if RemoteHost=NIL then<br>&nbsp; &nbsp; &nbsp; my_ip_address:=winsock.htonl($07000001) &nbsp;(* 127.0.0.1 *)<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; my_ip_address:=longint(pointer(RemoteHost^.h_addr_list^)^);<br>&nbsp; finally<br>&nbsp; &nbsp; if buf&lt;&gt;NIL then &nbsp;freemem(buf,bufsize);<br>&nbsp; end;<br>&nbsp; result:=winsock.ntohl(result);<br>end;<br><br>
 
我想如果你懂得IP地址应该对返回值是Longint不觉得奇怪吧
 
注册表中<br>HKEY_LOCAL_MACHINE/System/CurrentControlSet/<br>Services/Class/NetTrans/0000
 
读注册表的方法不完全正确,<br>根据安装的网络协议不同,<br>NetTrans/XXXX路径会不同,<br>我的机器上就是在NetTrans/0004下<br><br>应该用DNChen的方法,<br>使用WinSock函数
 
gethostbyname(''); 反回一个结构,在里面可得出本机ip
 
&nbsp; 我现在有更好的办法。<br>&nbsp; &nbsp;有需要请来信,别客气。<br>&nbsp; &nbsp;写清楚!
 
我这儿有来自tqz的一份方法<br>&nbsp;uses Winsock; <br><br>function LocalIP : 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>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>试一下,效果真好!
 
取得本地计算机的名字:GetComputerName<br>&nbsp;取得本地登录用户名:GetUserName<br><br>取得本地ip地址:<br>function LocalIP : 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>
 
(忘了从哪copy下的一段东西,没有实验,不知道好不好用)<br>取得本地计算机的名字:<br>&nbsp; Windows 95 API: GetComputerName<br>&nbsp; 取得本地登录用户名:GetUserName<br><br>取得本地ip地址:<br>function LocalIP : 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><br>var<br>&nbsp; WSData:TWSAData;<br>&nbsp; Buffer:array[0..63]of Char;<br>&nbsp; HostEnt:PHostEnt;<br>&nbsp; PPInAddr:^PInAddr;<br>&nbsp; //返回值<br>&nbsp; LocalIP:DWord;<br>&nbsp; IPString:String;<br><br>begin<br>&nbsp; LocalIP:=0;<br>&nbsp; IPString:='';<br>&nbsp; WSAStartUp($101,WSData);<br>&nbsp; try<br>&nbsp; &nbsp; GetHostName(Buffer,SizeOf(Buffer));<br>&nbsp; &nbsp; HostEnt:=GetHostByName(Buffer);<br>&nbsp; &nbsp; if Assigned(HostEnt) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; PPInAddr:=@(PInAddr(HostEnt.H_Addr_List^));<br>&nbsp; &nbsp; &nbsp; while Assigned(PPInAddr^) do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; IPString:=StrPas(INet_NToA(PPInAddr^^));<br>&nbsp; &nbsp; &nbsp; &nbsp; LocalIP:=PPInAddr^^.S_Addr;<br>&nbsp; &nbsp; &nbsp; &nbsp; Inc(PPInAddr);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; WSACleanUp;<br>&nbsp; end;<br>end;<br><br>HostEnt.H_Addr_List是一个PInAddr指针列表<br>原始代码中将它转换为PInAddr数组(0..10)<br>我觉得不好:虽然等价,但与定义不符;而且<br>0..10显然是自己想象的结果,认为0..10足够了。<br><br>修改后的结果,取消了array of PInAddr的定义,<br>将HostEnt.H_Addr_List作为^PInAddr(指针的指针),<br>保存在PPInAddr变量中,以Inc()过程指向下一个。<br><br>另外一些小的修改:<br>1、xxx&lt;&gt;Nil修改为Assigned(xxx)<br>2、主要操作放到try..finally中<br><br>大家也许嫌我罗嗦吧?
 
请教GetComputerName函数的用法,<br>当我打入:<br><br>HostName:String;<br>GetComputerName(pchar(HostName),Sizeof(HostName));<br>的时候,编译器报告:<br>Types of actual and formal var parameters must be identical<br><br>究竟是怎么一回事呢?<br><br>谢谢!
 
后退
顶部