Click here to Skip to main content
11,720,324 members (81,725 online)
Click here to Skip to main content

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

, 18 Dec 2004 80.1K 2.7K 38
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

You may also be interested in...

Comments and Discussions

 
GeneralThanks Pin
Gigy12-Feb-08 11:45
memberGigy12-Feb-08 11:45 
Questionwhat about win9x ? Pin
i_a_z13-Dec-06 21:53
memberi_a_z13-Dec-06 21:53 
GeneralNice article..But Pin
Mohammad Vossoghi19-Sep-06 9:17
memberMohammad Vossoghi19-Sep-06 9:17 
Generalerror C2440: “static_cast” Pin
yeahbull20061-May-06 18:30
memberyeahbull20061-May-06 18:30 
GeneralRe: error C2440: “static_cast” Pin
ken5524-Oct-07 16:28
memberken5524-Oct-07 16:28 
Generalcool Pin
Tranvuongtrung6-Apr-06 4:26
memberTranvuongtrung6-Apr-06 4:26 
GeneralHIbernate Pin
Yulianto AKA goodmast3r27-Feb-05 14:06
memberYulianto AKA goodmast3r27-Feb-05 14:06 
GeneralRe: HIbernate Pin
dc_20007-Aug-05 7:55
memberdc_20007-Aug-05 7:55 
GeneralClose Handle Pin
E.V.Prasad3-Feb-05 22:02
memberE.V.Prasad3-Feb-05 22:02 
GeneralWe Created Almost Same Project Pin
ThatsAlok26-Dec-04 18:27
memberThatsAlok26-Dec-04 18:27 
GeneralRe: We Created Almost Same Project Pin
T.YogaRamanan27-Dec-04 3:57
memberT.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 | Terms of Use | Mobile
Web02 | 2.8.150901.1 | Last Updated 18 Dec 2004
Article Copyright 2004 by Ramanan.T
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid