src/Helpers.c
bool IsProcessElevated() {

  // When the process is run on operating systems prior to Windows 
  // Vista, GetTokenInformation returns FALSE with the 
  // ERROR_INVALID_PARAMETER error code because TokenElevation is 
  // not supported on those operating systems.
  if (!IsWindowsVistaOrGreater()) { return false; }

  bool bIsElevated = false;
  HANDLE hToken = NULL;
  Globals.dwLastError = ERROR_SUCCESS;
  const WCHAR* pLastErrMsg = L"";

  do {
    if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
    {
      TOKEN_ELEVATION elevationToken;
      DWORD expectedRetVal = sizeof(TOKEN_ELEVATION);
      DWORD dwReturnLength = 0;
      if (GetTokenInformation(hToken, TokenElevation, &elevationToken, expectedRetVal, &dwReturnLength))
      {
        if (dwReturnLength == expectedRetVal) {
          bIsElevated = elevationToken.TokenIsElevated;
        }
      }
    }
    else {
      Globals.dwLastError = GetLastError();
      pLastErrMsg = L"IsProcessElevated()";
      break;
    }
  } while (false); // Centralized cleanup for all allocated resources.

  if (hToken) {
    CloseHandle(hToken);
    hToken = NULL;
  }

  if (Globals.dwLastError != ERROR_SUCCESS) {
    MsgBoxLastError(pLastErrMsg, Globals.dwLastError);
  }

  return bIsElevated;
}
//=============================================================================
//
//  IsProcessElevated()
//
//   PURPOSE: The function gets the elevation information of the current 
//   process. It dictates whether the process is elevated or not. Token 
//   elevation is only available on Windows Vista and newer operating 
//   systems, thus IsProcessElevated throws a C++ exception if it is called 
//   on systems prior to Windows Vista. It is not appropriate to use this 
//   function to determine whether a process is run as administartor.
//
//   RETURN VALUE: Returns TRUE if the process is elevated. Returns FALSE if 
//   it is not.
//
//   NOTE: TOKEN_INFORMATION_CLASS provides TokenElevationType to check the 
//   elevation type (TokenElevationTypeDefault / TokenElevationTypeLimited /
//   TokenElevationTypeFull) of the process. It is different from 
//   TokenElevation in that, when UAC is turned off, elevation type always 
//   returns TokenElevationTypeDefault even though the process is elevated 
//   (Integrity Level == High). In other words, it is not safe to say if the 
//   process is elevated based on elevation type. Instead, we should use 
//   TokenElevation.
//
IsProcessElevated()
PURPOSE: The function gets the elevation information of the current
process. It dictates whether the process is elevated or not. Token
elevation is only available on Windows Vista and newer operating
systems, thus IsProcessElevated throws a C++ exception if it is called
on systems prior to Windows Vista. It is not appropriate to use this
function to determine whether a process is run as administartor.

RETURN VALUE: Returns TRUE if the process is elevated. Returns FALSE if
it is not.


IsProcessElevated () PURPOSE : The function gets the elevation information of the current process . It dictates whether the process is elevated or not . Token elevation is only available on Windows Vista and newer operating systems thus IsProcessElevated throws a C ++ exception if it is called on systems prior to Windows Vista . It is not appropriate to use this function to determine whether a process is run as administartor . RETURN VALUE : Returns TRUE if the process is elevated . Returns FALSE if it is not .
bool IsProcessElevated() {
  if (!IsWindowsVistaOrGreater()) { return false; }
  bool bIsElevated = false;
  HANDLE hToken = NULL;
  Globals.dwLastError = ERROR_SUCCESS;
  const WCHAR* pLastErrMsg = L"";
  do {
    if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
    {
      TOKEN_ELEVATION elevationToken;
      DWORD expectedRetVal = sizeof(TOKEN_ELEVATION);
      DWORD dwReturnLength = 0;
      if (GetTokenInformation(hToken, TokenElevation, &elevationToken, expectedRetVal, &dwReturnLength))
      {
        if (dwReturnLength == expectedRetVal) {
          bIsElevated = elevationToken.TokenIsElevated;
        }
      }
    }
    else {
      Globals.dwLastError = GetLastError();
      pLastErrMsg = L"IsProcessElevated()";
      break;
    }
  } while (false); 
  if (hToken) {
    CloseHandle(hToken);
    hToken = NULL;
  }
  if (Globals.dwLastError != ERROR_SUCCESS) {
    MsgBoxLastError(pLastErrMsg, Globals.dwLastError);
  }
  return bIsElevated;
}
bool IsProcessElevated ( ) { if ( ! IsWindowsVistaOrGreater ( ) ) { return false ; } bool bIsElevated = false ; HANDLE hToken = NULL ; Globals . dwLastError = ERROR_SUCCESS ; const WCHAR * pLastErrMsg = L" " ; do { if ( OpenProcessToken ( GetCurrentProcess ( ) , TOKEN_QUERY , & hToken ) ) { TOKEN_ELEVATION elevationToken ; DWORD expectedRetVal = sizeof ( TOKEN_ELEVATION ) ; DWORD dwReturnLength = 0 ; if ( GetTokenInformation ( hToken , TokenElevation , & elevationToken , expectedRetVal , & dwReturnLength ) ) { if ( dwReturnLength == expectedRetVal ) { bIsElevated = elevationToken . TokenIsElevated ; } } } else { Globals . dwLastError = GetLastError ( ) ; pLastErrMsg = L" " ; break ; } } while ( false ) ; if ( hToken ) { CloseHandle ( hToken ) ; hToken = NULL ; } if ( Globals . dwLastError != ERROR_SUCCESS ) { MsgBoxLastError ( pLastErrMsg , Globals . dwLastError ) ; } return bIsElevated ; }
IsProcessElevated()
PURPOSE: The function gets the elevation information of the current
process.
IsProcessElevated () PURPOSE : The function gets the elevation information of the current process .
// When the process is run on operating systems prior to Windows  // Vista, GetTokenInformation returns FALSE with the  // ERROR_INVALID_PARAMETER error code because TokenElevation is  // not supported on those operating systems. // Centralized cleanup for all allocated resources.