Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C++
Article

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

Rate me:
Please Sign up or sign in to vote.
4.65/5 (12 votes)
18 Dec 20042 min read 91.4K   2.9K   39   11
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


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThanks Pin
Gigy12-Feb-08 11:45
Gigy12-Feb-08 11:45 
Questionwhat about win9x ? Pin
i_a_z13-Dec-06 21:53
i_a_z13-Dec-06 21:53 
GeneralNice article..But Pin
Mohammad Vossoghi19-Sep-06 9:17
Mohammad Vossoghi19-Sep-06 9:17 
Generalerror C2440: “static_cast” Pin
yeahbull20061-May-06 18:30
yeahbull20061-May-06 18:30 
GeneralRe: error C2440: “static_cast” Pin
ken5524-Oct-07 16:28
ken5524-Oct-07 16:28 
Generalcool Pin
Tranvuongtrung6-Apr-06 4:26
Tranvuongtrung6-Apr-06 4:26 
GeneralHIbernate Pin
Yulianto.27-Feb-05 14:06
Yulianto.27-Feb-05 14:06 
GeneralRe: HIbernate Pin
dc_20007-Aug-05 7:55
dc_20007-Aug-05 7:55 
GeneralClose Handle Pin
Member 5143153-Feb-05 22:02
Member 5143153-Feb-05 22:02 
GeneralWe Created Almost Same Project Pin
ThatsAlok26-Dec-04 18:27
ThatsAlok26-Dec-04 18:27 
GeneralRe: We Created Almost Same Project Pin
Ramanan.T27-Dec-04 3:57
Ramanan.T27-Dec-04 3:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.