Exploring the network  

Send By: Radikal (Q3 Team)
Web : http://www.q3.nu
Email: radikal@q3.nu
Date: 23/10/99

Tip accessed 395 times

 



In this example, we will fill a ListView with the net resources that we find (disks, printers and other servers)
If we have intention of allowing to the user of our application explore the net, this it will be the first step that you need.
The following step would be to explore each one of the resources, or to allow that the user map them to a local printer or disk, in short... that your you will know that you seek to make when exploring the local net.

The example enumerates the available net resources, begining with the resources of the own computer, in such a way that when it find another computer, it enumerates (recursively) also the resources that they have.

The Example:

  • Put a TListView (ListView1) and a TButton in your form
  • Put this code in the OnClick event of the TButton:


     procedure TForm1.Button1Click(Sender: TObject);
    
     procedure Enumera(Cual:PnetResource);
     var
       Mango           :THandle;
       NumeroEntradas  :DWord;
       Buffer          :Array [1..100] of TNetResource;
       LongBuffer      :DWord;
       n               :Integer;
       ListItem        :TListItem;
     begin
       LongBuffer:=SizeOf(Buffer);
    
       If WNetOpenEnum( RESOURCE_GLOBALNET,
                        RESOURCETYPE_ANY,
                        0,
                        Cual,
                        Mango
                        )<> No_ERROR then ShowMessage('Error');
    
       {100 entradas (máximo) por cada servidor}
       {100 enties (max) per server}
       NumeroEntradas:=100;
       WNetEnumResource( Mango,
                         NumeroEntradas,
                         @Buffer[1],
                         LongBuffer
                         );
    
       for n:=1 to NumeroEntradas do
       begin
         ListItem:=ListView1.Items.Add;
         With ListItem do
         begin
           Caption:=String(Buffer[n].lpRemoteName);
           If Caption='' then Caption:='Inicio de red';
           SubItems.Add(String(Buffer[n].lpComment));
         end;
    
         if (Buffer[n].dwUsage and RESOURCEUSAGE_CONTAINER)=
             RESOURCEUSAGE_CONTAINER then
         begin
           ListItem.SubItems.Add('Servidor');
           Enumera(@Buffer[n]);
         end
           else
         begin
           if (Buffer[n].dwType and RESOURCETYPE_DISK)=
               RESOURCETYPE_DISK
               then ListItem.SubItems.Add('Disco') else
           if (Buffer[n].dwType and RESOURCETYPE_PRINT)=
               RESOURCETYPE_PRINT
               then ListItem.SubItems.Add('Impresora');
         end;
       end;
     end;
    
    
     begin
       {Inicializamos nuestro ListView}
       {Initialice our ListView}
       with ListView1 do
       begin
         ViewStyle:=vsReport;
         with Columns.Add do
         begin
           {Remote Name}
           Caption:='Nombre Remoto';
           Width:=150;
         end;
         with Columns.Add do
         begin
           {Comment}
           Caption:='Comentario';
           Width:=150;
         end;
         with Columns.Add do
         begin
           {Type}
           Caption:='Tipo';
           Width:=100;
         end;
       end;
       {Rellenamos el ListView}
       {Fill the ListView}
       Enumera(nil);
     end;