判断当前运行权限是否是system

方法一:

BOOL CurrentUserIsLocalSystem()
{
BOOL bIsLocalSystem = FALSE;
PSID psidLocalSystem;
SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY;

BOOL fSuccess = ::AllocateAndInitializeSid(&ntAuthority, 1, SECURITY_LOCAL_SYSTEM_RID,
0, 0, 0, 0, 0, 0, 0, &psidLocalSystem);
if (fSuccess)
{
fSuccess = ::CheckTokenMembership(0, psidLocalSystem, &bIsLocalSystem);
::FreeSid(psidLocalSystem);
}

return bIsLocalSystem;
}

 

方法二:

bool IsSystemPrivilegeImp()
{
static bool isSystemPrivilege = false;

if (isSystemPrivilege)
{
return isSystemPrivilege;
}

char szPath[MAX_PATH] = { 0 };

if (::SHGetSpecialFolderPathA(NULL, szPath, CSIDL_APPDATA, TRUE))
{
std::string flag("config\\systemprofile");
std::string path(szPath);
if (path.find(flag) != std::string::npos)
{
isSystemPrivilege = true;
}
}
return isSystemPrivilege;
}

 

posted @ 2019-07-03 10:01  iwinbug  阅读(863)  评论(0)    收藏  举报