没有测试过,仅供楼主参考。
ugvanxk (2002-08-22 18:07:00)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TNetResourceArray = ^TNetResource; //网络资源类型的数组
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Memo1: TMemo;
StaticText1: TStaticText;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
Function GetUserResource( UserName : string ; var List : TStringList ) : Boolean;
Var
NetResource : TNetResource;
Buf : Pointer;
Count,BufSize,Res : DWord;
Ind : Integer;
lphEnum : THandle;
Temp : TNetResourceArray;
Begin
Result := False;
List.Clear;
FillChar(NetResource, SizeOf(NetResource), 0); //初始化网络层次信息
NetResource.lpRemoteName := @UserName[1]; //指定计算机名称
Res := WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_ANY,RESOURCEUSAGE_CONNECTABLE, @NetResource,lphEnum);
//获取指定计算机的网络资源句柄
If Res <> NO_ERROR Then exit; //执行失败
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 Then break;//资源列举完毕
If (Res <> NO_ERROR) then Exit; //执行失败
Temp := TNetResourceArray(Buf);
For Ind := 0 to Count - 1 do
Begin
List.Add(Temp^.lpRemoteName);
Inc(Temp);
End;
End;
Res := WNetCloseEnum(lphEnum); //关闭一次列举
If Res <> NO_ERROR Then exit; //执行失败
Result := True;
FreeMem(Buf);
End;
procedure TForm1.Button1Click(Sender: TObject);
var
List:TstringList;
i:integer;
begin
try
List:=TstringList.Create;
if GetUserResource(edit1.text,List) then
if List.count=0 then //指定计算机下没有找到共享资源
begin
memo1.Lines.Add (edit1.text+'下没有找到共享资源!');
end
else
memo1.Lines.Add (edit1.text+'下的所有共享资源如下:');
for i:=0 to List.Count-1 do
begin
Memo1.lines.Add (List.strings);
end;
finally
List:=TstringList.Create; //如有异常则释放分配的资源
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Lines .clear;
end;
end.