Click here to Skip to main content
Licence 
First Posted 18 Dec 2004
Views 72,068
Bookmarked 35 times

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

By | 18 Dec 2004 | Article
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

About the Author

Ramanan.T

Software Developer (Senior)

Australia Australia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks PinmemberGigy11:45 12 Feb '08  
Questionwhat about win9x ? Pinmemberi_a_z21:53 13 Dec '06  
GeneralNice article..But PinmemberMohammad Vossoghi9:17 19 Sep '06  
Generalerror C2440: “static_cast” Pinmemberyeahbull200618:30 1 May '06  
GeneralRe: error C2440: “static_cast” Pinmemberken5516:28 24 Oct '07  
Generalcool PinmemberTranvuongtrung4:26 6 Apr '06  
GeneralHIbernate PinmemberYulianto AKA goodmast3r14:06 27 Feb '05  
GeneralRe: HIbernate Pinmemberdc_20007:55 7 Aug '05  
GeneralClose Handle PinmemberE.V.Prasad22:02 3 Feb '05  
Hi Ramanan,
 
When we Open the Access Token, I think we must close it before the application exits using Close handle. Even in documentation, it is written that OpenProcessToken remarks that Close handle should be used to Close the Handle.
 

 
Enamundram Venkata Prasad
GeneralWe Created Almost Same Project PinmemberThatsAlok18:27 26 Dec '04  
GeneralRe: We Created Almost Same Project PinmemberT.YogaRamanan3:57 27 Dec '04  

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.

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