Delphi - how to check for any windows user if it is an administrator? -
i need check kind of accounts available on local machine.
i found how can current logged user:
function iswindowsadmin: boolean; var haccesstoken: thandle; ptggroups: ptokengroups; dwinfobuffersize: dword; psidadministrators: psid; g: integer; bsuccess: bool; begin result:= false; bsuccess:= openthreadtoken(getcurrentthread, token_query, true, haccesstoken); if not bsuccess begin if getlasterror = error_no_token bsuccess:= openprocesstoken(getcurrentprocess, token_query, haccesstoken); end; if bsuccess begin getmem(ptggroups, 1024); bsuccess:= gettokeninformation(haccesstoken, tokengroups, ptggroups, 1024, dwinfobuffersize); closehandle(haccesstoken); if bsuccess begin allocateandinitializesid(security_nt_authority, 2, security_builtin_domain_rid, domain_alias_rid_admins, 0, 0, 0, 0, 0, 0, psidadministrators); g:= 0 ptggroups.groupcount - 1 if equalsid(psidadministrators, ptggroups.groups[g].sid) begin result:= true; break; end; freesid(psidadministrators); end; freemem(ptggroups); end; end;
i found how users on local machine. possible check user if administrator or limited account without having log in each , every user?
here's take on subject, sertac akyuz hint netuserenum
user_info_2
request , structure.
we can request users following privilege levels
const user_priv_guest = 0; user_priv_user = 1; user_priv_admin = 2; user_priv_any = 3; // own invention type tprivlevel = user_priv_guest..user_priv_any;
some declarations need
const // consts nerr_success = 0; max_preferred_length = $ffffffff; type netapistatus = dword; // https://msdn.microsoft.com/en-us/library/windows/desktop/aa371337(v=vs.85).aspx tuserinfo2 = record usri2_name: lpwstr ; usri2_password: lpwstr ; usri2_password_age: dword ; usri2_priv: dword ; usri2_home_dir: lpwstr ; usri2_comment: lpwstr ; usri2_flags: dword ; usri2_script_path: lpwstr ; usri2_auth_flags: dword ; usri2_full_name: lpwstr ; usri2_usr_comment: lpwstr ; usri2_parms: lpwstr ; usri2_workstations: lpwstr ; usri2_last_logon: dword ; usri2_last_logoff: dword ; usri2_acct_expires: dword ; usri2_max_storage: dword ; usri2_units_per_week: dword ; usri2_logon_hours: pbyte ; usri2_bad_pw_count: dword ; usri2_num_logons: dword ; usri2_logon_server: lpwstr ; usri2_country_code: dword ; usri2_code_page: dword ; end; puser_info_2 = ^tuserinfo2; lpuser_info_2 = ^tuserinfo2; // https://msdn.microsoft.com/en-us/library/windows/desktop/aa370304(v=vs.85).aspx function netapibufferfree (buffer: pointer): netapistatus ; stdcall; external 'netapi32.dll'; // https://msdn.microsoft.com/en-us/library/windows/desktop/aa370652%28v=vs.85%29.aspx function netuserenum( servername: lpcwstr; level: dword; filter: dword; var bufptr: pointer; prefmaxlen: dword; var entriesread: dword; var totalentries: dword; resume_handle: lpdword ): netapistatus; stdcall; external 'netapi32.dll';
and procedure privlevel
privilege level asking users , users
tstringlist
populated user names.
function getusers(privlevel: tprivlevel; users: tstrings): integer; var i: integer; netapistatus: dword; bufptr: pointer; recptr: puser_info_2; entriesread, totalentries, hresume: dword; begin hresume := 0; repeat netapistatus := netuserenum( nil, // local 2, // user_info_2 0, // no special filter bufptr, max_preferred_length, entriesread, totalentries, @hresume ); if (netapistatus = nerr_success) or (netapistatus = error_more_data) begin recptr := bufptr; := 0 entriesread-1 begin if (privlevel = user_priv_any) or (recptr^.usri2_priv = privlevel) users.add(recptr^.usri2_name); inc(recptr); end; netapibufferfree(bufptr); end; until netapistatus <> error_more_data; result := netapistatus; end;
example of usage
procedure tform1.button1click(sender: tobject); var res: integer; begin res := getusers(user_priv_admin, memo.lines); if res <> 0 memo.lines.add('error getting users! error code '+inttostr(res)); end;
edit
i changed getusers()
function , return success/error code call netuserenum()
likely return values according documentation
nerr_success = 0 error_access_denied = 5; // $0005 error_invalid_level = 124; // $007c error_more_data = 234; // $00ea nerr_buftoosmall = 2123; // $084b nerr_invalidcomputer = 2351; // $092f
Comments
Post a Comment