Link to home
Start Free TrialLog in
Avatar of SJohnson
SJohnsonFlag for Australia

asked on

Network Connections

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
Avatar of JimBob091197
JimBob091197

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
Avatar of SJohnson

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of JimBob091197
JimBob091197

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ?
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.