IsUserAdminTest.cpp:Defines the entry point for the console application.//#include <Windows.h> #include <tcha r.h> #include <iostream>bool isuseradmin (); int _tmain (int argc, _tchar* argv[]) {printf_s ("%d\n", Isuseradmin ( )); return 0;} BOOL Isuseradmin () {bool b = FALSE; Sid_identifier_authority ntauthority = security_nt_authority; PSID administratorsgroup = NULL; b = AllocateAndInitializeSid (&ntauthority,2,security_builtin_domain_rid,domain_alias_rid_admins,0, 0, 0, 0, 0, 0, &administratorsgroup); if (b) {HANDLE Hthreadusertoken = null;if (!::openthreadtoken (:: GetCurrentThread (), Token_read,//must has Token_ Queryfalse,&hthreadusertoken)) {if (! CheckTokenMembership (Hthreadusertoken, Administratorsgroup, &b)) {b = FALSE;} if (administratorsgroup) {freesid (Administratorsgroup);}} if (Hthreadusertoken) {CloseHandle (Hthreadusertoken);}} return (b);}
Windows determines whether it is an administrator