type
TWKSTA_USER_INFO_0 = record
username: PWideChar;
end;
PTWKSTA_USER_INFO_0 = ^TWKSTA_USER_INFO_0;
function NetWkstaUserEnum(servername: PWideChar; level: DWORD; var bufptr: Pointer;
prefmaxlen: DWORD; var entriesread: PDWord; var totalentries: PDWord;
var resumehandle: PDWord): Longint;
stdcall; external 'netapi32.dll' Name 'NetWkstaUserEnum';
function EnumNetUsers(Users: TStrings): TWKSTA_USER_INFO_0;
const
STR_ERROR_ACCESS_DENIED = 'User does not have
access to the requested information.';
STR_ERROR_MORE_DATA = 'Specify a buffer large enough to receive all entries.';
STR_ERROR_INVALID_LEVEL = 'Parameter "level" has been set invalid.';
var
UI: PTWKSTA_USER_INFO_0;
HostInfo: Pointer;
ElTotal: PDWord;
ElCount: PDWord;
Resume: PDWord;
Error: Longint;
i: Integer;
begin
Resume := 0;
NetWkstaUserEnum(nil, 0, HostInfo, 0, ElCount, ElTotal, Resume);
Error := NetWkstaUserEnum(nil, 0, HostInfo, 256 * Integer(ElTotal),
ElCount, ElTotal, Resume);
case Error of
ERROR_ACCESS_DENIED: Result.UserName := STR_ERROR_ACCESS_DENIED;
ERROR_MORE_DATA: Result.UserName := STR_ERROR_MORE_DATA;
ERROR_INVALID_LEVEL: Result.UserName := STR_ERROR_INVALID_LEVEL;
else
if HostInfo <> nil then
begin
Result := TWKSTA_USER_INFO_0(HostInfo^);
UI := PTWKSTA_USER_INFO_0(HostInfo);
for i := 1 to DWord(ElCount) do
begin
Users.Add(UI^.username);
inc(UI);
end;
end
else
begin
Result.UserName := '<???>';
end;
end;
end;