Solved

Enum network computers

Posted on 2001-08-30
14
453 Views
Last Modified: 2010-05-18
How is possible to enum network computers in code, to maybe treeview or similar control?

Karen
0
Comment
Question by:karen021897
  • 4
  • 4
  • 4
  • +1
14 Comments
 
LVL 3

Accepted Solution

by:
VSF earned 200 total points
ID: 6440225
This unit should do it!
Hope this helps!
VSF
www.victory.hpg.com.br

unit Unit2;

interface

uses
  classes,sysutils,windows,Forms,Dialogs,FileCtrl;

type
  TenEnumKind  = (ekNetwork,ekDomain,ekServer,ekShare);
const
  kasEnumKind  : array[TenEnumKind] of string[20] =
                          (
                            'Network',
                            'Domain',
                            'Server',
                            'Share'
                            );

procedure EnumNetRes(aScope,aType,aUsage : integer; prNetRes :
PNetResource; sl : TStringList; ek : TenEnumKind);
procedure GetDomainList(const sNetwork : string; sl : TStringList);
procedure GetNetworkList(sl : TStringList);
procedure GetExtendedError(iWinErr : integer);
procedure GetServerList(const sDomain,sNetwork : string; sl :
TStringList);

function GetItAll(Target:String):String;

implementation

function GetItAll(Target:String):String;
   function TakeOutSemiAndAddSlash(InVal:String):String;
   var F:Integer;
   begin
        Result:='';
        For F:= 1 to Length(InVal) do
            begin
                if InVal[F]<>':' then
                   Result:=Result+(InVal[F]);
            end;
        Result:='\'+Result;
   end;
Var
  AllNetwork,
  AllDomains,
  AllServers:TStringList;
  I,J,K:Integer;
  NetworkName,DomainName,Servername:String;
