Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

NetShareEnum in delphi

Posted on 2004-10-27
6
Medium Priority
?
1,756 Views
Last Modified: 2008-01-09
Im trying to list all shares on a REMOTE machine (this includes hidden shares such as c$), can someone please advise me on how to do this in full-working code?
0
Comment
Question by:robbiegrove
6 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12430886
I think NetShareEnum is wrong starting point. You need WNetEnumResource for network shares

http://www.delphiworld.narod.ru/base/enum_net_connections.html
0
 

Author Comment

by:robbiegrove
ID: 12431214
im 90% sure WNetEnumResource is for LOCAL shares..
I have used NetShareEnum in C before and it IS what iam after, however i had alot of trouble trying to port it to delphi.
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 750 total points
ID: 12434937
Just to clarify:

1.) NetShareEnum is not the wrong starting point; it can return hidden shares where WNetEnumResource cant.
2.) WNetEnumResource CAN be used to get remote resource location; it just can't get the hidden shares.

Here is an msdn example translated to delphi, just compile it as a console app, run it, and enter the server name. note: PLEASE make sure you have permissions to the server before saying it does not work.

Regards,
Russell

----

program netenum;
{$APPTYPE CONSOLE}

uses
  Windows,
  SysUtils,
  Classes;

type
  _SHARE_INFO_502         =  packed record
     shi502_netname:      PWideChar;
     shi502_type:         DWORD;
     shi502_remark:       PWideChar;
     shi502_permissions:  DWORD;
     shi502_max_uses:     DWORD;
     shi502_current_uses: DWORD;
     shi502_path:         LPWSTR;
     shi502_passwd:       LPWSTR;
     shi502_reserved:     DWORD;
     shi502_security_dsc: PSECURITY_DESCRIPTOR;
  end;
  SHARE_INFO_502          =  _SHARE_INFO_502;
  PSHARE_INFO_502         =  ^SHARE_INFO_502;
  LPSHARE_INFO_502        =  PSHARE_INFO_502;
  TShareInfo502           =  SHARE_INFO_502;
  PShareInfo502           =  PSHARE_INFO_502;

type
  TShareInfo502Array      =  Array [0..MaxWord] of TShareInfo502;
  PShareInfo502Array      =  ^TShareInfo502Array;

function   NetApiBufferFree(buffer: Pointer): DWORD; stdcall; external 'netapi32.dll';
function   NetShareEnum(servername: PWideChar;
                        level: DWORD;
                        bufptr: PByteArray;
                        prefmaxlen: DWORD;
                        entriesread: PDWORD;
                        totalentries: PDWORD;
                        resume_handle: PDWORD): DWORD; stdcall; external 'netapi32.dll';


var
  p:             PShareInfo502Array;
  res,
  er, tr,
  resume,
  i:             DWORD;
  szServer:      String;
  lpwszServer:   Array [0..255] of WideChar;
  pwszServer:    PWideChar;

begin

   er:=0;
   tr:=0;
   resume:=0;

   // Flush input on console
   FlushConsoleInputBuffer(GetStdhandle(STD_INPUT_HANDLE));

   // Get the server
   Write('Enter server name: ');
   ReadLn(szServer);

   // Check parameter
   if (Length(szServer) = 0) then
     pwszServer:=nil
   else
   begin
     // Make sure the server starts with a \\
     if (Pos('\\', szServer) <> 1) then szServer:='\\'+szServer;
     StringToWideChar(szServer, @lpwszServer, SizeOf(lpwszServer));
     pwszServer:=@lpwszServer;
   end;

   // Print a report header.
   WriteLn('Share:              Local Path:                   Uses:   Descriptor:');
   WriteLn('---------------------------------------------------------------------');

   // Call the NetShareEnum function; specify level 502.
   repeat
     res:=NetShareEnum(pwszServer, 502, @p, DWORD(-1), @er, @tr, @resume);
     // If the call succeeds
     if (res = ERROR_SUCCESS) or (res = ERROR_MORE_DATA) then
     begin
        // Loop through the entries;
        // print retrieved data.
        for i:=1 to Pred(er) do
        begin
           Write(Format('%-20s%-30s%-0.8d', [WideCharToString(p^[i].shi502_netname),
                   WideCharToString(p^[i].shi502_path), p^[i].shi502_current_uses]));
           //
           //  Validate the value of the
           //  shi502_security_descriptor member.
           //
           if IsValidSecurityDescriptor(p^[i].shi502_security_dsc) then
               WriteLn('Yes')
            else
               WriteLn('No');
        end;
        //
        // Free the allocated buffer.
        //
        NetApiBufferFree(p);
     end
     else
         WriteLn('Error: ', res);

     // Continue to call NetShareEnum while
     //  there are more entries.

   until (res <> ERROR_MORE_DATA);

   // Pause
   ReadLn;

