怎么遍历网络邻接上的所以计算机?(50分)

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

zbird

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么遍历网络邻接上的所以计算机?
随便问一下,怎么得到消息发送来源。
自己编的消息发送软件发的
 
下面的例子可以遍历和你同一网段的所有计算机,既可得工作组又可得用户名和ip,
你自己加个TListView就行了

procedure TForm1.FindIpClick(Sender: TObject);
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
lpVerInfo : OSVERSIONINFO;

NetRes,NetRes_User : Array[0..9] of TNetResource;
ErrCode:integer;
EnumHandle : THandle;
I,J,K,Group_No,Total : Integer;
EnumEntries : DWord;
BufferSize : DWord;

phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
GInitData : TWSADATA;
IpAddress,GroupName,UserName,lpProvider : string;
p :pchar;
ListItem: TListItem;
begin
StatusBar1.Panels[0].Text := '正在查询所有网上邻居......';
delay(1);
Total := 0;
With NetRes[0] do
begin
dwScope :=RESOURCE_GLOBALNET;
dwType :=RESOURCETYPE_ANY;
dwDisplayType :=RESOURCEDISPLAYTYPE_DOMAIN;
dwUsage :=RESOURCEUSAGE_CONTAINER;
lpLocalName :=NIL;
lpRemoteName :=NIL;
lpComment :=NIL;
lpProvider :=NIL;
end;
ErrCode:=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,@NetRes[0],EnumHandle);
If ErrCode = NO_ERROR then
begin
EnumEntries:=100;
BufferSize:=SizeOf(NetRes);
//查找组
ErrCode:=WNetEnumResource(EnumHandle,EnumEntries,@NetRes[0],BufferSize);
lpProvider := NetRes[0].lpProvider;
WNetCloseEnum(EnumHandle);
with ListView1 do
begin
Items.Clear;
Group_No := 0;
//组

//由于NT或2000多了一个"Microsoft Windows 网络"组,因此要多一次查询组操作
//如果是win9X,下面这段不要
lpVerInfo.dwOSVersionInfoSize := SizeOf(lpVerInfo);
GetVersionEx(lpVerInfo);
if (lpVerInfo.dwPlatformId = VER_PLATFORM_WIN32_NT) then
begin
ErrCode:=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,
RESOURCEUSAGE_CONTAINER,@NetRes[Group_No],EnumHandle);
EnumEntries:=1024;
BufferSize:=SizeOf(NetRes_User);
ErrCode:=WNetEnumResource(EnumHandle,EnumEntries,@NetRes[Group_No],BufferSize);
end;
//

while ((NetRes[Group_No].lpRemoteName<>nil) and (NetRes[Group_No].lpProvider = lpProvider)) do //遍历组
begin
//组用户
GroupName := NetRes[Group_No].lpRemoteName;
ListItem := Items.Add;
Listitem.Caption := GroupName;
ErrCode:=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,
RESOURCEUSAGE_CONTAINER,@NetRes[Group_No],EnumHandle);
EnumEntries:=1024;
BufferSize:=SizeOf(NetRes_User);
ErrCode:=WNetEnumResource(EnumHandle,EnumEntries,@NetRes_User[0],BufferSize);
IF ErrCode=No_Error then
begin
I:=0;
while ((NetRes_User.lpRemoteName<>nil) and (NetRes_User.lpProvider = lpProvider)) do //遍历组用户
begin
UserName := Copy(NetRes_User.lpRemoteName,3,Length(NetRes_User.lpRemoteName));
p := PChar(UserName);
For J:=0 to Length(UserName)-1 do
Buffer[J] := p[J];
Buffer[J] := #0;
//查该机器的ip
WSAStartup($101, GInitData);
IpAddress := '';
phe :=GetHostByName(Buffer);
if phe = nil then break;
ListItem.SubItems.Add(UserName);
pptr := PaPInAddr(Phe^.h_addr_list);
K := 0;
while pptr^[K] <> nil do
begin
IpAddress := StrPas(inet_ntoa(pptr^[K]^));
ListItem.SubItems.Add(IpAddress);
Inc(K);
end;
ListItem.SubItems.Add(NetRes_User.lpComment);
WSACleanup;

I:=I+1;
Total := Total + 1;
ListItem := Items.Add;
Listitem.Caption := '';
end;
end;
ListView1.Items.Delete(ListView1.Items.IndexOf(ListItem));
Group_No := Group_No + 1;
end;
end;
end;
StatusBar1.Panels[0].Text :='共找到' + IntToStr(Total) + '个用户';
end;
你还要在uses里加上WinSock。
 
http://www.tommstudio.com/ 上有这样的例子,也有控件。
 
多人接受答案了。
 

Similar threads

后退
顶部