测试代码如下,你自已看一下吧 ^_^
------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ImgList;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
TreeView1: TTreeView;
Button2: TButton;
ImageList1: TImageList;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure TreeView1Click(Sender: TObject);
private
{ Private declarations }
Function GetGroupList( var List : TStringList ) : Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//自定义列举函数
Function TForm1.GetGroupList( 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;
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;
//列举过程实例...
procedure TForm1.Button1Click(Sender: TObject);
var WorkGroup : TStringList;
i : integer;
begin
WorkGroup:=TStringList.Create ;
try
if GetGroupList(WorkGroup) then begin
memo1.Clear ;
TreeView1.Items.Clear ;
for i:=0 to WorkGroup.Count-1 do begin
memo1.Lines.Add(WorkGroup.Strings);
TreeView1.Items.Add(nil,WorkGroup.Strings);
end;
WorkGroup.Free ;
end;
except
WorkGroup.Free ;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var WorkGroup, HostList : TStringList;
i,j : integer;
Node,SubNode: TTreeNode;
begin
WorkGroup:=TStringList.Create ;
WorkGroup.Add('1111');
WorkGroup.Add('2222');
WorkGroup.Add('3333');
WorkGroup.Add('4444');
WorkGroup.Add('5555');
HostList:=TStringList.Create ;
HostList.Add('aaa');
HostList.Add('bbb');
HostList.Add('ccc');
HostList.Add('ddd');
HostList.Add('eee');
//---------------------------------------------------------------------------
for i:=0 to WorkGroup.Count-1 do begin
memo1.Lines.Add(WorkGroup.Strings);
Node:=TreeView1.Items.Add(nil,WorkGroup.Strings); //根结点
Node.ImageIndex :=1;
Node.SelectedIndex :=1;
for j:=0 to HostList.Count-1 do begin
SubNode:=TreeView1.Items.AddChild(Node,HostList.Strings[j]);
SubNode.ImageIndex :=2;
SubNode.SelectedIndex :=2;
end;
end;
//---------------------------------------------------------------------------
WorkGroup.Free;
HostList.Free ;
end;
procedure TForm1.TreeView1Click(Sender: TObject);
var node, chNode : Ttreenode;
begin
Node:=TreeView1.Selected ;
if Node.Index<0 then exit;
edit1.Text :='Content: '+Node.Text ;
chNode:=Node.getFirstChild ;
if chNode<>Nil then
Edit1.Text :=Edit1.Text +' Sub: '+chNode.Text ;
end;
end.