Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2012-03-23
4
529 Views
Last Modified: 2012-04-15
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
0
Comment
Question by:sepknow
  • 2
4 Comments
 
LVL 9

Expert Comment

by:rinfo
ID: 37760489
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
 
LVL 9

Assisted Solution

by:rinfo
rinfo earned 125 total points
ID: 37760558
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
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 125 total points
ID: 37761029
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
 
LVL 19

Accepted Solution

by:
Thommy earned 250 total points
ID: 37764705
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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

861 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