<br>来自:ecfan1 (2001-10-26 15:45:00) <br>//调试了一下可以运行,就是很慢.需要创建线程 FormCreate 我做了完善<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls, WinSock;<br><br>type<br> TForm1 = class(TForm)<br> ListBox1: TListBox;<br> procedure FormCreate(Sender: TObject);<br>function GetServerList(var List: TStringList): Boolean;<br>function GetUsers(GroupName: string; var List: TStringList): Boolean;<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br>function Tform1.GetServerList(var List: TStringList): Boolean;<br>type<br> TNetResourceArray = ^TNetResource; //网络类型的数组<br>var<br> NetResource: TNetResource;<br> Buf: Pointer;<br> Count, BufSize, Res: DWORD;<br> lphEnum: THandle;<br> p: TNetResourceArray;<br> i, j: SmallInt;<br> NetworkTypeList: TList;<br>begin<br> Result := False;<br> NetworkTypeList := TList.Create;<br> List.Clear;<br>//获取整个网络中的文件资源的句柄,lphEnum为返回名柄<br> Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,<br> RESOURCEUSAGE_CONTAINER, nil, lphEnum);<br> if Res <> NO_ERROR then exit; //Raise Exception(Res);//执行失败<br>//获取整个网络中的网络类型信息<br> Count := $FFFFFFFF; //不限资源数目<br> BufSize := 8192; //缓冲区大小设置为8K<br> GetMem(Buf, BufSize); //申请内存,用于获取工作组信息<br> Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);<br> if (Res = ERROR_NO_MORE_ITEMS) //资源列举完毕<br> or (Res <> NO_ERROR) //执行失败<br> then Exit;<br> P := TNetResourceArray(Buf);<br> for I := 0 to Count - 1 do //记录各个网络类型的信息<br> begin<br> NetworkTypeList.Add(p);<br> Inc(P);<br> end;<br> //WNetCloseEnum关闭一个列举句柄<br> Res := WNetCloseEnum(lphEnum); //关闭一次列举<br> if Res <> NO_ERROR then exit;<br> for J := 0 to NetworkTypeList.Count - 1 do //列出各个网络类型中的所有工作组名称<br> begin //列出一个网络类型中的所有工作组名称<br> NetResource := TNetResource(NetworkTypeList.Items[J]^); //网络类型信息<br>//获取某个网络类型的文件资源的句柄,NetResource为网络类型信息,lphEnum为返回名柄<br> Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,<br> RESOURCEUSAGE_CONTAINER, @NetResource, lphEnum);<br> if Res <> NO_ERROR then break; //执行失败<br> while true do //列举一个网络类型的所有工作组的信息<br> begin<br> Count := $FFFFFFFF; //不限资源数目<br> BufSize := 8192; //缓冲区大小设置为8K<br> GetMem(Buf, BufSize); //申请内存,用于获取工作组信息<br> //获取一个网络类型的文件资源信息,<br> Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);<br> if (Res = ERROR_NO_MORE_ITEMS) //资源列举完毕<br> or (Res <> NO_ERROR) //执行失败<br> then break;<br> P := TNetResourceArray(Buf);<br> for I := 0 to Count - 1 do //列举各个工作组的信息<br> begin<br> List.Add(StrPAS(P^.lpRemoteName)); //取得一个工作组的名称<br> Inc(P);<br> end;<br> end;<br> Res := WNetCloseEnum(lphEnum); //关闭一次列举<br> if Res <> NO_ERROR then break; //执行失败<br> end;<br> Result := True;<br> FreeMem(Buf);<br> NetworkTypeList.Destroy;<br>end;<br><br>function Tform1.GetUsers(GroupName: string; var List: TStringList): Boolean;<br>type<br> TNetResourceArray = ^TNetResource; //网络类型的数组<br>var<br> NetResource: TNetResource;<br> Buf: Pointer;<br> Count, BufSize, Res: DWord;<br> Ind: Integer;<br> lphEnum: THandle;<br> Temp: TNetResourceArray;<br>begin<br> Result := False;<br> List.Clear;<br> FillChar(NetResource, SizeOf(NetResource), 0); //初始化网络层次信息<br> NetResource.lpRemoteName := @GroupName[1]; //指定工作组名称<br> NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER; //类型为服务器(工作组)<br> NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;<br> NetResource.dwScope := RESOURCETYPE_DISK; //列举文件资源信息<br> //获取指定工作组的网络资源句柄<br> Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,<br> RESOURCEUSAGE_CONTAINER, @NetResource, lphEnum);<br> if Res <> NO_ERROR then Exit; //执行失败<br> while True do //列举指定工作组的网络资源<br> begin<br> Count := $FFFFFFFF; //不限资源数目<br> BufSize := 8192; //缓冲区大小设置为8K<br> GetMem(Buf, BufSize); //申请内存,用于获取工作组信息<br> //获取计算机名称<br> Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);<br> if Res = ERROR_NO_MORE_ITEMS then break; //资源列举完毕<br> if (Res <> NO_ERROR) then Exit; //执行失败<br> Temp := TNetResourceArray(Buf);<br> for Ind := 0 to Count - 1 do //列举工作组的计算机名称<br> begin<br> //获取工作组的计算机名称,+2表示删除"//",如//wangfajun=>wangfajun<br> List.Add(Temp^.lpRemoteName + 2);<br> Inc(Temp);<br> end;<br> FreeMem(Buf);<br> end;<br> Res := WNetCloseEnum(lphEnum); //关闭一次列举<br> if Res <> NO_ERROR then exit; //执行失败<br> Result := True;<br>end;<br><br><br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br> WSAData: TWSAData;<br> HostEnt: PHostEnt;<br> sIP: string;<br> i, j: Integer;<br> list1, list2: TStringList;<br>begin<br> WSAStartup(2, WSAData);<br> list1 := TStringList.Create;<br> list2 := TStringList.Create;<br> if GetServerList(list1) then<br> begin<br> for i := 0 to list1.Count - 1 do<br> begin<br> if getusers(list1.Strings, list2) then<br> begin<br> for j := 0 to list2.Count - 1 do<br> begin<br> hostEnt:=nil;<br> sip:='';<br> HostEnt := gethostbyname(PChar(list2.Strings[j]));<br> if HostEnt <> nil then<br> begin<br> with HostEnt^ do<br> sIP := Format('%d.%d.%d.%d', [Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);<br> end;<br> listbox1.Items.Add(list1.Strings+' : '+ list2.Strings[j]+' , '+sIP);<br> end;<br> end;<br> end;<br> end;<br> list1.Destroy;<br> list2.Destroy;<br> WSACleanup;<br>end;<br><br>end.<br><br> <br>