Enumerating shares on a server ?

Does anybody know how to enumerate shares on a NT server ?

A code sample would be great.

Thanks,
Oliver
novowiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
Hello
there is component with source here:

http://www.jgsoftware.com/nt.htm

under the components link.
Regards Barry
0
simonetCommented:
Following...®
0
novowiAuthor Commented:
Barry,

Sorry for the rejection but this is way over my head. You point me to a whole project of a dozen of units interacting in some way with each other in a complex way and little documentation.

I would like to have a sample of the WNetOpenEnum and WNetEnumResource API call in a simple and clear way. If you can provide me with this, you'll get the points and 50 on top of the 200.

Deal ?

Regards,
Oliver
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

simonetCommented:
Novowi,

If you want something simple (only 1 unit), that uses WNetEnumResources, here's a project with full source code that will do the trick:

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

yours,

Alex
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
novowiAuthor Commented:
Alex,

That's exactly what I was looking for. Thanks...

0
intheCommented:
heres two ways in code:

way one:

use the following function to get the list of computers available through the network neighborhood folder (it put them in a TStrings descendant, just call it as: LoadNetNodes(ComputerList, nil, 5); )
 
 
 
 
procedure LoadNetNode(Dest: TStrings; NetNode: PNetResourceA; depth: integer);
var hEnum : THandle;
    Count,BufSize: DWORD;
    NR,Buf: PNetResource;
    R: Integer;
begin
    R:=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,0,NetNode,hEnum);     if R <> NO_ERROR then exit;
 
    BufSize:=$1;
    GetMem(Buf,BufSize);
    try
        while True do
        begin
            Count:=$FFFFFFFF; // I wish to read ALL items
            R:=WNetEnumResource(hEnum,Count, Buf, BufSize);
 
            if R = ERROR_MORE_DATA then // The InitialSize is too small !             begin
                Count:=$FFFFFFFF; // I wish to read ALL items
                FreeMem(Buf); GetMem(Buf,BufSize);
                R:=WNetEnumResource(hEnum,Count, Buf, BufSize);
            end;
 
            if R = ERROR_NO_MORE_ITEMS then
              Break; // All items are processed
            if R <> NO_ERROR then
              Abort; // R is the error code. Process it!
 
            NR:=Buf;
            while Count > 0 do
            begin
                if NR.dwDisplayType = RESOURCEDISPLAYTYPE_SERVER then                     dest.Add(NR.lpRemoteName);
                if depth > 0 then
                    LoadNetNode(Dest,NR, Depth - 1);
                INC(NR);
                DEC(COunt);
            end;
        end;
    finally
        WNetCloseEnum(hEnum);
        FreeMem(Buf);
    end;
end;








next one :



interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,   StdCtrls, WinSock;
 
type
  TNetWorkResource = (nwDrive, nwPrinter, nwServer, nwDomain);
  PNetResourceArray = ^TNetResourceArray;
  TNetResourceArray = array[0..MaxInt div SizeOf(TNetResource) - 1] of TNetResourceA;
  procedure EnumNetWork(aList: TStrings; ResType: TNetWorkResource);   procedure SearchNetWork(aRes: PNetResource; var Target: PNetResourceArray;           var aCount, aSize: integer; ResType: TNetWorkResource; List: TStringList);
implementation
procedure SearchNetWork(aRes: PNetResource; var Target: PNetResourceArray;             var aCount, aSize: integer; ResType: TNetWorkResource; List: TStringList);
var NetHandle: THandle;
    Temp: String;
    I, Count, BufSize, Size, NetResult, Error: Integer;
    aBuf : PNetResourceArray;
