Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A simple utility for shutting down your System

0.00/5 (No votes)
28 Mar 2003 1  
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.

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 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