获取IP地址(100分)

  • 主题发起人 主题发起人 xuzh9
  • 开始时间 开始时间
X

xuzh9

Unregistered / Unconfirmed
GUEST, unregistred user!
请教:如何获取与本机连接的所有机器的IP地址和名称,并把它们显示在LIST列表框中??
 
这样很困难吧,看看防火墙的原理[:)]
 
给你参考:
unit GetDnsList;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TIPMainForm = class(TForm)
Info: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Procedure GetNetworkParameters;
Procedure GetAdapterInformation;
end;

var
IPMainForm: TIPMainForm;
aDnsList:TStrings;
implementation

{$R *.DFM}

Const
MAX_HOSTNAME_LEN = 128; { from IPTYPES.H }
MAX_DOMAIN_NAME_LEN = 128;
MAX_SCOPE_ID_LEN = 256;
MAX_ADAPTER_NAME_LENGTH = 256;
MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
MAX_ADAPTER_ADDRESS_LENGTH = 8;

Type
TIPAddressString = Array[0..4*4-1] of Char;

PIPAddrString = ^TIPAddrString;
TIPAddrString = Record
Next : PIPAddrString;
IPAddress : TIPAddressString;
IPMask : TIPAddressString;
Context : Integer;
End;

PFixedInfo = ^TFixedInfo;
TFixedInfo = Record { FIXED_INFO }
HostName : Array[0..MAX_HOSTNAME_LEN+3] of Char;
DomainName : Array[0..MAX_DOMAIN_NAME_LEN+3] of Char;
CurrentDNSServer : PIPAddrString;
DNSServerList : TIPAddrString;
NodeType : Integer;
ScopeId : Array[0..MAX_SCOPE_ID_LEN+3] of Char;
EnableRouting : Integer;
EnableProxy : Integer;
EnableDNS : Integer;
End;

PIPAdapterInfo = ^TIPAdapterInfo;
TIPAdapterInfo = Record { IP_ADAPTER_INFO }
Next : PIPAdapterInfo;
ComboIndex : Integer;
AdapterName : Array[0..MAX_ADAPTER_NAME_LENGTH+3] of Char;
Description : Array[0..MAX_ADAPTER_DESCRIPTION_LENGTH+3] of Char;
AddressLength : Integer;
Address : Array[1..MAX_ADAPTER_ADDRESS_LENGTH] of Byte;
Index : Integer;
_Type : Integer;
DHCPEnabled : Integer;
CurrentIPAddress : PIPAddrString;
IPAddressList : TIPAddrString;
GatewayList : TIPAddrString;
DHCPServer : TIPAddrString;
HaveWINS : Bool;
PrimaryWINSServer : TIPAddrString;
SecondaryWINSServer : TIPAddrString;
LeaseObtained : Integer;
LeaseExpires : Integer;
End;

Function GetNetworkParams(FI : PFixedInfo; Var BufLen : Integer) : Integer;
StdCall; External 'iphlpapi.dll' Name 'GetNetworkParams';

Function GetAdaptersInfo(AI : PIPAdapterInfo; Var BufLen : Integer) : Integer;
StdCall; External 'iphlpapi.dll' Name 'GetAdaptersInfo';

procedure TIPMainForm.GetAdapterInformation;
Var
AI,Work : PIPAdapterInfo;
Size : Integer;
Res : Integer;
I : Integer;

Function MACToStr(ByteArr : PByte; Len : Integer) : String;
Begin
Result := '';
While (Len > 0) do Begin
Result := Result+IntToHex(ByteArr^,2)+'-';
ByteArr := Pointer(Integer(ByteArr)+SizeOf(Byte));
Dec(Len);
End;
SetLength(Result,Length(Result)-1); { remove last dash }
End;

Function GetAddrString(Addr : PIPAddrString) : String;
Begin
Result := '';
While (Addr <> nil) do Begin
Result := Result+'A: '+Addr^.IPAddress+' M: '+Addr^.IPMask+#13;
Addr := Addr^.Next;
End;
End;

Function TimeTToDateTimeStr(TimeT : Integer) : String;
Const UnixDateDelta = 25569; { days between 12/31/1899 and 1/1/1970 }
Var
DT : TDateTime;
TZ : TTimeZoneInformation;
Res : DWord;

