How to programmatically read shared names using Delphi 6 in Win XP ?

How to programmatically read shared names using Delphi 6 in Win XP ?

It should be similar to the output from the "net share" command (see attached file).
In this output, I am interest in the shared name:
ADMIN$
C$
IPC$
typical-net-share-output.bmp
sepknowAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rinfoCommented:
Have not tested but have you tried it
It will output names to a file which you can read

ShellExecute(0,nil,'cmd.exe','net share > filename.txt',nil,sw_normal);
0
rinfoCommented:
ok try this
function TForm1.GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;
var
  SA: TSecurityAttributes;
  SI: TStartupInfo;
  PI: TProcessInformation;
  StdOutPipeRead, StdOutPipeWrite: THandle;
  WasOK: Boolean;
  Buffer: array[0..255] of AnsiChar;
  BytesRead: Cardinal;
  WorkDir: string;
  Handle: Boolean;
begin
  Result := '';
  with SA do begin
    nLength := SizeOf(SA);
    bInheritHandle := True;
    lpSecurityDescriptor := nil;
  end;
  CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
  try
    with SI do
    begin
      FillChar(SI, SizeOf(SI), 0);
      cb := SizeOf(SI);
      dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
      wShowWindow := SW_HIDE;
      hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
      hStdOutput := StdOutPipeWrite;
      hStdError := StdOutPipeWrite;
    end;
    WorkDir := Work;
    Handle := CreateProcess(nil, PChar('cmd.exe /C ' + CommandLine),
                            nil, nil, True, 0, nil,
                            PChar(WorkDir), SI, PI);
    CloseHandle(StdOutPipeWrite);
    if Handle then
      try
        repeat
          WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
          if BytesRead > 0 then
          begin
            Buffer[BytesRead] := #0;
            Result := Result + Buffer;
          end;
        until not WasOK or (BytesRead = 0);
        WaitForSingleObject(PI.hProcess, INFINITE);
      finally
        CloseHandle(PI.hThread);
        CloseHandle(PI.hProcess);
      end;
  finally
    CloseHandle(StdOutPipeRead);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Text := GetDosOutput('Net Share');
end;

The result you get from function call has the names  among other information.
A way could be finding out string between CR LF and $.
You have to find out how to get those name .
0
Geert GOracle dbaCommented:
found this from Russel Libby
with a little modification returns info in a memo:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

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';


procedure ListShareEnum(server: string; items: TStrings);
var
  p:             PShareInfo502Array;
  res,
  er, tr,
  resume,
  i:             DWORD;
  szServer, line:      String;
  lpwszServer:   Array [0..255] of WideChar;
  pwszServer:    PWideChar;
begin

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

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

   szServer := server;
   // 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
           line := Format('%-20s%-30s%-0.8d', [WideCharToString(p^[i].shi502_netname),
                   WideCharToString(p^[i].shi502_path), p^[i].shi502_current_uses]);
           //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
               line := line + ' SecurityDescriptor:Yes'
            else
               line := line + ' SecurityDescriptor:No';
           items.Add(line);
        end;
        //
        // Free the allocated buffer.
        //
        NetApiBufferFree(p);
     end
     else
     begin
       line := 'Error: ' + IntToStr(res);
       items.Add(line);
     end;
     // Continue to call NetShareEnum while
     //  there are more entries.

   until (res <> ERROR_MORE_DATA);

   // Pause
   //ReadLn;

end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  ListShareEnum('', memo1.Lines);
end;

end.

Open in new window

0
ThommyCommented:
Here's a little demo for you, derived from get the local shares...

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

Type
 SHARE_INFO_2   = record
    shi2_netname     :  PWideChar;
    shi2_type        :  DWORD;
    shi2_remark      :  PWideChar;
    shi2_permissions :  DWORD;
    shi2_max_uses    :  DWORD;
    shi2_current_uses:  DWORD;
    shi2_path        :  PWideChar;
    shi2_passwd      :  PWideChar;
  end;

  NET_API_STATUS = DWORD;

  SHARE_INFO_0   = record
    shi0_netname : PWideChar;
  end;

  PSHARE_INFO_2  = ^SHARE_INFO_2;
  

  function NetShareEnum(ServerName   : PWideChar;
                        Level        : DWORD;
                        bufptr       : Pointer;
                        PrefMaxLen   : DWORD;
                        EntriesRead  : PDWORD;
                        TotalEntries : PDWORD;
                        Resume_Handle: PDWORD):NET_API_STATUS;
                        stdcall; external 'NetAPI32.dll' name 'NetShareEnum';
  function NetApiBufferFree(Buffer: Pointer): NET_API_STATUS;
                        stdcall; external 'NetAPI32.dll' name 'NetApiBufferFree';




{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
const
  MAX_PREFERRED_LENGTH = -1;
  NERR_SUCCESS         = 0;
var
  ER : DWORD; // enries read
  TE : DWORD; // total entries
  ResHandle : DWORD;
  ShareInfo : PSHARE_INFO_2;
  p         : pChar;
  fResult   : NET_API_STATUS;
  i         : Integer;
  share, path : string;
begin
  ResHandle := 0;
  fResult := NetShareEnum(nil, 2, @ShareInfo, DWORD(MAX_PREFERRED_LENGTH), @ER, @TE, @ResHandle);
  if(fResult <> NERR_SUCCESS)then
   Exit;
  p := Pointer(ShareInfo);
  for i := 0 to TE - 1 do
  begin
    share := WideCharToString(PSHARE_INFO_2(p)^.shi2_netname);
    path  := WideCharToString(PSHARE_INFO_2(p)^.shi2_path);
    if path<>'' then path:=', '+path;
    ListBox1.Items.Add(share+path);
    p := p + SizeOf(SHARE_INFO_2);
  end;
  NetApiBufferFree(@ShareInfo);
end;

end.

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.