Created
November 4, 2013 18:27
-
-
Save davidglezz/7307067 to your computer and use it in GitHub Desktop.
This function check that your app is elevated
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <windows.h> | |
// If have compile errors: | |
// Replace TokenElevation by (TOKEN_INFORMATION_CLASS)20 | |
// Uncomment next struct | |
/* | |
typedef struct _TOKEN_ELEVATION { | |
DWORD TokenIsElevated; | |
} TOKEN_ELEVATION, *PTOKEN_ELEVATION; | |
*/ | |
BOOL IsElevated() | |
{ | |
DWORD dwSize = 0; | |
HANDLE hToken = NULL; | |
BOOL bReturn = FALSE; | |
TOKEN_ELEVATION tokenInformation; | |
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) | |
return FALSE; | |
if (GetTokenInformation(hToken, TokenElevation, &tokenInformation, sizeof(TOKEN_ELEVATION), &dwSize)) | |
bReturn = (BOOL)tokenInformation.TokenIsElevated; | |
CloseHandle(hToken); | |
return bReturn; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
if (IsElevated()) | |
printf("Process is elevated\n"); | |
else | |
printf("Process is not elevated\n"); | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment