Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get the local shares 2

Status
Not open for further replies.

SamC

Programmer
Aug 22, 2002
24
DE
hi,
i need to query the local shares of the computer the prg is running on. Is there an API-Call to do this?
 
I usually read them from registry:
W9x:
HKCU\Network\Persistent
WNT/W2K/WXP:
HKCU\Network

HTH
TonHu
 
thanx for your replies,

markus: please can you give me a short example. in the help there is only a link to WNetEnumResources and a comment '...is obsolete.'

i need something like the result of 'net share'.

SamC
 
There ya go:
Code:
...
interface
... 
  type
    NET_API_STATUS = DWORD;

    SHARE_INFO_0   = record
      shi0_netname : PWideChar;
    end;

    PSHARE_INFO_0  = ^SHARE_INFO_0;

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

implementation
...
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_0;
  p         : pChar;
  fResult   : NET_API_STATUS;
  i         : Integer;
begin
  ResHandle := 0;
  fResult := NetShareEnum(nil, 0, @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
    ListBox1.Items.Add(WideCharToString(PSHARE_INFO_0(p)^.shi0_netname));
    p := p + 4;
  end;
  NetApiBufferFree(@ShareInfo);
end;
Have fun

--- markus

 
hi markus,

thanks for the example. i've changed it a little and now i've got it. great.



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;

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

...

implementation

...

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;


SamC.

 
I gave you a simpliest example and you are free to do with it whatever you need :)

--- markus
 
sorry markus,

in german i would say: ich war sehr dankbar für Dei Beispiel. es hat mir sehr geholfen. Ich wollte Dich nicht kritisieren.

SamC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top