如何获得本机的IP?(50分)

  • 主题发起人 主题发起人 westdog
  • 开始时间 开始时间
W

westdog

Unregistered / Unconfirmed
GUEST, unregistred user!
高手出招
 
这个不可以么?

来自:李颖 时间:2000-4-26 21:49:30 ID:232186
程序如下:
uses
.....WinSock....

var
WSData: TWSAData;
Buffer: array[0..63] of Char;
HostEnt: PHostEnt;
PPInAddr: ^PInAddr;

LocalIP: DWord;
IPString: String;

//取本机IP地址
procedure GetIP;
begin
LocalIP:=0;
IPString:='';
try
WSAStartUp($101, WSData);
GetHostName(Buffer, SizeOf(Buffer));
HostEnt:=GetHostByName(Buffer);
if Assigned(HostEnt) then
begin
PPInAddr:=@(PInAddr(HostEnt.H_Addr_List^));
while Assigned(PPInAddr^) do
begin
IPString:=StrPas(INet_NToA(PPInAddr^^));
LocalIP:=PPInAddr^^.S_Addr;
Inc(PPInAddr);
end;
end;
finally
try
WSACleanUp;
except
end;
end;
end;

//取本机IP地址,返回4字节格式
function GetIPAddress: DWORD;
begin
GetIP;
Result := LocalIP;
end;


//取本机IP地址,返回点分隔字符串格式
function GetLocalIP: String;
begin
GetIP;
Result := IPString;
end;



 
function GetLocalIPAddr:string;
var
pBuf: pointer;
RemoteHost : PHostEnt; (* No, don't free it! *)
liTemp : longint;
wsData: TWSAData;
begin
pBuf := NIL;
try
WSAStartup(2, wsData);
Getmem(pBuf,255);
GetHostName(pBuf,255); (* this one maybe without domain *)
RemoteHost:=Winsock.GetHostByName(pBuf);
if RemoteHost=NIL then
liTemp:=winsock.htonl($7F000001) (* 127.0.0.1 *)
else
liTemp:=longint(pointer(RemoteHost^.h_addr_list^)^);
finally
if pBuf <> NIL then Freemem(pBuf,255);
WSACleanup;
end;
liTemp := Winsock.ntohl(liTemp);

if liTemp = $7F000001 then
Result := ''
else
Result := IntToStr((liTemp shr 24) and $000000FF) + '.' +
IntToStr((liTemp shr 16) and $000000FF) + '.' +
IntToStr((liTemp shr 8) and $000000FF) + '.' +
IntToStr(liTemp and $000000FF);

end;
 
function my_ip_address:longint;
const
bufsize=255;
var
buf: pointer;
RemoteHost : PHostEnt; (* No, don't free it! *)
begin
buf:=NIL;
try
getmem(buf,bufsize);
winsock.gethostname(buf,bufsize); (* this one maybe without domain *)
RemoteHost:=Winsock.GetHostByName(buf);
if RemoteHost=NIL then
my_ip_address:=winsock.htonl($07000001) (* 127.0.0.1 *)
else
my_ip_address:=longint(pointer(RemoteHost^.h_addr_list^)^);
finally
if buf<>NIL then freemem(buf,bufsize);

end;
result:=winsock.ntohl(result);
end;
不知道行不行
 
定义一个函数,下面这个函数的返回值就是你要的ip地址
function TForm1.LocalIP : string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;
begin
WSAStartup($101, GInitData);
Result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^ <> nil do begin
result:=StrPas(inet_ntoa(pptr^^));
Inc(I);
end;
WSACleanup;
end;
 
查注册表,在
HKEY_LOCAL_MACHINE/System/CurrentControlSet/Services/Class/NetTrans
有些信息你可能会感兴趣
 
function my_ip_address:longint;
const
bufsize=255;
var
buf: pointer;
RemoteHost : PHostEnt; (* No, don't free it! *)
begin
buf:=NIL;
try
getmem(buf,bufsize);
winsock.gethostname(buf,bufsize); (* this one maybe without domain *)
RemoteHost:=Winsock.GetHostByName(buf);
if RemoteHost=NIL then
my_ip_address:=winsock.htonl($07000001) (* 127.0.0.1 *)
else
my_ip_address:=longint(pointer(RemoteHost^.h_addr_list^)^);
finally
if buf<>NIL then freemem(buf,bufsize);
end;
result:=winsock.ntohl(result);
end;
 
最简单的方法,使用inetdetector控件,
其中有一个online是检测是否在线,另外还有一个(叫什么我忘了)属性就是本机地址,未
连网时为127.0.0.1.,很多地方有下载,具体提供一个下载地址:
http://bcbdev.myetang.com/kjtt/download/inetdetector.zip
 
接受答案了.
 
这个问题其实太简单了,你只要在窗体上放一 TNMFTP组件,按纽的Onclick
为label1.caption:=localip;
 
后退
顶部