//列出所有共享目录。要用NetShareEnum函数,NT,98不一样:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
lbxShares: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TShareInfo50 = packed record
shi50_netname : array [0..12] of Char;
shi50_type : Byte;
shi50_flags : Word;
shi50_remark : PChar;
shi50_path : PChar;
shi50_rw_password : array [0..8] of Char;
shi50_ro_password : array [0..8] of Char;
end;
type
TShareInfo2 = packed record
shi2_netname : PWChar;
shi2_type : DWORD;
shi2_remark : PWChar;
shi2_permissions : DWORD;
shi2_max_uses : DWORD;
shi2_current_uses : DWORD;
shi2_path : PWChar;
shi2_passwd : PWChar;
end;
PShareInfo2 = ^TShareInfo2;
TShareInfo2Array = array [0..512] of TShareInfo2;
PShareInfo2Array = ^TShareInfo2Array;
var
Form1: TForm1;
NetShareEnum :function (pszServer : PChar;
sLevel : Cardinal;
pbBuffer : PChar;
cbBuffer : Cardinal;
pcEntriesRead,
pcTotalAvail : Pointer )
WORD; stdcall;
NetShareEnumNT :function (ServerName
WChar;
Level
WORD;
Bufptr
ointer;
Prefmaxlen
WORD;
EntriesRead,
TotalEntries,
resume_handle:LPDWORD): DWORD; stdcall;
implementation
{$R *.dfm}
function IsNT(var Value: Boolean): Boolean;
var Ver: TOSVersionInfo;
BRes: Boolean;
begin
Ver.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
BRes := GetVersionEx(Ver);
if not BRes then
begin
Result := False;
Exit;
end else
Result := True;
case Ver.dwPlatformId of
VER_PLATFORM_WIN32_NT : Value := True;
VER_PLATFORM_WIN32_WINDOWS : Value := False;
VER_PLATFORM_WIN32s : Result := False;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
FLibHandle : THandle;
ShareNT : PShareInfo2Array;
entriesread,totalentries
WORD;
Share : array [0..512] of TShareInfo50;
pcEntriesRead,pcTotalAvail:Word;
OS: Boolean;
begin
lbxShares.Items.Clear;
if not IsNT(OS) then Close;
if OS then begin
FLibHandle := LoadLibrary('NETAPI32.DLL');
if FLibHandle = 0 then Exit;
@NetShareEnumNT := GetProcAddress(FLibHandle,'NetShareEnum');
if not Assigned(NetShareEnumNT) then
begin
FreeLibrary(FLibHandle);
Exit;
end;
ShareNT := nil;
if NetShareEnumNT(nil,2,@ShareNT,DWORD(-1),
@entriesread,@totalentries,nil) <> 0 then
begin
FreeLibrary(FLibHandle);
Exit;
end;
if entriesread > 0 then
for i:= 0 to entriesread - 1 do
lbxShares.Items.Add(String(ShareNT^
.shi2_netname));
end else begin
FLibHandle := LoadLibrary('SVRAPI.DLL');
if FLibHandle = 0 then Exit;
@NetShareEnum := GetProcAddress(FLibHandle,'NetShareEnum');
if not Assigned(NetShareEnum) then
begin
FreeLibrary(FLibHandle);
Exit;
end;
if NetShareEnum(nil,50,@Share,SizeOf(Share),
@pcEntriesRead,@pcTotalAvail) <> 0 then
begin
FreeLibrary(FLibHandle);
Exit;
end;
if pcEntriesRead > 0 then
for i:= 0 to pcEntriesRead - 1 do
lbxShares.Items.Add(String(Share.shi50_netname));
end;
FreeLibrary(FLibHandle);
end;
end.