...get a list of users who are registered on the NT Workstation?

Author: Henrik Jonsson

Category: System

// The NetUserEnum function provides information about all user accounts on a server.

type
  
USER_INFO_1 = 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;
  lpUSER_INFO_1 = ^USER_INFO_1;

function NetUserEnum(ServerName: PWideChar;
  Level,
  Filter: DWORD;
  var Buffer: Pointer;
  PrefMaxLen: DWORD;
  var EntriesRead,
  TotalEntries,
  ResumeHandle: DWORD): Longword; stdcallexternal 'netapi32.dll';

function NetApiBufferFree(pBuffer: PByte): Longint; stdcallexternal
'netapi32.dll';

{...}

procedure TForm1.Button1Click(Sender: TObject);
var
  
EntiesRead: DWORD;
  TotalEntries: DWORD;
  UserInfo: lpUSER_INFO_1;
  lpBuffer: Pointer;
  ResumeHandle: DWORD;
  Counter: Integer;
  NetApiStatus: LongWord;
begin
  
ResumeHandle := 0;
  repeat
    
// NetApiStatus :=
    // NetUserEnum(PChar('\\NT-Domain'), 1, 0, lpBuffer, 0,EntiesRead, TotalEntries, ResumeHandle);
    
NetApiStatus := NetUserEnum(nil, 1, 0, lpBuffer, 0, EntiesRead,
      TotalEntries, ResumeHandle);
    UserInfo     := lpBuffer;

    for Counter := 0 to EntiesRead - 1 do
    begin
      
listbox1.Items.Add(WideCharToString(UserInfo^.usri1_name) + ' --> ' +
        WideCharToString(UserInfo^.usri1_comment));
      Inc(UserInfo);
    end;

    NetApiBufferFree(lpBuffer);
  until (NetApiStatus <> ERROR_MORE_DATA);
end;

 

printed from
www.swissdelphicenter.ch
developers knowledge base