begin
  Result:='';
  AllNetwork:=TStringList.Create;
  GetNetworkList(AllNetwork);
  For I:=0 to AllNetwork.Count-1 do
      begin
           NetworkName:=(AllNetwork[i]);
           {ShowMessage('Looking into the ['+NetworkName+'] Network'); }
           AllDomains:=TStringList.Create;
           Application.Processmessages;
           GetDomainList(NetworkName,AllDomains);
           For J:=0 to AllDomains.Count-1 do
               begin
                    DomainName:=(AllDomains[i]);
                    {ShowMessage('Looking at the '+DomainName+' domain');
}
                    AllServers:=TStringList.Create;
                    GetServerList(DomainName,NetworkName,AllServers);
                    For K:=0 to AllServers.Count-1 do
                        begin
                             Servername:=(AllServers[k]);
                             {ShowMessage('Looking at the '+Servername+'
server');}
                             if DirectoryExists
                                (
                                Servername+TakeOutSemiAndAddSlash
                                (
                                ExtractFilePath(Application.ExeName)
                                )
                                ) then
                                  begin
                                       Result:=Servername+Target;
                                       {ShowMessage('Found: '+Result); }
                                       AllServers.Free;
                                       AllDomains.Free;
                                       AllNetwork.Free;
                                       Exit;
                                  end;
                        end;
                    AllServers.Free;
               end;
           AllDomains.Free;
      end;
  AllNetwork.Free;
end;

procedure GetNetworkList(sl : TStringList);
begin
  if sl = nil then exit;
  sl.BeginUpdate;
  try
    sl.Clear;

EnumNetRes(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,nil,sl,ekNetwork);   finally
    sl.EndUpdate;
  end;
end;


procedure GetDomainList(const sNetwork : string; sl : TStringList);
var
  rNetRes          : PNetResource;
begin
  if sl = nil then exit;
  sl.BeginUpdate;
    sl.Clear;
    New(rNetRes);
    { Start here }
    with rNetRes^ do begin
      dwScope := RESOURCE_GLOBALNET;
      dwType := RESOURCETYPE_ANY ;
      dwDisplayType := RESOURCEDISPLAYTYPE_GENERIC;
      dwUsage := RESOURCEUSAGE_CONNECTABLE;
      lpLocalName := nil;
      lpRemoteName := 0;
      lpComment := nil;
      lpProvider := pChar(sNetwork);
    end;

EnumNetRes(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEDISPLAYTYPE_GENERIC,rNetRes,sl,ekDomain);
    sl.EndUpdate;
    Dispose(rNetRes);
end;



procedure GetServerList(const sDomain,sNetwork : string; sl :
TStringList);
var
  rNetRes          : PNetResource;
begin
  if sl = nil then exit;
  sl.BeginUpdate;
    sl.Clear;
    New(rNetRes);
    with rNetRes^ do begin
      dwScope := RESOURCE_GLOBALNET;
      dwType := RESOURCETYPE_ANY;
      dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;
      dwUsage := RESOURCEUSAGE_CONNECTABLE;
      lpLocalName := nil;
      lpRemoteName := pChar(sDomain);
      lpComment := nil;
      lpProvider := pChar(sNetwork);
    end;


EnumNetRes(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEDISPLAYTYPE_SERVER,rNetRes,sl,ekServer);
    Dispose(rNetRes);
    sl.EndUpdate;
end;



procedure EnumNetRes(aScope,aType,aUsage : integer; prNetRes :
PNetResource; sl : TStringList; ek : TenEnumKind);
const
  kiNetResCount = 1024;
type
  TarNetRes  = array[0..pred(kiNetResCount)] of TNetResource;

  procedure FillEntries(arNetRes : TarNetRes; iCount : integer);   var
    i      : integer;
    Thething:String;
  begin
    for i := 0 to pred(iCount) do begin
      TheThing:=arNetRes[i].lpRemoteName;
      if sl<>Nil then
         begin
              Try
              sl.Add(TheThing);
              Except
              End;
         end;
    end;  // for  i
  end;
  procedure DoEnum(hEnum : THandle);
  var
    iWinErr           :  integer;
    arNetRes          :  TarNetRes;
    iCount            :  DWORD;
    dwBufSize         :  DWord;
  begin
    iCount := 20;
    fillchar(arNetRes,sizeof(arNetRes),0);
    dwBufSize := SizeOf(arNetRes);
    iWinErr := WNetEnumResource( hEnum,
                                iCount,
                                @arNetRes[0],
                                dwBufSize);
    case iWinErr of
      No_Error                  : FillEntries(arNetRes,iCount);
      ERROR_NO_MORE_ITEMS,
      ERROR_INVALID_PARAMETER  : ;
      ERROR_ACCESS_DENIED      : sl.Add('Access denied');
      else GetExtendedError(iWinErr);
    end; // case
  end;

var
  iWinErr          : Integer;
  hEnum            : THandle;
begin
  iWinErr := WNetOpenEnum(aScope,aType,aUsage,prNetRes,hEnum);
  try
    case iWinErr of
      NO_ERROR                  : DoEnum(hEnum);
      ERROR_NO_MORE_ITEMS,
      ERROR_INVALID_PARAMETER  : ;
      ERROR_ACCESS_DENIED      : sl.Add('Access denied');
    end;  // case
    if iWinErr=ERROR_EXTENDED_ERROR then GetExtendedError(iWinErr);
  finally
    WNetCloseEnum(hEnum);
  end;
end;

procedure GetExtendedError(iWinErr : integer);
const
  kiDesc      = 255;
  kiProvider  = 255;
var
  iNetErr      : DWORD;
  zDesc        : array[0..255] of char;
  zProvider    : array[0..255] of char;
  sDesc        : String;
  sProvider    : string;
begin
  setLength(sDesc,kiDesc);
  SetLength(sProvider,kiProvider);
  WNetGetLastError(iNetErr,zDesc,kiDesc,zProvider,kiProvider);
  sDesc := StrPas(zDesc);
  sProvider := StrPas(zProvider);
  Showmessage(sDesc);
end;

end.
0
 

Author Comment

by:karen021897
ID: 6440480
do you have a example of how to use it, a demo project?
if you have please send it to karen@vis.is

Thanks
0
 

Expert Comment

by:DelFreak
ID: 6443434
Listening...
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 3

Expert Comment

by:VSF
ID: 6444462
ok!
0
 

Author Comment

by:karen021897
ID: 6445834
Raise the points up to 200
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6446185
Give me your address to send you a component and demos to do this job

Regards,
Nick aka ginsonic
0
 

Author Comment

by:karen021897
ID: 6446481
karen@vis.is

question to VSF
---------------
Do you have this component for D6, i cannot install it into my IDE

Thankx
Karen.
0
 
LVL 3

Expert Comment

by:VSF
ID: 6448767
It's not a component at all!
It's only a unit that can be added to your project and used to list some usefull network info!

VSF
www.victory.hpg.com.br
0
 

Expert Comment

by:DelFreak
ID: 6449403
VSF can you send it to me too. Thanks!
0
 

Author Comment

by:karen021897
ID: 6450107
when i open the demo project's i have the message from Delphi IDE, TNeighborhood class not found
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6451224
Install first the component :)
0
 

Expert Comment

by:DelFreak
ID: 6451829
ginsonic can you sendthe project to me too. My e-mail address is delfreak@thedoghousemail.com

VSF can you send me the project. My e-mail address is delfreak@thedoghousemail.com
0
 
LVL 3

Expert Comment

by:VSF
ID: 6618507
Sorry, but right now I'm having too much things to at my Uni!
No time to make samples on that code... maybe latter!

Sorry

VSF
0
 

Expert Comment

by:DelFreak
ID: 6619273
VSF, can you send me the file? My e-mail address is stated above. Thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to open a new windows chrome resized and disabled the toolbar? 3 148
LAN or WAN ? 11 95
Error E2158 compiling with Delphi XE10 Seattle 2 112
CheckListBox usage 3 71
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question