Gonzalez Posted September 7, 2009 Report Posted September 7, 2009 #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;} Quote