Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A tool to perform automatic shutdown, log off or restart action

0.00/5 (No votes)
18 Dec 2004 1  
This is a simple tool to perform automatic shutdown, log off or restart your computer on specific time. This tool will run on system tray when minimized.

Sample Image - StayON.jpg

Introduction

This is a simple tool to perform automatic shutdown, log off or restart your computer on specific time. This is very useful when you want to logoff, switch off or restart your computer after some time when you are not near to your computer, for example say you want to shutdown your computer after 3 or 4 hours after completing download or defragmentation. For that select the radio button "Switch off this computer after" and specify 240 minutes on the edit box and click apply.

This tool also teaches the techniques behind tray icon manipulation through simplified code.

In detail

Two timers will be started when Applied ("Apply"). One timer will display the blinking icon and the other will check the time and perform the action. The action could be any one of these.

  • Shutdown
  • Log off
  • Restart

The code below is the code to perform the action. To perform these actions the logged in user must have TOKEN_ADJUST_PRIVILEGES and TOKEN_QUERY privileges. Otherwise this will fail.

void CStayONDlg::OnAction()
{
    HANDLE hToken; 
    TOKEN_PRIVILEGES tkp; 
 
    if (OpenProcessToken(    GetCurrentProcess(),
                TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, 
                & hToken)) 
    {// open and check the privileges for to perform the actions


        LookupPrivilegeValue(    NULL, 
                    SE_SHUTDOWN_NAME, 
                    & tkp.Privileges[0].Luid); 
         
        tkp.PrivilegeCount = 1; 
        tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 

        if(AdjustTokenPrivileges(    hToken, 
                        FALSE, 
                        & tkp, 
                        0, 
                        (PTOKEN_PRIVILEGES)NULL, 
                        0))
        {/// adjust the privilege to perform the action

            if(m_dwAction & STAT_SWOFF)
                ExitWindowsEx(    EWX_SHUTDOWN|
                        EWX_POWEROFF|
                        EWX_FORCE,
                        0);
            else if(m_dwAction & STAT_SWOFFTIMER)
                ExitWindowsEx(    EWX_SHUTDOWN|
                        EWX_POWEROFF|
                        EWX_FORCE,
                        0);
            else if(m_dwAction & STAT_LOGOFF)
                ExitWindowsEx(    EWX_LOGOFF|
                        EWX_FORCE,
                        0);
            else if(m_dwAction & STAT_LOGOFFTIMER)
                ExitWindowsEx(    EWX_LOGOFF|
                        EWX_FORCE,
                        0);
            else if(m_dwAction & STAT_RESTART)
                ExitWindowsEx(    EWX_REBOOT|
                        EWX_FORCE,
                        0);
            else if(m_dwAction & STAT_RESTARTTIMER)
                ExitWindowsEx(    EWX_REBOOT|
                        EWX_FORCE,
                        0);
        }
    }

    int err = GetLastError();
    if(err)
    {// if any error occurs the report to the user

        LPVOID lpMsgBuf;
        FormatMessage(    FORMAT_MESSAGE_ALLOCATE_BUFFER|
                FORMAT_MESSAGE_FROM_SYSTEM, 
                NULL,
                err, 
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                (LPTSTR) & lpMsgBuf, 
                0, 
                NULL);
        MessageBox(    (LPTSTR)lpMsgBuf, 
                "StayON", 
                MB_OK|MB_ICONERROR);
        LocalFree(lpMsgBuf);
    }
    
    // make sure you quit after issueing any commands

    PostQuitMessage(0);
    exit(0);
}

EWX_FORCE flag will ignore any process running. If this is removed, shutting down, logging off or restarting actions will wait for other process to complete. For example, if a document is opened in MS-Word and not saved then shutdown, logoff or restart actions will wait for MS-Word to close. i.e., it will wait for the user to click save on MS-Word document. For more detail please refer ExitWindowsEx in MSDN.

Conclusion

Hope this tool will be useful to some one out there. I want to remind you that I'm not responsible for any damage caused by this tool. Thanks.

Warning: This tool has no capabilities to detect any on going process on your computer. It will shutdown, log off or restart regardless of any process running. So be careful on predicting the time to shutdown, restart or log off.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here