This will check the application if it is executed with administrative privileges. Perhaps this can be added as a parameter in the manifest, but this is useful nonetheless.
Include header
#include <stdbool.h>
Check privilege
bool IsAdmin(void) {
BOOL isAdmin;
SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY;
PSID adminGroup;
if (!AllocateAndInitializeSid(&ntAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &adminGroup)) {
return false;
}
if (!CheckTokenMembership(NULL, adminGroup, &isAdmin)) {
FreeSid(adminGroup);
return false;
}
FreeSid(adminGroup);
return isAdmin != 0;
}
Usage
static INT_PTR CALLBACK MainDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
if (!IsAdmin()) {
MessageBox(hwndDlg, L"Must be in Administrator mode.", L"MyApp", MB_OK | MB_ICONWARNING);
EndDialog(hwndDlg, 0);
DestroyWindow(hwndDlg);
return TRUE;
}
// other cases
}
return FALSE;
}