src/Helpers.c
bool IsRunAsAdmin()
{
  BOOL fIsRunAsAdmin = FALSE;
  PSID pAdministratorsGroup = NULL;

  Globals.dwLastError = ERROR_SUCCESS;
  const WCHAR* pLastErrMsg = L"";

  do {
    // 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,
      &pAdministratorsGroup))
    {
      Globals.dwLastError = GetLastError();
      pLastErrMsg = L"AllocateAndInitializeSid()";
      break;
    }

    // Determine whether the SID of administrators group is enabled in 
    // the primary access token of the process.
    if (!CheckTokenMembership(NULL, pAdministratorsGroup, &fIsRunAsAdmin))
    {
      Globals.dwLastError = GetLastError();
      pLastErrMsg = L"CheckTokenMembership()";
      break;
    }

  } while (false); // Centralized cleanup for all allocated resources.

  // Centralized cleanup for all allocated resources.
  if (pAdministratorsGroup)
  {
    FreeSid(pAdministratorsGroup);
    pAdministratorsGroup = NULL;
  }

  return (bool)fIsRunAsAdmin;
}
//=============================================================================
// 
//   IsRunAsAdmin()
//
//   PURPOSE: The function checks whether the current process is run as 
//   administrator. In other words, it dictates whether the primary access 
//   token of the process belongs to user account that is a member of the 
//   local Administrators group and it is elevated.
//
//   RETURN VALUE: Returns TRUE if the primary access token of the process 
//   belongs to user account that is a member of the local Administrators 
//   group and it is elevated. Returns FALSE if the token does not.
//
IsRunAsAdmin()
PURPOSE: The function checks whether the current process is run as
administrator. In other words, it dictates whether the primary access
token of the process belongs to user account that is a member of the
local Administrators group and it is elevated.

RETURN VALUE: Returns TRUE if the primary access token of the process
belongs to user account that is a member of the local Administrators
group and it is elevated. Returns FALSE if the token does not.
IsRunAsAdmin () PURPOSE : The function checks whether the current process is run as administrator . In other words it dictates whether the primary access token of the process belongs to user account that is a member of the local Administrators group and it is elevated . RETURN VALUE : Returns TRUE if the primary access token of the process belongs to user account that is a member of the local Administrators group and it is elevated . Returns FALSE if the token does not .
bool IsRunAsAdmin()
{
  BOOL fIsRunAsAdmin = FALSE;
  PSID pAdministratorsGroup = NULL;
  Globals.dwLastError = ERROR_SUCCESS;
  const WCHAR* pLastErrMsg = L"";
  do {
    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,
      &pAdministratorsGroup))
    {
      Globals.dwLastError = GetLastError();
      pLastErrMsg = L"AllocateAndInitializeSid()";
      break;
    }
    if (!CheckTokenMembership(NULL, pAdministratorsGroup, &fIsRunAsAdmin))
    {
      Globals.dwLastError = GetLastError();
      pLastErrMsg = L"CheckTokenMembership()";
      break;
    }
  } while (false); 
  if (pAdministratorsGroup)
  {
    FreeSid(pAdministratorsGroup);
    pAdministratorsGroup = NULL;
  }
  return (bool)fIsRunAsAdmin;
}
bool IsRunAsAdmin ( ) { BOOL fIsRunAsAdmin = FALSE ; PSID pAdministratorsGroup = NULL ; Globals . dwLastError = ERROR_SUCCESS ; const WCHAR * pLastErrMsg = L" " ; do { 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 , & pAdministratorsGroup ) ) { Globals . dwLastError = GetLastError ( ) ; pLastErrMsg = L" " ; break ; } if ( ! CheckTokenMembership ( NULL , pAdministratorsGroup , & fIsRunAsAdmin ) ) { Globals . dwLastError = GetLastError ( ) ; pLastErrMsg = L" " ; break ; } } while ( false ) ; if ( pAdministratorsGroup ) { FreeSid ( pAdministratorsGroup ) ; pAdministratorsGroup = NULL ; } return ( bool ) fIsRunAsAdmin ; }
IsRunAsAdmin()
PURPOSE: The function checks whether the current process is run as
administrator.
IsRunAsAdmin () PURPOSE : The function checks whether the current process is run as administrator .
// Allocate and initialize a SID of the administrators group. // Determine whether the SID of administrators group is enabled in  // the primary access token of the process. // Centralized cleanup for all allocated resources. // Centralized cleanup for all allocated resources.