2005/10/10 | 探测共享
类别(语言类学习笔记) | 评论(0) | 阅读(125) | 发表于 10:05
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ShellApi;

type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }

end;

var
Form1: TForm1;
mydir:string;



implementation
uses unit2;
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
Threads: array[0..9] of TSearchFileThread;
begin
mydir:=Edit1.Text;
//for i := Low(Threads) to High(Threads) do
begin
//Threads[i] := TSearchFileThread.Create(False);
Threads[0] := TSearchFileThread.Create(False);
end;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
ShellExecute(0,nil,PChar(ListBox1.Items.Strings[ListBox1.ItemIndex]),nil,nil,SW_SHOW);
end;

end.


unit Unit2;

interface

uses
Classes,Windows,Messages,SysUtils, Graphics, Controls,unit1;

type
TSearchFileThread = class(TThread)
private
{ Private declarations }
s:string;
procedure AddFile;
protected
procedure Execute; override;
end;

implementation


procedure TSearchFileThread.AddFile;
begin
Form1.ListBox1.Items.Add(s);
end;


procedure TSearchFileThread.Execute;
Var
EnumHandle : THandle;
FileRS : TNetResource;
Buf : Array[1..500] of TNetResource;
BufSize : DWord;
Entries : DWord;
Result : Integer;
begin
mydir := mydir + #0;//mydir为工作组名时可以得到组内所有主机名
//mydir为主机名时可以得到机内所有共享文件夹名
FillChar(FileRS, SizeOf(FileRS) , 0);
With FileRS do begin
dwScope := 2;
dwType := 3;
dwDisplayType := 1;
dwUsage := 2;
lpRemoteName := @mydir[1];
end;
WNetOpenEnum( RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
0,
@FileRS,
EnumHandle );
Repeat
Entries := 1;
BufSize := SizeOf(Buf);
Result := WNetEnumResource( EnumHandle,Entries,@Buf,BufSize );
If (Result = NO_ERROR) and (Entries = 1) then
begin
s:= StrPas(Buf[1].lpRemoteName); //得到网上资源名,主机或文件夹
Synchronize(AddFile); //Form1.ListBox1.Items.Add(s);
end ;
Until (Entries <> 1) or (Result <> NO_ERROR);
WNetCloseEnum( EnumHandle );
end;

end.
0

评论Comments