如何实现点击按钮打开网络资源?(200分)

  • 主题发起人 主题发起人 xwing111
  • 开始时间 开始时间
X

xwing111

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