How to delay shutdown / restart / hibernate in Windows 7 or Windows 8 in VC ++

I am using a vs2012 based app developed using C ++.

If the application is running and some user handlers and triggers restart / exit or hibernate, the restart / shutdown should be suspended until the application is processed.

After the application finishes processing, the windows must resume for restart / shutdown.

I would really appreciate if some links or example code might be available

thank

+3


source to share


1 answer


I did something like this



bool shutdownSystemWin(EShutdownActions action)
{
    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.
    UINT flags = EWX_FORCE;
    switch (action)
    {
    case EShutdownActions::Shutdown:
        flags |= EWX_SHUTDOWN;
        break;
    case EShutdownActions::PowerOff:
        flags |= EWX_POWEROFF;
        break;
    case EShutdownActions::SuspendToRAM:
        return SetSuspendState(FALSE, FALSE, FALSE);
    case EShutdownActions::Hibernate:
        return SetSuspendState(TRUE, FALSE, FALSE);
        break;
    case EShutdownActions::Reboot:
        flags |= EWX_REBOOT;
        break;
    case EShutdownActions::LogOff:
        flags |= EWX_LOGOFF;
        break;
    }

    if (!ExitWindowsEx(flags,
        SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
        SHTDN_REASON_MINOR_UPGRADE |
        SHTDN_REASON_FLAG_PLANNED))
        return false;

    //shutdown was successful
    return true;
}

      

+2


source







All Articles