end.

0
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!

 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12437731
program WMI;

{$APPTYPE CONSOLE}

uses Windows, SysUtils, Classes, ActiveX, Variants, WbemScripting_TLB;

function NextItem(var Enum: IEnumVARIANT; const riid: TGUID; out ppObject): Boolean;
var
  OleProperty: OleVariant;
  NumProp: LongWord;
begin
  try
    Result := Succeeded(Enum.Next(1, OleProperty, NumProp)) and (NumProp > 0) and Succeeded(IDispatch(OleProperty).QueryInterface(riid, ppObject));
  except
    Result := False;
    IUnknown(ppObject) := nil;
  end;
end;

procedure RunMe;
const
  sQuery = 'select * from Win32_Share';
var
  Enum: IEnumVariant;
  Item: SWbemObject;
  Lines: TStringList;
  Locator: SWbemLocator;
  MachineName: string;
  ObjectSet: SWbemObjectSet;
  Prop: SWbemProperty;
  PropEnum: IEnumVariant;
  Services: SWbemServices;
begin
  Lines := TStringList.Create;
  MachineName := 'Knight'; // Name of machine that needs to be enumerated.
  try
    Locator := CoSWbemLocator.Create;
    Services := Locator.ConnectServer(MachineName, 'root\cimv2', '', '', '', '', 0, nil);
    ObjectSet := Services.ExecQuery(sQuery, 'WQL', wbemFlagBidirectional, nil);
    Enum := ObjectSet._NewEnum as IEnumVariant;
    while NextItem(Enum, SWBemObject, Item) do begin
      Lines.Add(Item.Path_.Path);
      PropEnum := Item.Properties_._NewEnum as IEnumVariant;
      while NextItem(PropEnum, SWBemProperty, Prop) do begin
        Lines.Add(Format('+ %s: %s = %s', [Prop.Name, VarTypeAsText(VarType(Prop.Get_Value)), Trim(VarToStrDef(Prop.Get_Value, '<Error>'))]));
      end;
      Lines.Add('');
    end;
  except on E: Exception do Lines.Add(E.Message);
  end;
  Write(Lines.Text);
  Lines.Free;
end;

begin
  if Assigned(InitProc) then TProcedure(InitProc);
  RunMe;
  Write('Press <Enter> to quit');
  ReadLn;
end.

See http://www.workshop-alex.org/Sources/WbemScripting_TLB/WbemScripting_TLB.html or http://www.workshop-alex.org/Sources/WbemScripting_TLB/WbemScripting_TLB.pas for the WMI unit used in above code. This version is optimized for Delphi 7.

Yeah, well. I'm using WMI to access the share information, which is the easiest way to enumerate shares on a REMOTE system. All you need to provide is the right machine name, which in my case is "Knight".
Since this is a console application, I needed to initialize the COM system by calling InitProc. The use of WMI can be a lot easier than the use of some obscure API functions. ;-)
0
 

Author Comment

by:robbiegrove
ID: 12439297
Thanks rllibby helped ALOT! almost exactly what i was looking for :D

Sorry Workshop_alex, just wanted to know how to use NetShareEnum in delphi.
Probably should have made that more clear.. Is there a way i can accept your answer aswell tho?
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12462367
Don't worry, robbie... I just added my answer to show there are some other ways to get information too. Many people are afraid to use WMI in the first place since they think it's complex... Well, they are right of course but hey, if you learn fast you'll learn to use it fast too. :-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

916 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