Pelles C forum

C language => User contributions => Topic started by: WiiLF23 on August 15, 2023, 04:13:23 AM

Title: Check Application Privilages
Post by: WiiLF23 on August 15, 2023, 04:13:23 AM
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
Code: [Select]
#include <stdbool.h>

Check privilege
Code: [Select]
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
Code: [Select]
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;
}
Title: Re: Check Application Privilages
Post by: frankie on August 15, 2023, 10:37:41 AM
 :)
Title: Re: Check Application Privilages
Post by: Vortex on August 15, 2023, 11:36:44 AM
Hello,

Nice work. Another method presented by the tool Reg Converter is to use the batch file portion below :

Code: [Select]
:IsAdmin
Reg.exe query "HKU\S-1-5-19\Environment"
If Not %ERRORLEVEL% EQU 0 (
 Cls & Echo You must have administrator rights to continue ...
 Pause & Exit
)
Cls
goto:eof

https://www.sordum.org/8478/reg-converter-v1-2/
Title: Re: Check Application Privilages
Post by: John Z on August 15, 2023, 12:34:24 PM
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.

Very nice!

John Z