Click here to Skip to main content
15,868,056 members
Articles / Desktop Programming / MFC
Article

A simple utility for shutting down your System

Rate me:
Please Sign up or sign in to vote.
2.73/5 (16 votes)
28 Mar 2003CPOL2 min read 96.5K   765   31   19
Simply set the time on the tool for the machine you wish to shut down (24 hrs), choose whether to log off, reboot or shutdown, or power off (ATX main boards only), and click Hide. It has been tested on Win9x/ME, WinNT/2000/XP.

Image 1

Introduction

This is a simple utility for shutting down your system.

Simply set the time on the tool, for the machine you wish to shut down (24 hrs), choose whether to log off, reboot or shutdown, or power off (ATX main boards only), and click Hide. It has been tested on Win9x/ME, WinNT/2000/XP. The full source code is in the Zip file.

In case of problems in getting it to work, when setting the end time make sure that you click on the number after that. It is a list box, so the GetCaretIndex() call will always return 0 unless you select a number by clicking on it.

What's new in this tool?

  1. Well, old wine in a new bottle. The nasty code in OnInitDialog() is totally replaced by a new one. Now some of my colleagues will neither get totally shutdown by looking at that code ;-D nor will it look nasty!!!.
  2. Dialog refreshing is toggled by the flag, so that it doesn't get unnecessarily repainted (when clicked on menu "Open main window").
  3. Ability to abort shutdown in progress through message box.

What is really missing?

  • The ability to shut down remote computers.

I don't have network here. But if you want to remotely shutdown the computer use the following function: BOOL InitiateSystemShutdown. It has the following parameters:

  1. pointer to the name of the computer to be shutdown,
  2. pointer to the message to be displayed in the dialog box,
  3. time to display the dialog box,
  4. force applications closed flag,
  5. reboot flag.

The first parameter takes the name of the computer in the network. If it's null the local machine will be down.

Working

As soon as you run/compile, it will be passed to the system tray. OnInitDialog() will fill the list box with hrs/mins/secs as follows:

CListBox* pListHrs = static_cast(GetDlgItem(IDC_LISTHRS));
while(i<=24) { _itoa(i,buff,10); pListHrs->AddString(buff); i++;}

Timer will start its execution and the user's time will be checked at regular intervals. As soon as the condition is met, the selected option will be passed to Sflag. This flag in turn is checked in the switch statement and the following code executes:

Switch(Sflag) {
    case 0: CDialog::KillTimer (nIDEvent); 
            ExitWindowsEx(EWX_SHUTDOWN ,EWX_FORCE);
            exit(0);
            break; 
    .
    .
    .
}

If you have recent versions of Visual Studio and MSDN (which, I don't have), you can simply add hibernate. You can use EWX_FORCEIFHUNG instead of EWX_FORCE, sometimes for Windows NT 5.0 and later: it forces processes to terminate if they do not respond to the WM_QUERYENDSESSION or WM_ENDSESSION message. This flag is ignored if EWX_FORCE is used.

Credits

As far as the different classes that are used in my utility, appropriate credits are given to:

  • CHoverButton by Niek Albers.
  • Auto Font by Keith Rule.
  • System Tray by Chris Maunder.
  • On line MSDN by Microsoft.

tray.cpp and tray.h were originally named as systemtray.cpp and systemtray.h at CodeProject. All the above class files were downloaded from CodeProject. Thanks to all those who helped me directly or indirectly for this (and other utilities). Special thanks to all those people who maintain CodeProject.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Computer Engineer. Pune univ.

Comments and Discussions

 
Generalshutdown -i Pin
Vasudevan Deepak Kumar17-Aug-07 4:42
Vasudevan Deepak Kumar17-Aug-07 4:42 
GeneralError "A required privilege is not held by the client" Pin
GDoubleD2-Apr-03 5:02
GDoubleD2-Apr-03 5:02 
GeneralRe: Error "A required privilege is not held by the client" Pin
Balkrishna Talele8-Apr-03 9:39
Balkrishna Talele8-Apr-03 9:39 
GeneralRe: Error "A required privilege is not held by the client" Pin
beastonabun15-Apr-03 8:34
beastonabun15-Apr-03 8:34 
GeneralRe: Error "A required privilege is not held by the client" Pin
Anonymous18-Apr-03 7:04
Anonymous18-Apr-03 7:04 
GeneralRe: Error "A required privilege is not held by the client" Pin
GDoubleD24-Apr-03 5:13
GDoubleD24-Apr-03 5:13 
GeneralRe: Error &quot;A required privilege is not held by the client&quot; Pin
fato28-Aug-03 0:42
fato28-Aug-03 0:42 
GeneralWhen are you... Pin
Rickard Andersson2029-Mar-03 10:12
Rickard Andersson2029-Mar-03 10:12 
GeneralRe: When are you... Pin
mwilliamson29-Mar-03 11:22
mwilliamson29-Mar-03 11:22 
GeneralRe: When are you... Pin
Rickard Andersson2030-Mar-03 5:48
Rickard Andersson2030-Mar-03 5:48 
GeneralRe: When are you... Pin
Alexander Wiseman30-Mar-03 10:16
Alexander Wiseman30-Mar-03 10:16 
GeneralRe: When are you... Pin
Andreas Saurwein30-Mar-03 22:35
Andreas Saurwein30-Mar-03 22:35 
GeneralRe: When are you... Pin
Gareth Maguire18-Mar-04 5:49
Gareth Maguire18-Mar-04 5:49 
GeneralRe: When are you... Pin
Gareth Maguire18-Mar-04 5:51
Gareth Maguire18-Mar-04 5:51 
GeneralRe: When are you... Pin
Vasudevan Deepak Kumar17-Aug-07 4:44
Vasudevan Deepak Kumar17-Aug-07 4:44 
GeneralRe: When are you... Pin
Balkrishna Talele30-Mar-03 7:20
Balkrishna Talele30-Mar-03 7:20 
GeneralRe: When are you... Pin
Rickard Andersson2030-Mar-03 20:23
Rickard Andersson2030-Mar-03 20:23 
GeneralRe: When are you... Pin
Balkrishna Talele31-Mar-03 5:31
Balkrishna Talele31-Mar-03 5:31 
GeneralRe: When are you... Pin
Rickard Andersson201-Apr-03 7:42
Rickard Andersson201-Apr-03 7:42 

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.