Solved

Enumerate hidden network shares

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

827 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