• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

Enumerating shares on a server ?

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

A code sample would be great.

Thanks,
Oliver
0
novowi
Asked:
novowi
  • 3
  • 3
  • 3
1 Solution
 
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now