Solved

Enumerate hidden network shares

Posted on 2003-11-07
3
547 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

10 Experts available now in Live!

Get 1:1 Help Now