begin
  if aRes=nil then
    Error:= WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, Nil, NetHandle)
  else
    Error:= WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, aRes, NetHandle);
  if Error <> NO_ERROR then
  exit;
  try
    aSize:= 50 * sizeOf(TNetResource);
    GetMem(aBuf, aSize);
    BufSize := aSize;
    if not Assigned(Target) then
      GetMem(Target, aSize);
    try
      while True do
      begin
        Count := -1;
        Size := BufSize;
        NetResult := WNetEnumResource(NetHandle, Count, aBuf, Size);         if NetResult = ERROR_MORE_DATA then
        begin
          BufSize := Size;
          ReallocMem(aBuf, BufSize);
          aSize:= BufSize;
          ReallocMem(Target, aSize);
          Continue;
        end;
        if NetResult <> NO_ERROR then Exit;
        aCount:= Count - 1;
        for I:= 0 to aCount do
          with aBuf^[I] do
          begin
            Target^[I].dwScope := dwScope;
            Target^[I].dwType := dwType;
            Target^[I].dwDisplayType := dwDisplayType;
            Target^[I].dwUsage:= dwUsage;
            Target^[I].lpLocalName:= StrNew(lpLocalName);
            Target^[I].lpRemoteName:= StrNew(lpRemoteName);
            Target^[I].lpComment:= StrNew(lpComment);
            Target^[I].lpProvider:= StrNew(lpProvider);
            case ResType of
              nwDrive : if dwType = ResourceType_Disk then
                          List.Add(lpRemoteName);
              nwPrinter : if dwType = ResourceType_Print then
                          List.Add(lpRemoteName);
              nwServer : if dwDisplayType = ResourceDisplayType_Server then                           List.Add(Copy(lpRemoteName, 3,
Length(lpRemoteName) - 2));
              nwDomain : if dwDisplayType = ResourceDisplayType_Domain then                           List.Add(lpRemoteName);
            end;
          end;
      end;
    finally
    end;
  finally
    WNetCloseEnum(NetHandle);
    FreeMem(aBuf, aSize);
  end;
end;
 
procedure EnumNetWork(aList: TStrings; ResType: TNetWorkResource); var
  Roots : PNetResourceArray;
  Groups : PNetResourceArray;
  Servers : PNetResourceArray;
  Disks : PNetResourceArray;
  Container: PNetResource;
  RootsCount, GroupsCount, ServersCount,
  DisksCount, K, I, J, RootsSize, GroupsSize,
  ServersSize, DisksSize: integer;
  List: TStringList;
begin
  RootsSize:= 0;
  RootsCount:= -1;
  GroupsCount:= -1;
  ServersCount:= -1;
  DisksCount:= -1;
  Roots:= Nil;
  Groups:= Nil;
  Servers:= Nil;
  Disks:= Nil;
  Container:= Nil;
  SearchNetWork(Container, Roots, RootsCount, RootsSize, ResType, List);   if Not Assigned(Roots) then Exit;
  List:= TStringList.Create;
  List.Sorted:= True;
  List.Duplicates:= dupIgnore;
  for K:= 0 to RootsCount do
  begin
    Container:= @Roots^[k];
    SearchNetWork(Container,Groups, GroupsCount, GroupsSize, ResType, List);     for I:= 0 to GroupsCount do
    begin
      Container:= @Groups^[I];
      SearchNetWork(Container, Servers, ServersCount, ServersSize, ResType, List);
      for J:= 0 to ServersCount do
      begin
        Container:= @Servers^[J];
        SearchNetWork(Container, Disks, DisksCount, DisksSize, ResType, List);
      end;
        if Assigned(Disks) then
          FreeMem(Disks, DisksSize);
      FreeMem(Servers, ServersSize);
    end;
    FreeMem(Groups, GroupsSize);
  end;
  aList.AddStrings(List);
  List.Destroy;
  FreeMem(Roots, RootsSize);
end;
 
end.
0
intheCommented:
mmm maybe i should've done a  refresh first ;-)
0
simonetCommented:
>That's exactly what I was looking for. Thanks...

If it was EXACTLY what you were looking for, why did you give me a "B" and not an "A" ???
0
novowiAuthor Commented:
Oops, sorry, I thought I clicked on A. Is there any way of amending ratings ? Let me know and I'll do it. There was no purpose in giving you a "B", you definitly deserved an "A".

Sorry,
Oliver
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.