• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 477
  • Last Modified:

Enum all computers in domain or workgroup


i like to be able to enum all connected network computers into , let's say
ListBox or ListView with categorize.

Much Much Much points for useful answer
0
esk
Asked:
esk
  • 12
  • 9
  • 2
  • +1
1 Solution
 
intheCommented:
Hi esk,
see this demo project:

http://www.delphifreestuff.com/examples/wnetexmp.zip

Regards Barry
0
 
intheCommented:
note this may also help if you want to get logged in users and stuff:

{
  TNetUsers  VCL component for Delphi 2,3 & 4.

    Author :  Vijendra Kumar H.
              Software Engineer
              Bangalore, INDIA.
              E-Mail : vijendrah@hotmail.com

    Desc  :
              i) Retrieves all the network user names from the given = domain
                If the user don't specify the domain name, it will take = from
                the default domain(i.e. the domain to which the machine = is
                connected)
            ii) Retrieves all Servers(Domains) on the Network
 
    Note  : This component is a freeware and can be used by anyone.  If = you
            use it for a commercial products, please give credit to me. =
 
            You may modify the source code to your desire and if you = make any
            cool modifications, please send me the mods!!! :-)
 
            For getting the default domain name, I had tried a lot and = atlast
            I have followed searching the machine name in each domain.  = If anyone
            knows a better way of determining the default domain name, = please
            inform me.
 
  Initial Release  : 9th July 1998
  Mods for Delphi 4 : 15th Mar 1999
  Added GetServerList and Getting DefaultDomainName : 20th Mar 1999. }
 
Unit NetUsers;
 
Interface
 
Uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,Dialogs;

Type
  TNetUsers  Class(TComponent)
  Private
    fServer : String;
    Function GetUsers : Boolean;
  Protected
    Procedure SetServer(Server : String);
  Public
    List : TStringList;
    Constructor Create(Owner:TComponent); Override;
    Destructor Destroy; Override;
    Function GetServerList : Boolean;
    Function GetUserList : Boolean;
  Published
    Property Server :String Read fServer Write SetServer;
  End;
 
  PnetResourceArr  ^TNetResource;
 
Procedure Register;
 
Implementation
 
Procedure TNetUsers.SetServer(Server : String);
Begin
  If fServer <> Server Then
    fServer : Server;
End;
 
Constructor TNetUsers.Create(Owner:TComponent);
Begin
  Inherited Create(Owner);
  If Not (csDesigning in ComponentState) Then
  Begin
    List : TStringList.Create;
    List.Sorted : True;
  End;
End;
 
Destructor TNetUsers.Destroy;
Begin
  If Not(csDesigning In ComponentState) Then
    List.Destroy;
  Inherited Destroy;
End;
 
Function TNetUsers.GetServerList : Boolean;
Type
  {$H+}
  PMyRec  ^MyRec;
  MyRec  Record
            dwScope      : Integer;
            dwType        : Integer;
            dwDisplayType : Integer;
            dwUsage      : Integer;
            LocalName    : String;
            RemoteName    : String;
            Comment      : String;
            Provider      : String;
          End;
  {H-}
Var
  NetResource : TNetResource;
  TempRec    : PMyRec;
  Buf        : Pointer;
  Count,
  BufSize,
  Res        : DWORD;
  lphEnum    : THandle;
  p          : PNetResourceArr;
  i,
  j          : SmallInt;
  NetworkTypeList : TList;
Begin
  Result : False;
  NetworkTypeList : TList.Create;
  List.Clear;
  GetMem(Buf, 8192);
  Try
    Res : WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, = RESOURCEUSAGE_CONTAINER, Nil,lphEnum);
    If Res <> 0 Then Raise Exception(Res);
    Count : $FFFFFFFF;
    BufSize : 8192;
    Res : WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
         If Res  ERROR_NO_MORE_ITEMS Then Exit;
    If (Res <> 0) Then Raise Exception(Res);
    P : PNetResourceArr(Buf);
    For I : 0 To Count - 1 Do
    Begin
      New(TempRec);
      TempRec^.dwScope : P^.dwScope;
      TempRec^.dwType : P^.dwType ;
      TempRec^.dwDisplayType : P^.dwDisplayType ;
      TempRec^.dwUsage : P^.dwUsage ;
      TempRec^.LocalName : StrPas(P^.lpLocalName);
      TempRec^.RemoteName : StrPas(P^.lpRemoteName);
      TempRec^.Comment : StrPas(P^.lpComment);
      TempRec^.Provider : StrPas(P^.lpProvider);
      NetworkTypeList.Add(TempRec);
      Inc(P);
    End;
    Res : WNetCloseEnum(lphEnum);
    If Res <> 0 Then Raise Exception(Res);
    For J : 0 To NetworkTypeList.Count-1 Do
    Begin
      TempRec : NetworkTypeList.Items[J];
      NetResource : TNetResource(TempRec^);
      Res : WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, = RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
      If Res <> 0 Then Raise Exception(Res);
      While true Do
      Begin
        Count : $FFFFFFFF;
        BufSize : 8192;
        Res : WNetEnumResource(lphEnum, Count, Pointer(Buf), = BufSize);
        If Res  ERROR_NO_MORE_ITEMS Then Break;
        If (Res <> 0) Then Raise Exception(Res);
        P : PNetResourceArr(Buf);
        For I : 0 To Count - 1 Do
        Begin
          List.Add(P^.lpRemoteName);
          Inc(P);
        End;
      End;
    End;
    Res : WNetCloseEnum(lphEnum);
    If Res <> 0 Then Raise Exception(Res);
    Result : True;
    Finally
      FreeMem(Buf);
      NetworkTypeList.Destroy;
  End;
