比较慢,我用线程做的。希望对你有所帮助:
PStrings = ^TStrings;
TThrdGetLocalPC = class(TThread)
private
{ Private declarations }
FhWnD: HWND;
FpssPCInfo: PStrings;
function GetGroupList(assGroupNames: TStrings): Boolean;
function GetUserList(asGroupName: string; assPCNames: TStrings): Boolean;
protected
procedure Execute; override;
public
constructor Create(aHWND: HWND; apssPCInfo: PStrings);
destructor Destroy; override;
published
end;
//--------------------------------------------------------------
{ TThrdGetLocalPC }
constructor TThrdGetLocalPC.Create(aHWND: HWND; apssPCInfo: PStrings);
begin
FhWnD := aHWND;
FpssPCInfo := apssPCInfo;
FreeOnTerminate := True;
Inherited Create(False);
end;
destructor TThrdGetLocalPC.Destroy;
begin
SendMessage(FhWnD, WM_PCINFO_THRD_END, 0, 0);
inherited;
end;
procedure TThrdGetLocalPC.Execute;
var
i: Integer;
ssGroupList: TStrings;
ssaUserList: array of TStrings;
begin
FpssPCInfo^.Clear;
ssGroupList := TStringList.Create;
try
GetGroupList(ssGroupList);
SetLength(ssaUserList, ssGroupList.Count);
for i := 0 to ssGroupList.Count - 1 do
begin
if Terminated then Exit;
ssaUserList
:= TStringList.Create;
GetUserList(ssGroupList, ssaUserList);
FpssPCInfo^.AddObject(ssGroupList, ssaUserList);
end;
finally
ssGroupList.Free;
end;
end;
//列举出整个网络中的工作组名称,返回值为TRUE表示执行成功,
//参数List中返回服务器(工作组)的名称
function TThrdGetLocalPC.GetGroupList(assGroupNames: TStrings): Boolean;
var
NetResource : TNetResource;
Buf : Pointer;
Count,BufSize,Res : DWORD;
lphEnum : THandle;
p NetResource;
i,j : SmallInt;
NetworkTypeList : TList;
begin
Result := False;
NetworkTypeList := TList.Create;
assGroupNames.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 := PNetResource(Buf);
for I := 0 to Count - 1 do//记录各个网络类型的信息
begin
NetworkTypeList.Add(p);
Inc(P);
end;
//WNetCloseEnum关闭一个列举句柄
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
if Terminated then Exit;
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 := PNetResource(Buf);
for I := 0 to Count - 1 do//列举各个工作组的信息
begin
assGroupNames.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;
function TThrdGetLocalPC.GetUserList(asGroupName: string;
assPCNames: TStrings): Boolean;
var
NetResource: TNetResource;
Buf: Pointer;
Count,BufSize,Res: DWord;
Ind: Integer;
lphEnum: THandle;
Temp: PNetResource;
begin
Result := False;
Buf := nil;
assPCNames.Clear;
FillChar(NetResource, SizeOf(NetResource), 0);//初始化网络层次信息
NetResource.lpRemoteName := @asGroupName[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
if Terminated then Exit;
Count := $FFFFFFFF;//不限资源数目
BufSize := 8192;//缓冲区大小设置为8K
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 := PNetResource(Buf);
for Ind := 0 to Count - 1 do//列举工作组的计算机名称
begin
//获取工作组的计算机名称,+2表示删除"/",如//wangfajun=>wangfajun
assPCNames.Add(Temp^.lpRemoteName + 2);
Inc(Temp);
end;
end;
Res := WNetCloseEnum(lphEnum);//关闭一次列举
if Res <> NO_ERROR then Exit;//执行失败
Result := True;
FreeMem(Buf);
end;