Solved

Enumerate hidden network shares

Posted on 2003-11-07
3
551 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

786 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