End;
 
Function TNetUsers.GetUsers : Boolean;
Var
  NetResource : TNetResource;
  Buf        : Pointer;
  Count,
  BufSize,
  Res        : DWord;
  Ind        : Integer;
  lphEnum    : THandle;
  Temp        : PNetResourceArr;
Begin
  Result : False;
  List.Clear;
  GetMem(Buf, 8192);
  Try
    FillChar(NetResource, SizeOf(NetResource), 0);
    NetResource.lpRemoteName : @fServer[1];
    NetResource.dwDisplayType : RESOURCEDISPLAYTYPE_SERVER;
    NetResource.dwUsage : RESOURCEUSAGE_CONTAINER;
    NetResource.dwScope : RESOURCETYPE_DISK;
    Res : WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, = RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
    If Res <> 0 Then Exit;
    While True Do
    Begin
      Count : $FFFFFFFF;
      BufSize : 8192;
      Res : WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
      If Res  ERROR_NO_MORE_ITEMS Then Exit;
      If (Res <> 0) then Exit;
      Temp : PNetResourceArr(Buf);
      For Ind : 0 to Count - 1 do
      Begin
        List.Add(Temp^.lpRemoteName + 2); { Add all the network = usernames to List StringList }
 
        Inc(Temp);
      End;
    End;
    Res : WNetCloseEnum(lphEnum);
    If Res <> 0 Then Raise Exception(Res);
    Result : True;
  Finally
    FreeMem(Buf);
  End;
End;
 
Function TNetUsers.GetUserList : Boolean;
Var
  ServerList  : TStringList;
  TempInt,
  Ind        : Integer;
  {$IFDEF VER100}
  MaxLen      : Integer;
  {$ELSE}
  MaxLen      : Cardinal;
  {$ENDIF}
  Buf        : PChar;
  MachineName : String;
Begin
  Result : False;
  If fServer  '' Then
  Begin
    If GetServerList Then
    Begin
      ServerList := List;
      GetMem(Buf,255);
      MaxLen := 255;
      GetComputerName(Buf, MaxLen);
      MachineName := StrPas(Buf);
      Freemem(Buf,255);
      For Ind := 0 to ServerList.Count-1 Do
      Begin
        fServer := ServerList.Strings[Ind];
        If GetUsers Then
          If List.Find(MachineName, TempInt) Then
          Begin
            Result : True;
            Exit;
          End;
      End;
    End;
  End
  Else
    Result : GetUsers;
End;
 
Procedure Register;
Begin
  RegisterComponents('HVK Utility', [TNetUsers]);
End;
 
End.
0
 
intheCommented:
mmm..
 there are some strange "=" signs in there to be deleted
(they come from cut n paste?)
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
MadshiCommented:
listening...
0
 
eskAuthor Commented:
Adjusted points to 400
0
 
eskAuthor Commented:
Inthe- can you fix this, i only want complet enumerating all computers categorized in ?

i raise the points to 400, and more for useful asnwer

esk
0
 
MadshiCommented:
What do you mean with "categorized"?
0
 
intheCommented:
also dont know what is "categorized in"...
0
 
eskAuthor Commented:
Adjusted points to 500
0
 
eskAuthor Commented:
i only want to enum all computers (domain / workgroup) into anything i can easily see it and work with. drag and drop from treeview to anything raise to 500
0
 
eskAuthor Commented:
that would be helpful if i can get working project via email?
0
 
intheCommented:
the example project in the first link adds them to treeview..i dont know what else you could need..

here is one to just add the names and workgroup:

procedure TForm1.Button2Click(Sender: TObject);
procedure Enum(h: THandle);
var
  nr: array[0..2000] of TNetResource;
  nRes, cbBuf : Cardinal;
  i: Integer;
  he: THandle;
begin
  nRes := 100;
  cbBuf := sizeof(nr);
  FillChar(nr, sizeof(nr), 0);
  WNetEnumResource(h, nRes, @nr, cbBuf);
  for i := 0 to nRes-1 do
  begin
    if (nr[i].dwDisplayType in [RESOURCEDISPLAYTYPE_DOMAIN,
    RESOURCEDISPLAYTYPE_DIRECTORY,RESOURCEDISPLAYTYPE_SERVER]) and Assigned(nr[i].lpRemoteName) then
     listbox1.Items.add(nr[i].lpRemoteName);
      if nr[i].dwDisplayType in [RESOURCEDISPLAYTYPE_DOMAIN,
RESOURCEDISPLAYTYPE_NETWORK] then
    begin
      WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_ALL, @nr[i], he);
      Enum(he);
      WNetCloseEnum(he);
    end;
   end;