Begin
If (TimeT = 0) Then Result := ''
Else Begin
{ Unix TIME_T is secs since 1/1/1970 }
DT := UnixDateDelta+(TimeT / (24*60*60)); { in UTC }
{ calculate bias }
Res := GetTimeZoneInformation(TZ);
If (Res = TIME_ZONE_ID_INVALID) Then RaiseLastWin32Error;
If (Res = TIME_ZONE_ID_STANDARD) Then Begin
DT := DT-((TZ.Bias+TZ.StandardBias) / (24*60));
Result := DateTimeToStr(DT)+' '+WideCharToString(TZ.StandardName);
End
Else Begin { daylight saving time }
DT := DT-((TZ.Bias+TZ.DaylightBias) / (24*60));
Result := DateTimeToStr(DT)+' '+WideCharToString(TZ.DaylightName);
End;
End;
End;

begin
Size := 5120;
GetMem(AI,Size);
Res := GetAdaptersInfo(AI,Size);
If (Res <> ERROR_SUCCESS) Then Begin
SetLastError(Res);
RaiseLastWin32Error;
End;
With Info,Lines do Begin
Work := AI;
I := 1;
Repeat
Add('');
Add('Adapter '+IntToStr(I));
Add(' ComboIndex: '+IntToStr(Work^.ComboIndex));
Add(' Adapter name: '+Work^.AdapterName);
Add(' Description: '+Work^.Description);
Add(' Adapter address: '+MACToStr(@Work^.Address,Work^.AddressLength));
Add(' Index: '+IntToStr(Work^.Index));
Add(' Type: '+IntToStr(Work^._Type));
Add(' DHCP: '+IntToStr(Work^.DHCPEnabled));
Add(' Current IP: '+GetAddrString(Work^.CurrentIPAddress));
Add(' IP addresses: '+GetAddrString(@Work^.IPAddressList));
Add(' Gateways: '+GetAddrString(@Work^.GatewayList));
Add(' DHCP servers: '+GetAddrString(@Work^.DHCPServer));
Add(' Has WINS: '+IntToStr(Integer(Work^.HaveWINS)));
Add(' Primary WINS: '+GetAddrString(@Work^.PrimaryWINSServer));
Add(' Secondary WINS: '+GetAddrString(@Work^.SecondaryWINSServer));
Add(' Lease obtained: '+TimeTToDateTimeStr(Work^.LeaseObtained));
Add(' Lease expires: '+TimeTToDateTimeStr(Work^.LeaseExpires));
Inc(I);
Work := Work^.Next;
Until (Work = nil);
End;
FreeMem(AI);
end;

procedure TIPMainForm.GetNetworkParameters;
Var
FI : PFixedInfo;
Size : Integer;
Res : Integer;
I : Integer;
DNS : PIPAddrString;

begin
Size := 1024;
GetMem(FI,Size);
Res := GetNetworkParams(FI,Size);
If (Res <> ERROR_SUCCESS) Then Begin
SetLastError(Res);
RaiseLastWin32Error;
End;
With Info do Begin
Clear;
Lines.Add('Host name: '+FI^.HostName);
Lines.Add('Domain name: '+FI^.DomainName);
aDnsList.Clear;
If (FI^.CurrentDNSServer <> nil) Then
begin
Lines.Add('Current DNS Server: '+FI^.CurrentDNSServer^.IPAddress);
end
Else Lines.Add('Current DNS Server: (none)');
I := 1;
DNS := @FI^.DNSServerList;
Repeat
Lines.Add('DNS '+IntToStr(I)+': '+DNS^.IPAddress);
aDnsList.Add(DNS^.IPAddress);
Inc(I);
DNS := DNS^.Next;
Until (DNS = nil);
Lines.Add('Scope ID: '+FI^.ScopeId);
Lines.Add('Routing: '+IntToStr(FI^.EnableRouting));
Lines.Add('Proxy: '+IntToStr(FI^.EnableProxy));
Lines.Add('DNS: '+IntToStr(FI^.EnableDNS));
End;
FreeMem(FI);
end;

procedure TIPMainForm.Button1Click(Sender: TObject);
begin
GetNetworkParameters;
GetAdapterInformation;

end;

procedure TIPMainForm.FormCreate(Sender: TObject);
begin
aDnsList:=TStringList.Create;
end;

end.
 
请查阅Windows 的API命令。我曾试过,现在忘记了.待我抽抽书再说[:)]
 
来返回所有IP地址、网络掩码、广播地址和连接状态。

--------------------------------------------------------------------



unit USock;



interface



uses Windows, Winsock;



{



此函数列举出所有的TCP/IP连接,并返回一个由回车换行(CRLF)符分隔的字符串,
包含以下信息:



IP, NetMask, BroadCast-Address, Up/Down status,

Broadcast support, Loopback



如果你将这个字符串赋给TMemo(它的Memo.Lines.Text属性),你可以看到更清晰的结果。



使用此函数,你需要Win98/ME/2K, 95 OSR 2 或者NT service pack #3,因为程序会使用到WinSock 2(WS2_32.DLL)。



}



