Solved

Enumerate hidden network shares

Posted on 2003-11-07
3
555 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
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 …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 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