Solved

Network Connections

Posted on 1997-10-06
5
153 Views
Last Modified: 2010-04-04
Can anyone tell me how I can retrieve a list of all the shared resources on a network.  I am specifically interested in drives, not printers.

The returned data should list the server name and the drives on that server.

This will only be used on a WIndows 95 and Windows NT network.  I am not interested in answers which list previously connected paths - only paths which are available now.

I would like to to return something like

\\SERVER1\C-DRIVE
\\SERVER2\CDROM
\\SERVER3\HDD_D

etc etc etc.

Thanks in advance.

Stuart
0
Comment
Question by:SJohnson
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1346904
You can use the WNetEnumResource function to enumerate the currently connected drive resources.  In the example below I only look at connected drives.  You can change this to look for printers, etc.  Look at the help for WNetOpenEnum and WNetEnumResource for other options.

Example:
procedure GetConnections;
var
  EnumResult, dwScope, dwType, dwUsage, hEnum: Integer;
  NumEntries, BufSize: Integer;
  NetResBuf: array[1..3] of TNetResource;
begin
  dwScope := RESOURCE_CONNECTED;
  dwType := RESOURCETYPE_DISK;
  dwUsage := 0;
  WNetOpenEnum(dwScope, dwType, dwUsage, nil, hEnum);

  // Enumerate the resources.
  repeat
    FillChar(NetResBuf, SizeOf(NetResBuf), 0);
    NumEntries := 1;
    BufSize := SizeOf(NetResBuf);
    EnumResult := WNetEnumResource(hEnum, NumEntries, @NetResBuf, BufSize);
    if (EnumResult = NO_ERROR) then
      ShowMessage(string(PChar(NetResBuf[1].lpRemoteName)));
  until (EnumResult <> NO_ERROR);

  // Close the handle.
  WNetCloseEnum(hEnum);
end;

I set NetResBuf to a size of 3 so that you can play around with the NumEntries variable to return more than 1 TNetResource at a time.  It currently returns one connected drive resource per call to WNetEnumResource.

Hope this helps you.
JB
0
 
LVL 1

Author Comment

by:SJohnson
ID: 1346905
I dont think I made myself clear on this.  Acutally reading back, the question was what I ment, but just worded incorrectly.

I need to know what resources are available to my machine to connect too.  Im only interested in drives, not printers.  I dont want a list of resources that I have previously connected too or that I am currently connected too.

For example, go into the Windows Network Neighborhood and if you click on Entire Network, it will list each resource container.  Basically, I wasnt to retrieve that information, but with each containers drive resource as well.  

You where on the right track, but I have already tried this until I was blue in the face.  

Thanks for your help so far.

Stuart
0
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 100 total points
ID: 1346906
I modified the original code to recurse through the whole network.  It shows every domain, server and share on your network.  (Thus it may take a while on a big network.)  This code assumes you have a TMemo called Memo1 on your form.

You would obviously be interested in the shares only, but I put ALL the info into Memo1 so that you could see the steps taken.

Bear in mind that this code lists ALL the shares available to you (as you requested), even those that you may not have access to.  There is a difference.  This code lists all possible shares, but there is no way of knowing whether you have access until you try to connect to each one.  This would slow the process down even further...

Here is the code:
procedure TForm1.EnumNetRes(NetRes: TNetResource; FullPath: string);
var
  EnumResult, dwScope, dwType, dwUsage, hEnum: Integer;
  NumEntries, BufSize: Integer;
  ResStr: string;
  NetResBuf: array[1..3] of TNetResource;
begin
  dwScope := RESOURCE_GLOBALNET;
  dwType := RESOURCETYPE_DISK;
  dwUsage := RESOURCEUSAGE_CONNECTABLE;
  WNetOpenEnum(dwScope, dwType, dwUsage, @NetRes, hEnum);

  // Enumerate the resources.
  repeat
    FillChar(NetResBuf, SizeOf(NetResBuf), 0);
    NumEntries := 1;
    BufSize := SizeOf(NetResBuf);
    EnumResult := WNetEnumResource(hEnum, NumEntries, @NetResBuf, BufSize);
    if (EnumResult = NO_ERROR) then
      begin
        case NetResBuf[1].dwDisplayType of
          RESOURCEDISPLAYTYPE_DOMAIN:
            ResStr := 'Domain ';
          RESOURCEDISPLAYTYPE_GENERIC:
            ResStr := 'Generic ';
          RESOURCEDISPLAYTYPE_SERVER:
            ResStr := 'Server ';
          RESOURCEDISPLAYTYPE_SHARE:
            ResStr := 'Share ';
          else
            ResStr := '(Unknown) ';
        end;
        if (NetResBuf[1].dwUsage = RESOURCEUSAGE_CONTAINER) or (NetResBuf[1].dwDisplayType = RESOURCEDISPLAYTYPE_SERVER) then
          begin
            ResStr := ResStr + '(Container) ' + string(PChar(NetResBuf[1].lpRemoteName));
            Memo1.Lines.Add(ResStr);
            EnumNetRes(NetResBuf[1], FullPath + string(PChar(NetResBuf[1].lpRemoteName)));
          end
        else
          begin
            ResStr := ResStr + ' [' + FullPath + ']  ' + string(PChar(NetResBuf[1].lpRemoteName));
            Memo1.Lines.Add(ResStr);
          end;
        Application.ProcessMessages;
      end;
  until (EnumResult <> NO_ERROR);

  // Close the resource enumeration handle.
  WNetCloseEnum(hEnum);
end;


You call the code as follows:
var
  NetRes: TNetResource;
begin
  FillChar(NetRes, SizeOf(NetRes), 0);
  EnumNetRes(NetRes, '');
  ShowMessage('Finished');
end;

JB
0
 
LVL 3

Expert Comment

by:mirek071497
ID: 1346907
When you have container you can go into and retrive the rest. I have some simple code for all resources, but you need some filters.
When you create complete list you can delete from this what is not for you (printers and other). When you need to delete drives which is now connected you must enumerate all your assigned drive letters and check if they are in the list.
I can submit example to retrive all resources, do you need this ?
0
 
LVL 3

Expert Comment

by:mirek071497
ID: 1346908
Hi when i wrote my comment the answer from JimBob was not existed.
Sorry i see so example submitted by JimBob is perhaps working example.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

947 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now