Этот пример выдаёт ошибку при возврате Result := THostInfo(info^);
type
THostInfo = record
username: PWideChar;
logon_domain: PWideChar;
other_domains: PWideChar;
logon_server: PWideChar;
end;
WKSTA_USER_INFO_0 = packed record
wkui0_username: PWideChar;
end;
PWKSTA_USER_INFO_0 = ^WKSTA_USER_INFO_0;
implementation
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(HostName: WideString ): THostInfo;
const
STR_ERROR_ACCESS_DENIED = 'The user does not have access to the requested information.';
STR_ERROR_MORE_DATA = 'Specify a large enough buffer to receive all entries.';
STR_ERROR_INVALID_LEVEL = 'The level parameter is invalid.';
var
Info: Pointer;
ElTotal: PDWord;
ElCount: PDWord;
Resume: PDWord;
Error: Longint;
begin
Resume := 0;
NetWkstaUserEnum(PWideChar(HostName),
1,
Info,
0,
ElCount,
ElTotal,
Resume);
Error := NetWkstaUserEnum(PWideChar(HostName),
1,
Info,
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 Info <> nil then
begin
Result := THostInfo(info^);
end
else
begin
Result.UserName := 'N/A';
Result.logon_domain := 'N/A';
Result.other_domains := 'N/A';
Result.logon_server := 'N/A';
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
HostInfo: THostInfo;
begin
HostInfo := EnumNetUsers('WORKSTATION');
with HostInfo do
begin
Memo1.Lines.Add(username + #13#10+
logon_domain + #13#10+
other_domains + #13#10+
logon_server);
end;
end;