下面的代码可以遍历出局域网上所有的工作组(域)、机器、ip,用了toolbar97控件
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls,WinSock, StdCtrls, TFlatEditUnit, TFlatButtonUnit, Menus,
TB97Ctls, TB97, TB97Tlwn, TB97Tlbr, ToolWin;
type
TForm1 = class(TForm)
ListView1: TListView;
MainMenu1: TMainMenu;
N1: TMenuItem;
Quit: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
IP1: TMenuItem;
About: TMenuItem;
StatusBar1: TStatusBar;
Dock971: TDock97;
Toolbar971: TToolbar97;
FindIp: TToolbarButton97;
ToolbarButton972: TToolbarButton97;
ToolbarButton971: TToolbarButton97;
procedure QuitClick(Sender: TObject);
procedure FindIpClick(Sender: TObject);
procedure AboutClick(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure N5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.DFM}
Procedure Delay (Msecs : Integer);
var
FirstTickCount : real;
begin
FirstTickCount := GetTickCount;
Repeat
Application.ProcessMessages;
Until ((GetTickCount - FirstTickCount) >= LongInt (Msecs));
end;
procedure TForm1.QuitClick(Sender: TObject);
begin
Application.Terminate;
end;
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
char;
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;
procedure TForm1.AboutClick(Sender: TObject);
begin
AboutBox.Show;
end;
procedure TForm1.N4Click(Sender: TObject);
begin
if N4.Checked then
begin
N4.Checked := False;
Dock971.Hide;
ListView1.Top := Dock971.Top;
ListView1.Height := ListView1.Height + Dock971.Height;
end
else
begin
N4.Checked := True;
Dock971.Show;
ListView1.Top := ListView1.Top + Dock971.Height;
ListView1.Height := ListView1.Height - Dock971.Height ;
end;
end;
procedure TForm1.N5Click(Sender: TObject);
begin
if N5.Checked then
begin
N5.Checked := False;
StatusBar1.Hide;
ListView1.Height := ListView1.Height + StatusBar1.Height;
end
else
begin
N5.Checked := True;
StatusBar1.Show;
ListView1.Height := ListView1.Height - StatusBar1.Height;
end;
end;
end.