如何获取本机的IP地址?(100分)

  • 主题发起人 主题发起人 lilixin
  • 开始时间 开始时间
L

lilixin

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在拨号连接成功(接入服务器之后)后获取本机的IP地址?
 
抢分喽,这种问题在大富翁上提过千百遍了。
转贴自大富翁某处(忘了是谁的了):
function 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;
 
(ipconfig) or (winipcfg)
 
uses Winsock;


...


function getIPs: TStrings;

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:=TStringList.Create;

Result.Clear;

GetHostName(Buffer

SizeOf(Buffer));

phe := GetHostByName(buffer);

if phe = nil then

begin

Exit;

end;

pPtr := PaPInAddr(phe^.h_addr_list);

I := 0;

while pPtr^ <> nil do

begin

Result.Add(inet_ntoa(pptr^^));

Inc(I);

end;

WSACleanup;

end;



procedure TForm1.Button1Click(Sender: TObject);

begin

memo1.Lines:=GetIps;

end;
 
用winipcfg来查看PPP适配器的地址就是你的ip地址了!
 


Get myIP address




uses Winsock;


...


function getIPs: TStrings;

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:=TStringList.Create;

Result.Clear;

GetHostName(Buffer

SizeOf(Buffer));

phe := GetHostByName(buffer);

if phe = nil then

begin

Exit;

end;

pPtr := PaPInAddr(phe^.h_addr_list);

I := 0;

while pPtr^ <> nil do

begin

Result.Add(inet_ntoa(pptr^^));

Inc(I);

end;

WSACleanup;

end;



procedure TForm1.Button1Click(Sender: TObject);

begin

memo1.Lines:=GetIps;

end;
 
迟了一点
 
我也来分一杯羹
 
uses Winsock;


...


function getIPs: TStrings;

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:=TStringList.Create;

Result.Clear;

GetHostName(Buffer

SizeOf(Buffer));

phe := GetHostByName(buffer);

if phe = nil then

begin

Exit;

end;

pPtr := PaPInAddr(phe^.h_addr_list);

I := 0;

while pPtr^ <> nil do

begin

Result.Add(inet_ntoa(pptr^^));

Inc(I);

end;

WSACleanup;

end;



procedure TForm1.Button1Click(Sender: TObject);

begin

memo1.Lines:=GetIps;

end;
 
通过SOCK的方法,可以得到本机所有的IP,但是我无法区分出哪个是PPP,哪个是网卡的,
用这个方法,我在2000与98在调试,2000中最后一个IP是PPP的,98中第一个IP是PPP的,
所以,要取PPP的IP,最好还是用RAS函数
RasGetConnectStatus:function(HRASConn: THRASConn;lpRasConnStatus : PRASConnStatus): DWORD; stdcall;
RasEnumConnections : function(pRasConn : PRASConn;pCB : PDWord;pcConnections : PDWord) : DWORD; stdcall;
RasGetProjectionInfo:function(hrasconn:tHRASCONN ;
rasprojection:dword;
lpprojection:PTRASPPPIP;
lpcb:pdword ) : DWORD; stdcall;
 
多人接受答案了。
 
后退
顶部