enable debug privilege

enable privilege function:

#include 

#pragma comment(lib, "advapi32.lib")


bool fnenable_privilege(const char* privilege_name = SE_DEBUG_NAME)
{
    TOKEN_PRIVILEGES tp = {0};
    void* htoken = NULL;
    bool bret = false;

    tp.PrivilegeCount = 1;
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;// or 'FALSE' to disable.

    if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES, &htoken))
        return false;

    if(LookupPrivilegeValue(NULL, privilege_name, &(tp.Privileges[0].Luid)))
        if(AdjustTokenPrivileges(htoken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
            bret = true;

    CloseHandle(htoken);
    return bret;
}

你可能感兴趣的:(vc,windows)