怎样得到局域网内所有电脑的IP?(100分)

  • 主题发起人 主题发起人 chen_ht
  • 开始时间 开始时间
C

chen_ht

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样得到局域网内所有电脑的IP?最好有源代码
 
可以先找出网内所有计算机名,然后循环通过计算机名得到IP.
 
得到指定工作组下所有计算机的代码:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Edit1: TEdit;
StaticText1: TStaticText;
Panel1: TPanel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

Function GetUsers(GroupName:string;var List:TStringList):Boolean;

implementation
{$R *.DFM}
type
TNetResourceArray = ^TNetResource; //网络资源类型的数组

Function GetUsers(GroupName:string;var List:TStringList):Boolean;
Var
NetResource:TNetResource;
Buf : Pointer;
Count,BufSize,Res : DWord;
Ind : Integer;
lphEnum : THandle;
Temp:TNetResourceArray;
Begin
Result := False;
List.Clear;
FillChar(NetResource, SizeOf(NetResource), 0); //初始化网络层次信息
NetResource.lpRemoteName := @GroupName[1]; //指定工作组名称
NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;//类型为服务器(工作组)
NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;
NetResource.dwScope := RESOURCETYPE_DISK; //列举文件资源信息
Res := WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_DISK,RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
If Res <> NO_ERROR Then Exit; //执行失败
While True Do //列举指定工作组的网络资源
Begin
Count := $FFFFFFFF; //不限资源数目
BufSize := 8192; //缓冲区大小设置为8K
GetMem(Buf, BufSize);//申请内存,用于获取工作组信息,获取计算机名称
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
If Res = ERROR_NO_MORE_ITEMS Then break;//资源列举完毕
If (Res <> NO_ERROR) then Exit;//执行失败
Temp := TNetResourceArray(Buf);
For Ind := 0 to Count - 1 do//列举工作组的计算机名称
Begin
List.Add(Temp^.lpRemoteName);
Inc(Temp);
End;
End;
Res := WNetCloseEnum(lphEnum);//关闭一次列举
If Res <> NO_ERROR Then exit;//执行失败
Result:=True;
FreeMem(Buf);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
List:TstringList;
i:integer;
begin
try
List:=TstringList.Create;
if GetUsers(edit1.text,List) then
if List.count=0 then //工作组下没找到计算机
begin
memo1.Lines.Add (edit1.text+'工作组下没有计算机!');
end
else
memo1.Lines.Add (edit1.text+'下的所有计算机如下:');
for i:=0 to List.Count-1 do
begin
Memo1.lines.Add (List.strings);
end;
finally
List:=TstringList.Create; //如有异常则释放分配的资源
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Lines.Clear ;
end;


end.
 
