怎么样得到本机的地址(50分)

  • 主题发起人 主题发起人 zryzry
  • 开始时间 开始时间
Z

zryzry

Unregistered / Unconfirmed
GUEST, unregistred user!
在DELPHI中,怎样用API函数得到本机的IP地址,<br>望各位赐教!
 
GetHostName()<br>GetHostByName()
 
此类问题以前有很多贴子提到,你可以找一下。
 
uses windows ,winsock;<br><br>procedure GetHostInfo(var HostName,IPAddress:string);<br>{ 入口:两个字符串指针(传址)<br>&nbsp; &nbsp; &nbsp; &nbsp; hostname &nbsp;指向本机主机名的字符串<br>&nbsp; &nbsp; &nbsp; &nbsp; IPAddress 指向本机IP地址的字符串<br>出口:同入口<br>}<br><br>procedure GetHostInfo(var HostName,IPAddress:string);<br>var<br>&nbsp; &nbsp;WSAData :TWSAData;<br>&nbsp; &nbsp;HostEnt: PHostEnt;<br>begin<br>&nbsp; &nbsp;WSAStartup(2,WSAData);<br>&nbsp; &nbsp;SetLength(HostName,255);<br>&nbsp; &nbsp;GetHostName(PChar(HostName),255);<br>&nbsp; &nbsp;SetLength(HostName, StrLen(PChar(HostName)));<br>&nbsp; &nbsp;HostEnt :=gethostbyname(PChar(HostName));<br>&nbsp; &nbsp;with HostEnt^ do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;IPAddress := Format('%d.%d.%d.%d',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[Byte(h_addr^[0]),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Byte(h_addr^[1]),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Byte(h_addr^[2]),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Byte(h_addr^[3])<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );<br>&nbsp; &nbsp; &nbsp;end;//with<br>&nbsp; &nbsp;WSACleanup;<br>end;
 
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>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>then &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.<br>===========================================<br>
 
FastNet下的控件有IP属性
 
多人接受答案了。
 
后退
顶部