0

Sorry for my English, it might be a bit wonky due to Dyslexia. Working on a project using only pure C++ and windows API to make a service. If situation X happens, I want it log the user out of windows.

BOOL EmergencyShutdown()
{
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;

    // Get a token for this process. 

    if (!OpenProcessToken(GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
        return(FALSE);

    // Get the LUID for the shutdown privilege. 

    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
        &tkp.Privileges[0].Luid);

    tkp.PrivilegeCount = 1;  // one privilege to set    
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    // Get the shutdown privilege for this process. 

    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
        (PTOKEN_PRIVILEGES)NULL, 0);

    if (GetLastError() != ERROR_SUCCESS)
        return FALSE;

    // Shut down the system and force all applications to close. 
    if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,
        SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
        SHTDN_REASON_MINOR_SECURITY |
        SHTDN_REASON_MINOR_UNSTABLE))
        return FALSE;

    //shutdown was successful
    return TRUE;
}

This is the code the service run to shut the PC down. This works. When the service trigger EmergencyShutdown the PC is shutdown. EWX_LOGOFF does not work. From what I can tell, EWX_LOGOFF Demands that the session is interactive and thus I have run into a wall.

I have tried to start the service from Task Scheduler with SYSTEM, also tried running with maximum privs. Another thing I tried was changing the service options to run it with login information and also tried local account with "desktop changing" checkbox ticked.

Been looking around for a pure C++ solution to this in service form, but found nothing that I feel fits or work. Help MUCH loved.

0 Answers0