안녕하세요! 이승우 입니다.
다름이 아니라 회사에 네트워크 그룹이 여러개 있을때
그 네트워크 그룹별로 컴퓨터 리스트를 구하는 프로그램을
만들었는데, 이상하게 몇개가 빠져서 나오더군요.
소스를 보시고 제가 잘못한게 있다면 지적을 좀 해주시겠어요?
//--------------Source-----------------//
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, WinSock, ImgList, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
tvNetwork: TTreeView;
ImageList: TImageList;
StatusBar1: TStatusBar;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure GetNetworkComputerList; // 네트워크 환경에서 workgroup 목록
procedure GetWorkgroupList; // 네트워크 환경에서 workgroup에 속한 리소스 목록
// function FindIPAddress(Address: string; var PCName: string): Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
uses ShellAPI;
var
Computer : Array[1..500] of String[25];
ComputerCount : Integer;
WorkgroupCount : Integer;
Workgroup : Array[1..500] of String[25];
NetworkScope : DWord;
BCancel : Boolean;
procedure FindAllWorkgroups;
Var
EnumHandle : THandle;
EntireNetwork : TNetResource;
Buf : Array[1..500] of TNetResource;
BufSize : DWord;
Entries : DWord;
begin
FillChar(EntireNetwork, SizeOf(EntireNetwork) , 0);
With EntireNetwork do begin
dwScope := 2;
dwDisplayType := 6;
dwUsage := 2;
end;
WNetOpenEnum( RESOURCE_GLOBALNET,
RESOURCETYPE_DISK,
0,
@EntireNetwork,
EnumHandle );
WorkgroupCount := 0;
Repeat
Entries := 1;
BufSize := SizeOf(Buf);
WNetEnumResource( EnumHandle,
Entries,
@Buf,
BufSize );
If Entries = 1 then begin
Inc( WorkgroupCount );
Workgroup[ WorkgroupCount ] := StrPas(Buf[1].lpRemoteName);
end;
Until (Entries <> 1);
WNetCloseEnum( EnumHandle );
end;
procedure FindAllComputers(Workgroup : String);
Var
EnumHandle : THandle;
WorkgroupRS : TNetResource;
Buf : Array[1..500] of TNetResource;
BufSize : Dword;
Entries : DWord;
Result : Integer;
begin
ComputerCount := 0;
Workgroup := Workgroup + #0;
FillChar(WorkgroupRS, SizeOf(WorkgroupRS) , 0);
With WorkgroupRS do begin
dwScope := RESOURCE_GLOBALNET;
dwType := RESOURCETYPE_DISK;
dwDisplayType := 1;
dwUsage := 2;
lpRemoteName := @Workgroup[1];
end;
WNetOpenEnum( RESOURCE_GLOBALNET,
RESOURCETYPE_DISK,
0,
@WorkgroupRS,
EnumHandle );
Repeat
Entries := 1;
BufSize := SizeOf(Buf);
Result :=
WNetEnumResource( EnumHandle,
Entries,
@Buf,
BufSize );
If (Result = NO_ERROR) and (Entries = 1) then begin
Inc( ComputerCount );
Computer[ ComputerCount ] := StrPas(Buf[1].lpRemoteName);
end;
Until (Entries <> 1) or (Result <> NO_ERROR);
WNetCloseEnum( EnumHandle );
end; { Find All Computers }
procedure TForm1.GetWorkgroupList;
var
I: Integer;
begin
Screen.Cursor:= crHourGlass;
tvNetwork.Items.Clear;
FindAllWorkgroups;
for I:= Low(Workgroup) to WorkgroupCount - 1 do
begin
tvNetwork.Items.AddChild(nil, Workgroup[I]);
end;
Screen.Cursor:= crDefault;
end;
procedure TForm1.GetNetworkComputerList;
var
I, J: Integer;
Node: TTreeNode;
begin
Screen.Cursor:= crHourGlass;
StatusBar1.Panels[2].Text:= '전체 네크워크를 검색하고 있습니다. 잠시 기다려 주십시오...';
StatusBar1.Update;
BCancel:= False;
tvNetwork.Items.Clear;
tvNetwork.Items.BeginUpdate;
FindAllWorkgroups;
for I:= Low(Workgroup) to WorkgroupCount - 1 do
begin
Node:= tvnetwork.Items.AddChild(nil, Workgroup[I]);
FindAllComputers(Workgroup[I]);
for J:= Low(Computer) to ComputerCount - 1 do
begin
Application.ProcessMessages;
if BCancel then
begin
Screen.Cursor:= crDefault;
StatusBar1.Panels[2].Text:= '작업이 취소되었습니다.';
Exit;
end;
tvNetwork.Items.AddChild(Node, Copy(Computer[J], 3, Length(Computer[J])));
end;
end;
tvNetwork.Items.EndUpdate;
Screen.Cursor:= crDefault;
StatusBar1.Panels[2].Text:= '네크워크 검색이 끝났습니다.';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GetNetworkComputerList;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
NetworkScope:= RESOURCE_GLOBALNET;
BCancel:= False;
end;
end.
for J:= Low(Computer) to ComputerCount - 1 do
위의 두부분을
for I:= Low(Workgroup) to WorkgroupCount do
for J:= Low(Computer) to ComputerCount do
으로 바꾸면 된다고 장길석 님께서 말씀을 하시더군요
장길석님 다시한번 감사합니다.