Jump to content
Gonzalez

[C++] IsAdmin

Recommended Posts

Posted
#include <windows.h>
#include <cstdio>
#include <iostream>
using namespace std;
bool IsAdmin()
{
UINT i;
bool bRetval = FALSE;
BOOL bSuccess;
HANDLE hAccessToken;
UCHAR InfoBuffer[1024];
PTOKEN_GROUPS ptgGroups = (PTOKEN_GROUPS) InfoBuffer;
DWORD dwInfoBufferSize;
PSID psidAdministrators;
SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
OSVERSIONINFO Info = {0};

Info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (GetVersionEx(&Info)) {
if ((Info.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)&&
(Info.dwMajorVersion==4))
return TRUE;
} else return FALSE;

if (!OpenThreadToken(GetCurrentThread(),TOKEN_QUERY,TRUE,&hAccessToken)) {
if (GetLastError() != ERROR_NO_TOKEN)
return FALSE;

if (!OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hAccessToken))
return FALSE;
}

bSuccess = GetTokenInformation(hAccessToken,TokenGroups,InfoBuffer,
sizeof(InfoBuffer),&dwInfoBufferSize);
CloseHandle(hAccessToken);

if (!bSuccess)
return FALSE;

if (!AllocateAndInitializeSid(&siaNtAuthority,2,SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,0,0,0,0,0,0,&psidAdministrators))
return FALSE;

for (i = 0; i < ptgGroups->GroupCount; i++) {
if (EqualSid(psidAdministrators,ptgGroups->Groups[i].Sid)) {
bRetval = TRUE;
break;
}
}

FreeSid(psidAdministrators);
return bRetval;

return 0;
}


int main() {
if(IsAdmin()) {
cout << "You are an admin" << endl;
} else {
cout << "You are NOT an admin" << endl;
}
return 0;
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...