//获得本地网络的计算机列表
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
StaticText1: TStaticText;
ComboBox1: TComboBox;
ListBox1: TListBox;
Label1: TLabel;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function GetWorkGroupList(var nList : TStringList ) : Boolean;
function GetComputerList(nWorkGroup:string ;var nList:TStringList):Boolean;
implementation
uses About;
{$R *.DFM}
type
TNetResourceArray = ^TNetResource;
// ===============================================
// 功能说明:列举出整个网络中的工作组名称
// 传入参数:nList中返回工作组的名称列表
// 返回参数:TRUE表示执行成功
// ===============================================
function GetworkgroupList( var nList : TStringList ) : Boolean;
var
NetResource : TNetResource;
Buf : Pointer;
Count,BufSize,Res : DWORD;
lphEnum : THandle;
p:TNetResourceArray;
i,j : SmallInt;
NetworkTypeList : TList;
begin
Result := False;
NetworkTypeList := TList.Create;
nList.Clear;
Res := WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, nil,lphEnum);
if Res <> NO_ERROR then exit;
Count := $FFFFFFFF;
BufSize := 8192;
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;
Res:= WNetCloseEnum(lphEnum);
if Res <> NO_ERROR then exit;
for J := 0 to NetworkTypeList.Count-1 do
begin
NetResource := TNetResource(NetworkTypeList.Items[J]^);
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
if Res <> NO_ERROR then break;
while true do
begin
Count := $FFFFFFFF;
BufSize := 8192;
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
nList.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;
// ===============================================
// 功能说明:获得一个工作组内的所有计算机
// 传入参数:nWorkGroup(工作组名),nList中返回计算机的名称列表
// 返回参数:TRUE表示执行成功
// ===============================================
function GetComputerList(nWorkGroup:string ;var nList:TStringList):Boolean;
var
NetResource:TNetResource;
Buf : Pointer;
Count,BufSize,Res : DWord;
Ind : Integer;
lphEnum : THandle;
Temp:TNetResourceArray;
begin
Result := False;
nList.Clear;
FillChar(NetResource, SizeOf(NetResource), 0);
NetResource.lpRemoteName := @nWorkGroup[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;
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
nList.Add(Temp^.lpRemoteName);
Inc(Temp);
end;
end;
Res := WNetCloseEnum(lphEnum);
if Res <> NO_ERROR then exit;
Result:=True;
FreeMem(Buf);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
List:TstringList;
begin
try
List:=Tstringlist.create;
if GetworkgroupList(List) then
ComboBox1.Items.Assign(List);
finally
List.Free;
end;
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
var
List:TstringList;
begin
try
List:=TstringList.Create;
if GetComputerList(ComboBox1.Text,List) then
ListBox1.Items.Assign(List);
finally
List.Free;
end;
end;
end.