通过计算机名得到IP代码:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Panel1: TPanel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
WSAData: TWSAData;
HostEnt: PHostEnt;
sComputerName, sIP: string;
begin
sComputername:=edit1.text;
WSAStartup(2, WSAData);
HostEnt := gethostbyname(PChar(sComputerName));
if HostEnt <> nil then
begin
with HostEnt^ do
sIP := Format('%d.%d.%d.%d', [Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);

end;
WSACleanup;
edit2.text:=sIP;
end;
end.
 
只有它和你在同一个逻辑子网中你才能ping通或用GetHostByName得到,否则你得不到的。
 
先枚举局域网内的所有计算机名,再通过计算机名取IP地址。
 
unit fwqxz_s;

interface

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

type
Tfwqxz = class(TForm)
Panel1: TPanel;
UserList: TListBox;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
procedure FormShow(Sender: TObject);
Function GetServerList( var List : TStringList ) : Boolean;
Function GetUsers( GroupName : string; var List : TStringList ) : Boolean;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
fwqxz: Tfwqxz;

implementation

uses main_s;

{$R *.DFM}

procedure Tfwqxz.FormShow(Sender: TObject);
var list1,list2:TStringList;
i,j:Integer;
begin
i:=1;
list1:= TStringList.Create;
list2:= TStringList.Create;
if GetServerList(list1) then
begin
showmessage('执行成功');
for i:=0 to list1.Count-1 do
begin
if getusers(list1.Strings,list2) then
begin
for j:=0 to list2.Count-1 do
UserList.Items.Add(list2.Strings[j]);
end;
end;
end;
list1.Destroy;
list2.Destroy;
end;

Function Tfwqxz.GetServerList( var List : TStringList ) : Boolean;
type
TNetResourceArray = ^TNetResource;//网络类型的数组
Var
NetResource : TNetResource;
Buf : Pointer;
Count,BufSize,Res : DWORD;
lphEnum : THandle;
p : TNetResourceArray;
i,j : SmallInt;
NetworkTypeList : TList;
Begin
Result := False;
NetworkTypeList := TList.Create;
List.Clear;
//获取整个网络中的文件资源的句柄,lphEnum为返回名柄
Res := WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, Nil,lphEnum);
If Res <> NO_ERROR Then exit;//Raise Exception(Res);//执行失败
//获取整个网络中的网络类型信息
Count := $FFFFFFFF;//不限资源数目
BufSize := 8192;//缓冲区大小设置为8K
GetMem(Buf, BufSize);//申请内存,用于获取工作组信息
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
If ( Res = ERROR_NO_MORE_ITEMS )//资源列举完毕
or (Res <> NO_ERROR )//执行失败
Then Exit;
P := TNetResourceArray(Buf);
For I := 0 To Count - 1 Do//记录各个网络类型的信息
Begin
NetworkTypeList.Add(p);
Inc(P);
End;
//WNetCloseEnum关闭一个列举句柄
Res := WNetCloseEnum(lphEnum);//关闭一次列举
If Res <> NO_ERROR Then exit;
For J := 0 To NetworkTypeList.Count-1 Do //列出各个网络类型中的所有工作组名称
Begin//列出一个网络类型中的所有工作组名称
NetResource := TNetResource(NetworkTypeList.Items[J]^);//网络类型信息
//获取某个网络类型的文件资源的句柄,NetResource为网络类型信息,lphEnum为返回名柄
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
If Res <> NO_ERROR Then break;//执行失败
While true Do//列举一个网络类型的所有工作组的信息
Begin
Count := $FFFFFFFF;//不限资源数目
BufSize := 8192;//缓冲区大小设置为8K
GetMem(Buf, BufSize);//申请内存,用于获取工作组信息
//获取一个网络类型的文件资源信息,
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
If ( Res = ERROR_NO_MORE_ITEMS ) //资源列举完毕
or (Res <> NO_ERROR) //执行失败
then break;
P := TNetResourceArray(Buf);
For I := 0 To Count - 1 Do//列举各个工作组的信息
Begin
List.Add( StrPAS( P^.lpRemoteName ));//取得一个工作组的名称
Inc(P);
End;
End;
Res := WNetCloseEnum(lphEnum);//关闭一次列举
If Res <> NO_ERROR Then break;//执行失败
End;
Result := True;
FreeMem(Buf);
NetworkTypeList.Destroy;
End;

Function Tfwqxz.GetUsers( GroupName : string; var List : TStringList ) : Boolean;
type
TNetResourceArray = ^TNetResource;//网络类型的数组
Var
NetResource : TNetResource;
Buf : Pointer;
Count,BufSize,Res : DWord;
Ind : Integer;
lphEnum : THandle;
Temp : TNetResourceArray;
Begin
Result := False;
List.Clear;
FillChar(NetResource, SizeOf(NetResource), 0);//初始化网络层次信息
NetResource.lpRemoteName := @GroupName[1];//指定工作组名称
NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;//类型为服务器(工作组)
NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;
NetResource.dwScope := RESOURCETYPE_DISK;//列举文件资源信息
//获取指定工作组的网络资源句柄
Res := WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
If Res <> NO_ERROR Then Exit; //执行失败
While True Do//列举指定工作组的网络资源
Begin
Count := $FFFFFFFF;//不限资源数目
BufSize := 8192;//缓冲区大小设置为8K
GetMem(Buf, BufSize);//申请内存,用于获取工作组信息
//获取计算机名称
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
If Res = ERROR_NO_MORE_ITEMS Then break;//资源列举完毕
If (Res <> NO_ERROR) then Exit;//执行失败
Temp := TNetResourceArray(Buf);
For Ind := 0 to Count - 1 do//列举工作组的计算机名称
Begin
//获取工作组的计算机名称,+2表示删除"//",如//wangfajun=>wangfajun
List.Add(Temp^.lpRemoteName + 2);
Inc(Temp);
End;
End;
Res := WNetCloseEnum(lphEnum);//关闭一次列举
If Res <> NO_ERROR Then exit;//执行失败
Result := True;
FreeMem(Buf);
End;

procedure Tfwqxz.BitBtn1Click(Sender: TObject);
begin
main.Edit1.Text:=UserList.Items.Strings[UserList.itemIndex];
fwqxz.Close;
end;

procedure Tfwqxz.BitBtn2Click(Sender: TObject);
begin
fwqxz.Close;
end;

procedure Tfwqxz.FormCreate(Sender: TObject);
var list1,list2:TStringList;
i,j:Integer;
begin
i:=1;
list1:= TStringList.Create;
list2:= TStringList.Create;
if GetServerList(list1) then
begin
for i:=0 to list1.Count-1 do
begin
if getusers(list1.Strings,list2) then
begin
for j:=0 to list2.Count-1 do
UserList.Items.Add(list2.Strings[j]);
end;
end;
end;
list1.Destroy;
list2.Destroy;
end;

end.
 
谢谢,问题解决了。
 

Similar threads

后退
顶部