unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 =
class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
const
NERR_Success = 0;
MAX_PREFERRED_LENGTH = DWORD(-1);
type
NET_API_STATUS = DWORD;
TEnumUers =
function(Username:
string; cntUsers: Integer; Data: Pointer): Boolean;
TUserInfo1 =
record
usri1_name: LPWSTR;
usri1_password: LPWSTR;
usri1_password_age: DWORD;
usri1_priv: DWORD;
usri1_home_dir: LPWSTR;
usri1_comment: LPWSTR;
usri1_flags: DWORD;
usri1_script_path: LPWSTR;
end;
PUserInfo1 = ^TUserInfo1;
function NetUserEnum(servername: LPCWSTR; level: DWORD; filter: DWORD;
var buf: Pointer; prefmaxlen: DWORD;
var
entriesred: DWORD;
var totalentries: DWORD; resumehandle: PDWORD): NET_API_STATUS;
stdcall;
function NetApiBufferFree(Buffer: Pointer): NET_API_STATUS;
stdcall;
var
Form1: TForm1;
implementation
const
netapi32lib = '
netapi32.dll';
function NetUserEnum;
external netapi32lib
name '
NetUserEnum';
function NetApiBufferFree;
external netapi32lib
name '
NetApiBufferFree';
{$R *.dfm}
function EnumUsersCallback(Username:
string; cntUsers: Integer; Data: Pointer): Boolean;
begin
TListBox(Data).Items.Add(Username);
Result := True;
end;
function EnumUsers(
const Server: WideString; filter: DWORD; Callback: TEnumUers; Data: Pointer): DWORD;
var
ui1 : Pointer;
pWork : Pointer;
EntriesRead : DWORD;
EntriesTotal : DWORD;
NetError : DWORD;
Loop : Integer;
begin
ui1 :=
nil;
pWork :=
nil;
try
NetError := NetUserEnum(PWideChar(Server), 0, filter, ui1, MAX_PREFERRED_LENGTH, EntriesRead, EntriesTotal,
nil);
if (NetError = NERR_SUCCESS)
and (EntriesRead > 0)
then
begin
pWork := ui1;
if Assigned(Callback)
then
begin
for Loop := 0
to EntriesRead - 1
do
begin
if not Callback(PUserInfo1(ui1)^.usri1_name, EntriesRead, Data)
then
break;
Inc(Integer(ui1), sizeof(Pointer));
end;
end;
end;
finally
NetApiBufferFree(pWork);
end;
Result := NetError;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Data: Pointer;
begin
Data :=
nil;
EnumUsers('
', 0, @EnumUsersCallback, Form1.Listbox1);
end;
end.