bool isRunningAsAdmin()
{
BOOL runningAsAdmin = false;
PSID adminGroupSid = NULL; // allocate and initialize a SID of the administrators group.
SID_IDENTIFIER_AUTHORITY NtAuthority = { SECURITY_NT_AUTHORITY };
if (AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&adminGroupSid))
{
// determine whether the SID of administrators group is enabled in
// the primary access token of the process.
CheckTokenMembership(NULL, adminGroupSid, &runningAsAdmin);
} if (adminGroupSid)
{
FreeSid(adminGroupSid);
} return runningAsAdmin;
}