end;
var
  he: THandle;
begin
  Listbox1.Items.Clear;
  WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY,
RESOURCEUSAGE_ALL, nil, he);
  Enum(he);
  WNetCloseEnum(he);
end;

sorry i dont know how well it works i only have 2 pc network so cant test it properly .
0
 
EpsylonCommented:
0
 
intheCommented:
hi Epsylon,
this is the same component that i pasted here ...
although the zip has a good example of its usage ;-)
cheers barry
0
 
eskAuthor Commented:
I'll test this component and the code
Barry sends me, i let you know?
0
 
eskAuthor Commented:
Barry, you get the points if you can show me how i can drag from treeview to listview or listbox, - your code add computers to listbox can you change this into treeview?

you get 700 points

ESK
0
 
eskAuthor Commented:
Barry, you get the points if you can show me how i can drag from treeview to listview or listbox, - your code add computers to listbox can you change this into treeview?

you get 700 points

ESK
0
 
eskAuthor Commented:
Barry!!!
0
 
intheCommented:
hi,
sorry ive been working very long hours not even enough time for e.e at the moment :-(
please try this i think from your last comment its what your after:
Regards Barry


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    TreeView1: TTreeView;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
    procedure ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure TreeView1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
  private
     { Private declarations }
 public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
procedure Enum(h: THandle);
var
  nr: array[0..2000] of TNetResource;
  nRes, cbBuf : Cardinal;
  i: Integer;
  he: THandle;
begin
  nRes := 100;
  cbBuf := sizeof(nr);
  FillChar(nr, sizeof(nr), 0);
  WNetEnumResource(h, nRes, @nr, cbBuf);
  for i := 0 to nRes-1 do
  begin
  if (nr[i].dwDisplayType in [RESOURCEDISPLAYTYPE_DOMAIN]) then
    treeview1.items.add(treeview1.TopItem,pchar(nr[i].lpRemoteName));
    if (nr[i].dwDisplayType in [RESOURCEDISPLAYTYPE_DIRECTORY,RESOURCEDISPLAYTYPE_SERVER]) and Assigned(nr[i].lpRemoteName) then
     treeview1.items.addchild(treeview1.TopItem,pchar(nr[i].lpRemoteName));
      if nr[i].dwDisplayType in [RESOURCEDISPLAYTYPE_DOMAIN,
RESOURCEDISPLAYTYPE_NETWORK] then
    begin
      WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_ALL, @nr[i], he);
      Enum(he);
      WNetCloseEnum(he);
    end;
   end;
end;
var
  he: THandle;
begin
  WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY,
RESOURCEUSAGE_ALL, nil, he);
  Enum(he);
  WNetCloseEnum(he);
end;


procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
aPoint:TPoint;
begin
aPoint.x:=x;
aPoint.y:=y;
if Listbox1.ItemAtPos(APoint,true)=-1 then
ListBox1.Items.Add(TreeView1.selected.Text)
else
listbox1.items.Insert(listbox1.itematpos(aPoint,true),Treeview1.selected.text);
Treeview1.selected.Delete;
end;

procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
if Source is TTreeView then
Accept:=true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
listbox1.DragMode := dmAutomatic;
Treeview1.DragMode := dmAutomatic;
end;

procedure TForm1.TreeView1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
APoint:TPoint;
begin
APoint.x:=x;
APoint.y:=y;
if treeview1.GetNodeAt(x,y)<>nil then
Treeview1.begindrag(false);
end;

procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
Treeview1.Items.Insert(TreeView1.selected,Listbox1.Items.strings[Listbox1.itemIndex]);
Listbox1.Items.Delete(Listbox1.ItemIndex);
end;

procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
aPoint:TPoint;
begin
APoint.x:=x;
APoint.Y:=y;
if listbox1.ItemAtPos(apoint,true)<>-1 then
listbox1.begindrag(false);
end;

procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
if (source is TListBox) and (Treeview1.GetNodeAt(x,y)<>nil) then
accept:=true;
end;

end.
0
 
eskAuthor Commented:
when i push the button its add all computers into treeview but all computers will go under the topitem, but all workgroup or domain are down

Esk
0
 
intheCommented:
wouldnt it be easier to add the drag drop code to the http://www.delphifreestuff.com/examples/wnetexmp.zip 
 demo ..i cant test with more than 1 other pc so i dont know how the code i post works on big networks :0(
0
 
eskAuthor Commented:
Adjusted points to 700
0
 
eskAuthor Commented:
Alright Barry, thanks
0
 
intheCommented:
is it ok to answer now ?
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 12
  • 9
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now