Click here to Skip to main content
Click here to Skip to main content

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

, 18 Dec 2004
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Ramanan.T
Software Developer (Senior)
Australia Australia
No Biography provided

Comments and Discussions

 
GeneralThanks PinmemberGigy12-Feb-08 11:45 
Questionwhat about win9x ? Pinmemberi_a_z13-Dec-06 21:53 
GeneralNice article..But PinmemberMohammad Vossoghi19-Sep-06 9:17 
Generalerror C2440: “static_cast” Pinmemberyeahbull20061-May-06 18:30 
GeneralRe: error C2440: “static_cast” Pinmemberken5524-Oct-07 16:28 
Generalcool PinmemberTranvuongtrung6-Apr-06 4:26 
GeneralHIbernate PinmemberYulianto AKA goodmast3r27-Feb-05 14:06 
GeneralRe: HIbernate Pinmemberdc_20007-Aug-05 7:55 
GeneralClose Handle PinmemberE.V.Prasad3-Feb-05 22:02 
GeneralWe Created Almost Same Project PinmemberThatsAlok26-Dec-04 18:27 
GeneralRe: We Created Almost Same Project PinmemberT.YogaRamanan27-Dec-04 3:57 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.1 | Last Updated 18 Dec 2004
Article Copyright 2004 by Ramanan.T
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid