Solved

Enumerate hidden network shares

Posted on 2003-11-07
3
549 Views
Last Modified: 2010-04-05
Which WinApi function do I use to enumerate hidden network shares, ie those suffixed $.

I use WNetOpenEnum and WNetEnumResource, but this only finds the visible shares.
0
Comment
Question by:JohnStevenson
  • 2
3 Comments
 
LVL 6

Expert Comment

by:DaFox
ID: 9705552
Hi John,

NetShareEnum() does the trick. The WNet*() APIs don't show hidden devices, NetShareEnum() does. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netshareenum.asp

"You can also use the WNetEnumResource function to retrieve resource information. However, WNetEnumResource does not enumerate hidden shares or users connected to a share."

Regards,
Markus
0
 

Author Comment

by:JohnStevenson
ID: 9732623
Thanks Markus, that's what I need. I think I was put off by the following from the Windows SDK that ships with Delphi 7:

"The NetShareEnum function is obsolete. It is provided only for compatibility with LAN Manager and 16-bit versions of Windows. Win32-based applications should use the WNetEnumResource function."

I'll know in future to always look on the website.

Have you got a quick Delphi sample? Don't worry, I'll give you the points anyway. Thanks again.

Regards

John
0
 
LVL 6

Accepted Solution

by:
DaFox earned 250 total points
ID: 9736025
John,

Below is an example I found on the net (it's not mine!). Basically:
- You have to link NetShareEnum() dynamically, since on 9x it is placed in svrapi.dll, on NT platform it's netapi32.dll
- There are several SHARE_INFO_* structs. Watch out that you take the right one, share_info_50 is for 9x use only, all the other structs are available on NT platform only
- SHI50F_SYSTEM ($0200) indicates a hidden share (share_info_50.shi50_flags) on 9x, a $ indicates a hidden share on NT

The code shows these differences in 9x/NT quite nice. It works fine on my Windows 2000, but once again: This code was not made by me, I just put all of it together (http://www.delphimaster.ru/articles/netmon/)

type
  TShareInfo2 = packed record
    shi2_netname      : PWChar;
    shi2_type            : DWORD;
    shi2_remark       : PWChar;
    shi2_permissions      : DWORD;
    shi2_max_uses         : DWORD;
    shi2_current_uses      : DWORD;
    shi2_path               : PWChar;
    shi2_passwd       : PWChar;
  end;
  PShareInfo2 = ^TShareInfo2;
  TShareInfo2Array = array [0..512] of TShareInfo2;
  PShareInfo2Array = ^TShareInfo2Array;

  TShareInfo50 = packed record
    shi50_netname       : array [0..12] of Char;
    shi50_type             : Byte;
    shi50_flags       : Word;
    shi50_remark       : PChar;
    shi50_path             : PChar;
    shi50_rw_password       : array [0..8] of Char;
    shi50_ro_password       : array [0..8] of Char;
  end;

// ...

  NetShareEnum: function(pszServer: PChar; sLevel: Cardinal; pbBuffer: PChar;
                         cbBuffer: Cardinal; pcEntriesRead,
                         pcTotalAvail: Pointer): DWord; stdcall;

  NetShareEnumNT: function(ServerName: PWChar; Level: DWord; Bufptr: Pointer;
                           Prefmaxlen: DWord; EntriesRead, TotalEntries,
                           resume_handle: LPDWord): DWord; stdcall;

// ...

procedure TForm1.Button1Click(Sender: TObject);

  function IsNT(var Value: Boolean): Boolean;
  var
    Ver: TOSVersionInfo;
    BRes: Boolean;
  begin
    Ver.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
    BRes := GetVersionEx(Ver);
    if not BRes then
    begin
      Result := False;
      Exit;
    end else
      Result := True;
    case Ver.dwPlatformId of
      VER_PLATFORM_WIN32_NT      : Value := True;   // Windows NT
      VER_PLATFORM_WIN32_WINDOWS : Value := False;  // Windows 9x-Me
      VER_PLATFORM_WIN32s        : Result := False; // Windows 3.x
    end;
  end;

var
  i: Integer;
  FLibHandle: THandle;
  ShareNT: PShareInfo2Array;
  entriesRead, totalEntries: DWord;
  Share: array [0..512] of TShareInfo50;
  pcEntriesRead,pcTotalAvail: Word;
  OS: Boolean;
begin
  ListBox.Items.Clear;
  if not IsNT(OS) then Close;

  if OS then begin // NT
    FLibHandle := LoadLibrary('NETAPI32.DLL');
    if FLibHandle = 0 then Exit;
    @NetShareEnumNT := GetProcAddress(FLibHandle,'NetShareEnum');
    if not Assigned(NetShareEnumNT) then
    begin
      FreeLibrary(FLibHandle);
      Exit;
    end;
    ShareNT := nil;
    if NetShareEnumNT(nil,2,@ShareNT,DWORD(-1),
             @entriesread,@totalentries,nil) <> 0 then
    begin
      FreeLibrary(FLibHandle);
      Exit;
    end;
    if entriesRead > 0 then
    for i:= 0 to entriesread - 1 do
            ListBox.Items.Add(String(ShareNT^[i].shi2_netname));

  end else
  begin //9x
    FLibHandle := LoadLibrary('SVRAPI.DLL');
    if FLibHandle = 0 then Exit;
    @NetShareEnum := GetProcAddress(FLibHandle,'NetShareEnum');
    if not Assigned(NetShareEnum) then
    begin
      FreeLibrary(FLibHandle);
      Exit;
    end;
    if NetShareEnum(nil,50,@Share,SizeOf(Share),
            @pcEntriesRead,@pcTotalAvail) <> 0 then
    begin
      FreeLibrary(FLibHandle);
      Exit;
    end;
    if pcEntriesRead > 0 then
    for i:= 0 to pcEntriesRead - 1 do
            ListBox.Items.Add(String(Share[i].shi50_netname));
  end;
  FreeLibrary(FLibHandle);
end;

Markus
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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

920 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

16 Experts available now in Live!

Get 1:1 Help Now