function EnumInterfaces(var sInt: string): Boolean;



{ 从Winsock 2.0导入函数WSAIOCtl -- 在Win98/ME/2K and 95 OSR2, NT srv pack #3下才有Winsock 2 }



function WSAIoctl(s: TSocket; cmd: DWORD; lpInBuffer: PCHAR; dwInBufferLen:

DWORD;

lpOutBuffer: PCHAR; dwOutBufferLen: DWORD;

lpdwOutBytesReturned: LPDWORD;

lpOverLapped: POINTER;

lpOverLappedRoutine: POINTER): Integer; stdcall; external 'WS2_32.DLL';



{ Constants taken from C header files }



const SIO_GET_INTERFACE_LIST = $4004747F;

IFF_UP = $00000001;

IFF_BROADCAST = $00000002;

IFF_LOOPBACK = $00000004;

IFF_POINTTOPOINT = $00000008;

IFF_MULTICAST = $00000010;



type sockaddr_gen = packed record

AddressIn: sockaddr_in;

filler: packed array[0..7] of char;

end;



type INTERFACE_INFO = packed record

iiFlags: u_long; // Interface flags

iiAddress: sockaddr_gen; // Interface address

iiBroadcastAddress: sockaddr_gen; // Broadcast address

iiNetmask: sockaddr_gen; // Network mask

end;



implementation



{-------------------------------------------------------------------



1. 打开Winsock

2. 创建一个socket

3. 调用WSAIOCtl获取网络连接

4. 对每个连接,获取它的IP、掩码、广播地址、状态

5. 将信息填充到一个由CDLF分隔的字符串中

6. 结束



--------------------------------------------------------------------}



function EnumInterfaces(var sInt: string): Boolean;

var s: TSocket;

wsaD: WSADATA;

NumInterfaces: Integer;

BytesReturned, SetFlags: u_long;

pAddrInet: SOCKADDR_IN;

pAddrString: PCHAR;

PtrA: pointer;

Buffer: array[0..20] of INTERFACE_INFO;

i: Integer;

begin

result := true; // Initialize

sInt := '';



WSAStartup($0101, wsaD); // Start WinSock

// You should normally check

// for errors here :)



s := Socket(AF_INET, SOCK_STREAM, 0); // Open a socket

if (s = INVALID_SOCKET) then exit;



try // Call WSAIoCtl

PtrA := @bytesReturned;

if (WSAIoCtl(s, SIO_GET_INTERFACE_LIST, nil, 0, @Buffer, 1024, PtrA, nil,

nil)

<> SOCKET_ERROR)

then

begin // If ok, find out how

// many interfaces exist



NumInterfaces := BytesReturned div SizeOf(INTERFACE_INFO);



for i := 0 to NumInterfaces - 1 do // For every interface

begin

pAddrInet := Buffer.iiAddress.addressIn; // IP ADDRESS

pAddrString := inet_ntoa(pAddrInet.sin_addr);

sInt := sInt + ' IP=' + pAddrString + ',';

pAddrInet := Buffer.iiNetMask.addressIn; // SUBNET MASK

pAddrString := inet_ntoa(pAddrInet.sin_addr);

sInt := sInt + ' Mask=' + pAddrString + ',';

pAddrInet := Buffer.iiBroadCastAddress.addressIn; // Broadcast addr

pAddrString := inet_ntoa(pAddrInet.sin_addr);

sInt := sInt + ' Broadcast=' + pAddrString + ',';



SetFlags := Buffer.iiFlags;

if (SetFlags and IFF_UP) = IFF_UP then

sInt := sInt + ' Interface UP,' // Interface up/down

else

sInt := sInt + ' Interface DOWN,';



if (SetFlags and IFF_BROADCAST) = IFF_BROADCAST then // Broadcasts

sInt := sInt + ' Broadcasts supported,' // supported or

else // not supported

sInt := sInt + ' Broadcasts NOT supported,';



if (SetFlags and IFF_LOOPBACK) = IFF_LOOPBACK then // Loopback or

sInt := sInt + ' Loopback interface'

else

sInt := sInt + ' Network interface'; // normal



sInt := sInt + #13#10; // CRLF between

// each interface

end;

end;

except

end;

//

// Close sockets

//

CloseSocket(s);

WSACleanUp;

result := false;

end;



end.
 
[:D]
我曾用过一个叫 SHED 的工具,它可根据IP安查看所有在局域网的机,正好和你的要求相反
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
816
DelphiTeacher的专栏
D
D
回复
0
查看
761
DelphiTeacher的专栏
D
后退
顶部