Solved

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

Posted on 2012-03-23
4
554 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
[X]
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
  • 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 38

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Part One of the two-part Q&A series with MalwareTech.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Six Sigma Control Plans
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

632 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