抄来的原码
procedure GetDomainList(TV:TTreeView);
var
a : Integer;
ErrCode : Integer;
NetRes : Array[0..1023] of TNetResource;
EnumHandle : Thandle;
EnumEntries : Dword;
BufferSize : Dword;
s : string;
itm : TTreeNode;
begin
{ Start here }
try
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;
{ get net root }
ErrCode:=WNetOpenEnum(
RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
RESOURCEUSAGE_CONTAINER,
@NetRes[0],
EnumHandle
);
If ErrCode=NO_ERROR then begin
EnumEntries:=1;
BufferSize:=SizeOf(NetRes);
ErrCode:=WNetEnumResource(
EnumHandle,
EnumEntries,
@NetRes[0],
BufferSize
);
WNetCloseEnum(EnumHandle);
ErrCode:=WNetOpenEnum(
RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
RESOURCEUSAGE_CONTAINER,
@NetRes[0],
EnumHandle
);
EnumEntries:=1024;
BufferSize:=SizeOf(NetRes);
ErrCode:=WNetEnumResource(
EnumHandle,
EnumEntries,
@NetRes[0],
BufferSize
);
IF ErrCode=No_Error then with TV do try
a:=0;
Items.BeginUpDate;
Items.Clear;
Itm:=Items.Add(TV.Selected,string(NetRes[0].lpProvider));
Itm.ImageIndex:=0;
Itm.SelectedIndex:=0;
{ get domains }
下面的一个单元定义了一个组件. TNetworkBrowser, 可以枚举hierachical树上所有
的网络资源. 实际上浏览是要花费很长时间的,这您可以通过在WINDOWS资源管理器
中打开"整个网络" 来比较一下. 如果你设置SCOPE属性 为nsContext , 你就可以看到
和网络邻居中一样的机器列表.
Unit NetBrwsr;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TNetScope = (nsConnected, nsGlobal, nsRemembered, nsContext);
TNetResourceType = (nrAny, nrDisk, nrPrint);
TNetDisplay = (ndDomain, ndGeneric, ndServer, ndShare, ndFile, ndGroup,
ndNetwork, ndRoot, ndShareAdmin, ndDirectory, ndTree, ndNDSContainer);
TNetUsage = set of (nuConnectable, nuContainer);
TNetworkItems = class;
TNetworkItem = class
private
Fscope: TNetScope;
FResourceType: TNetResourceType;
Fdisplay: TNetDisplay;
Fusage: TNetUsage;
FLocalName: string;
FRemoteName: string;
Fcomment: string;
Fprovider: string;
FSubItems: TNetworkItems;
public
constructor Create;
destructor Destroy; override;
property Scope: TNetScope read Fscope;
property ResourceType: TNetResourceType read FResourceType;
property Display: TNetDisplay read Fdisplay;
property Usage: TNetUsage read Fusage;
property LocalName: string read FLocalName;
property RemoteName: string read FRemoteName;
property Comment: string read Fcomment;
property Provider: string read Fprovider;
property SubItems: TNetworkItems read FSubItems;
end;
TNetworkItems = class
private
Flist: Tlist;
procedure SetItem(Index: Integer; Value: TNetworkItem);
function GetItem(Index: Integer): TNetworkItem;
function GetCount: Integer;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure Add(Item: TNetworkItem);
procedure Delete(Index: Integer);
property Items[Index: Integer]: TNetworkItem read GetItem write
SetItem; default;
property Count: Integer read GetCount;
end;
TNetworkBrowser = class(Tcomponent)
private
Fitems: TNetworkItems;
Fscope: TNetScope;
FResourceType: TNetResourceType;
Fusage: TNetUsage;
Factive: Boolean;
procedure Refresh;
procedure SetActive(Value: Boolean);
procedure SetScope(Value: TNetScope);
procedure SetResourceType(Value: TNetResourceType);
procedure SetUsage(Value: TNetUsage);
procedure EnumerateNet(NetItems: TNetworkItems; lpnr: PNetResource);
protected
public
constructor Create(Aowner: Tcomponent); override;
destructor Destroy; override;
procedure Open;
procedure Close;
property Items: TNetworkItems read Fitems;
published
property Scope: TNetScope read Fscope write SetScope default nsGlobal;
property ResourceType: TNetResourceType read FResourceType
write SetResourceType default nrAny;
property Usage: TNetUsage read Fusage write SetUsage default [];
property Active: Boolean read Factive write SetActive default False;
end;
implementation
type
PNetResourceArray = ^TNetResourceArray;
TNetResourceArray = array[0..0] of TNetResource;
{ TNetworkItem }
constructor TNetworkItem.Create;
begin
inherited;
FSubItems := TNetworkItems.Create;
end;
destructor TNetworkItem.Destroy;
begin
if FSubItems <> nil then
FSubItems.Free;
inherited;
end;
{ TNetworkItems }
constructor TNetworkItems.Create;
begin
inherited;
Flist := Tlist.Create;
end;
destructor TNetworkItems.Destroy;
begin
Clear;
if Flist <> nil then
Flist.Free;
inherited;
end;
procedure TNetworkItems.SetItem(Index: Integer; Value: TNetworkItem);
begin
if (Flist.Items[Index] <> nil) and (Flist.Items[Index] <> Value) then
TNetworkItem(Flist.Items[Index]).Free;
Flist.Items[Index] := Value;
end;
function TNetworkItems.GetItem(Index: Integer): TNetworkItem;
begin
Result := TNetworkItem(Flist.Items[Index]);
end;
procedure TNetworkItems.Clear;
begin
while Count > 0 do
Delete(0);
end;
procedure TNetworkItems.Add(Item: TNetworkItem);
begin
Flist.Add(Item);
end;
procedure TNetworkItems.Delete(Index: Integer);
begin
if Flist.Items[Index] <> nil then
TNetworkItem(Flist.Items[Index]).Free;
Flist.Delete(Index);
end;
function TNetworkItems.GetCount: Integer;
begin
if Flist <> nil then
Result := Flist.Count
else
Result := 0;
end;
{ TNetworkBrowser }
constructor TNetworkBrowser.Create(Aowner: Tcomponent);
begin
inherited Create(Aowner);
Fitems := TNetworkItems.Create;
Fscope := nsGlobal;
FResourceType := nrAny;
Fusage := [];
end;
destructor TNetworkBrowser.Destroy;
begin
if Fitems <> nil then
Fitems.Free;
inherited;
end;
procedure TNetworkBrowser.EnumerateNet(NetItems: TNetworkItems; lpnr:
PNetResource);
var
dwResult, dwResultEnum: Integer;
hEnum: Thandle;
cbBuffer, cEntries, I: Integer;
nrArray: PNetResourceArray;
NewItem: TNetworkItem;
dwScope, dwType, dwUsage: Integer;
begin
cbBuffer := 16384;
cEntries := $FFFFFFFF;
case Fscope of
nsConnected: dwScope := RESOURCE_CONNECTED;
nsGlobal: dwScope := RESOURCE_GLOBALNET;
nsRemembered: dwScope := RESOURCE_REMEMBERED;
nsContext: dwScope := RESOURCE_CONTEXT;
else
dwScope := RESOURCE_GLOBALNET;
end;
case FResourceType of
nrAny: dwType := RESOURCETYPE_ANY;
nrDisk: dwType := RESOURCETYPE_DISK;
nrPrint: dwType := RESOURCETYPE_PRINT;
else
dwType := RESOURCETYPE_ANY;
end;
dwUsage := 0;
if nuConnectable in Fusage then
dwUsage := dwUsage or RESOURCEUSAGE_CONNECTABLE;
if nuContainer in Fusage then
dwUsage := dwUsage or RESOURCEUSAGE_CONTAINER;
dwResult := WNetOpenEnum(dwScope, dwType, dwUsage, lpnr, hEnum);
if dwResult <> NO_ERROR then Exit;
GetMem(nrArray, cbBuffer);
repeat
dwResultEnum := WNetEnumResource(hEnum, cEntries, nrArray, cbBuffer);
if dwResultEnum = NO_ERROR then
for I := 0 to cEntries-1 do
begin
NewItem := TNetworkItem.Create;
case nrArray.dwScope of
RESOURCE_CONNECTED: NewItem.Fscope := nsConnected;
RESOURCE_GLOBALNET: NewItem.Fscope := nsGlobal;
RESOURCE_REMEMBERED: NewItem.Fscope := nsRemembered;
RESOURCE_CONTEXT: NewItem.Fscope := nsContext;
else
NewItem.Fscope := nsGlobal;
end;
case nrArray.dwType of
RESOURCETYPE_ANY: NewItem.FResourceType := nrAny;
RESOURCETYPE_DISK: NewItem.FResourceType := nrDisk;
RESOURCETYPE_PRINT: NewItem.FResourceType := nrPrint;
else
NewItem.FResourceType := nrAny;
end;
case nrArray.dwDisplayType of
RESOURCEDISPLAYTYPE_GENERIC: NewItem.Fdisplay := ndGeneric;
RESOURCEDISPLAYTYPE_DOMAIN: NewItem.Fdisplay := ndDomain;
RESOURCEDISPLAYTYPE_SERVER: NewItem.Fdisplay := ndServer;
RESOURCEDISPLAYTYPE_SHARE: NewItem.Fdisplay := ndShare;
RESOURCEDISPLAYTYPE_FILE: NewItem.Fdisplay := ndFile;
RESOURCEDISPLAYTYPE_GROUP: NewItem.Fdisplay := ndGroup;
RESOURCEDISPLAYTYPE_NETWORK: NewItem.Fdisplay := ndNetwork;
RESOURCEDISPLAYTYPE_ROOT: NewItem.Fdisplay := ndRoot;
RESOURCEDISPLAYTYPE_SHAREADMIN: NewItem.Fdisplay :=
ndShareAdmin;
RESOURCEDISPLAYTYPE_DIRECTORY: NewItem.Fdisplay :=
ndDirectory;
RESOURCEDISPLAYTYPE_TREE: NewItem.Fdisplay := ndTree;
RESOURCEDISPLAYTYPE_NDSCONTAINER: NewItem.Fdisplay :=
ndNDSContainer;
else
NewItem.Fdisplay := ndGeneric;
end;
NewItem.Fusage := [];
if nrArray.dwUsage and RESOURCEUSAGE_CONNECTABLE <> 0 then
Include(NewItem.Fusage, nuConnectable);
if nrArray.dwUsage and RESOURCEUSAGE_CONTAINER <> 0 then
Include(NewItem.Fusage, nuContainer);
NewItem.FLocalName := StrPas(nrArray.lpLocalName);
NewItem.FRemoteName := StrPas(nrArray.lpRemoteName);
NewItem.Fcomment := StrPas(nrArray.lpComment);
NewItem.Fprovider := StrPas(nrArray.lpProvider);
NetItems.Add(NewItem);
// if container, call recursively
if (nuContainer in NewItem.Fusage) and (Fscope <> nsContext) then
EnumerateNet(NewItem.FSubItems, @nrArray)
end;
until dwResultEnum = ERROR_NO_MORE_ITEMS;
FreeMem(nrArray);
WNetCloseEnum(hEnum);
end;
procedure TNetworkBrowser.Refresh;
begin
Fitems.Clear;
if Factive then
EnumerateNet(Fitems, nil);
end;
procedure TNetworkBrowser.SetActive(Value: Boolean);
begin
if Value <> Factive then
begin
Factive := Value;
Refresh;
end;
end;
procedure TNetworkBrowser.SetScope(Value: TNetScope);
begin
if Value <> Fscope then
begin
Fscope := Value;
Refresh;
end;
end;
procedure TNetworkBrowser.SetResourceType(Value: TNetResourceType);
begin
if Value <> FResourceType then
begin
FResourceType := Value;
Refresh;
end;
end;
procedure TNetworkBrowser.SetUsage(Value: TNetUsage);
begin
if Value <> Fusage then
begin
Fusage := Value;
Refresh;
end;
end;
procedure TNetworkBrowser.Open;
begin
Active := True;
end;
procedure TNetworkBrowser.Close;
begin
Active := False;
end;
end.