The VC's way of determining whether a process has administrator rights

  • 2020-04-02 02:55:11
  • OfStack

This article illustrates how a VC can determine whether a process has admin privileges. Returns TRUE if yes, FALSE otherwise. Share with you for your reference. The specific implementation method is as follows:

static BOOL IsAdmin(void)  

     HANDLE                   hAccessToken; 
     BYTE                     InfoBuffer[1024];    
     PTOKEN_GROUPS            ptgGroups; 
     DWORD                    dwInfoBufferSize; 
     PSID                     psidAdministrators; 
     SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY; 
     UINT                     i; 
     BOOL                     bRet = FALSE; 
  
     if(!OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hAccessToken))
        return bRet;          
     bRet = GetTokenInformation(hAccessToken, TokenGroups, InfoBuffer, 1024, &dwInfoBufferSize);
     CloseHandle(hAccessToken); 
     if(!bRet) 
        return bRet;
     if(!AllocateAndInitializeSid(&siaNtAuthority, 
                                  2, 
                                  SECURITY_BUILTIN_DOMAIN_RID, 
                                  DOMAIN_ALIAS_RID_ADMINS, 
                                  0,0,0,0,0,0, 
                                  &psidAdministrators))          
        return FALSE;                                 
     bRet = FALSE;    
     ptgGroups = (PTOKEN_GROUPS)InfoBuffer; 
     for(i=0;i<ptgGroups->GroupCount;i++)      
         if(EqualSid(psidAdministrators,ptgGroups->Groups[i].Sid))       
             bRet = TRUE;                 
     FreeSid(psidAdministrators);  
     return bRet;
}

Hope that this article described the VC programming for you to help.


Related articles: