How do I check if a user has local admin privileges in win32 from c++
2
-
Dupe? stackoverflow.com/questions/560366/… Answers point to win32 api solutions.– AnonymousFeb 24, 2009 at 10:21
-
3Not exactly the same with UAC under Vista. "Running with admin privileges" now implies the user has admin rights and used them when starting a process.– MSaltersMar 4, 2009 at 13:16
Add a comment
|
2 Answers
Just found IsUserAnAdmin() in shlobj.h which does the job for me.
-
Being an admin does not mean that the current process runs with elevated privileges Sep 8, 2014 at 9:23
-
Caution: IsUserAnAdmin() is deprecated: "It may be altered or unavailable in subsequent versions." Last supported client version: Windows Vista! Nov 23, 2015 at 17:00
-
oli fortunately I only needed it for XP support, for Vista and later I can check for elevation. Nov 24, 2015 at 19:07
-
You might need more than that to deal with elevation and such like...
I do it like this....
bool CProcessToken::IsUserAnAdmin() const
{
#if _WIN32_WINNT >= 0x0600
bool isAdmin = false;
DWORD bytesUsed = 0;
TOKEN_ELEVATION_TYPE tokenElevationType;
if (!::GetTokenInformation(m_hToken, TokenElevationType, &tokenElevationType, sizeof(tokenElevationType), &bytesUsed))
{
const DWORD lastError = ::GetLastError();
throw CWin32Exception(_T("CProcessToken::IsUserAnAdmin() - GetTokenInformation - TokenElevationType"), lastError);
}
if (tokenElevationType == TokenElevationTypeLimited)
{
CSmartHandle hUnfilteredToken;
if (!::GetTokenInformation(m_hToken, TokenLinkedToken, reinterpret_cast<void *>(hUnfilteredToken.GetHandle()), sizeof(HANDLE), &bytesUsed))
{
const DWORD lastError = ::GetLastError();
throw CWin32Exception(_T("CProcessToken::IsUserAnAdmin() - GetTokenInformation - TokenLinkedToken"), lastError);
}
BYTE adminSID[SECURITY_MAX_SID_SIZE];
DWORD sidSize = sizeof(adminSID);
if (!::CreateWellKnownSid(WinBuiltinAdministratorsSid, 0, &adminSID, &sidSize))
{
const DWORD lastError = ::GetLastError();
throw CWin32Exception(_T("CProcessToken::IsUserAnAdmin() - CreateWellKnownSid"), lastError);
}
BOOL isMember = FALSE;
if (::CheckTokenMembership(hUnfilteredToken, &adminSID, &isMember))
{
const DWORD lastError = ::GetLastError();
throw CWin32Exception(_T("CProcessToken::IsUserAnAdmin() - CheckTokenMembership"), lastError);
}
isAdmin = (isMember != FALSE);
}
else
{
isAdmin = ToBool(::IsUserAnAdmin());
}
return isAdmin;
#else
return ToBool(::IsUserAnAdmin());
#endif
}
I can't remember where I got the information from to write that bit of code though...
-
1Fortunately I can ignore elevation at the moment as I request elevation in the manifest, this is just for pre Vista installs. Feb 24, 2009 at 12:21
-
1@Len Holgate do you know if this works for members of a domain admin as well (not just local machine admins, which I verified to be working ok)– OSHNov 19, 2012 at 10:33
-
m_hToken is not defined, CWin32Exception is not defined, CSmartHandle is not defined, ToBool() is not defined. So this code only works when _WIN32_WINNT < 0x0600, but uses the deprecated ::IsUserAnAdmin() then. Nov 23, 2015 at 16:58
-
It's not supposed to be compilable, it simply shows the API calls to make - fill in the detail yourself. Nov 24, 2015 at 15:44