The Shutdown Alarm

Source: Internet
Author: User
ArticleDirectory
    • Using in �at way :-
    • Using in way
    • Download Demo project-9.75 KB
    • Download source-15.7 KB

 

Introduction

Before going in detail about the application, let me tell you what this application will do? As the name suggests, it is used for shutdown, log off or restarting of the computer at a user defined time.

So what is its use?

today, the world is going digitized. every thing is running day & night, but not human body; it needs rest. think of a situation when at night, you want to download your favorite song or favorite software, or you want to defragment your hard disk or burn a CDR, or do some time consuming work, where your sitting for it is not required, but you have to sit, waiting for the task to complete so that you can switch off your PC, as if it is left open, it will waste precious net usage, atomicity, and electrical component life of your PC.

So one day, me and my friend came up with a solution: shutdown Timer/alarm, that will shutdown your computer at a user defined time. it provides both in and at option; means, for e.g ., you can shutdown your computerIn2 hrs or you can shutdown your computerAt1.00 PM. Me and my friend decided to release the software with source code so that other users get benefited with it and also we can find bugs in our application.

So how am I Achieving it? (What is the code behind it ?)

Basic power behind this software is some Win32 APIs, that provide support for shutdown of the computer. There are two APIs to achieve it; these are:

  • exitdomainwex

    collapse copy Code
     bool  exitwindowsex  (uint  uflags ,  //   shutdown operation  DWORD  dwreason   //   shutdown reason  ); 

    advantage: works with all operating systems.

  • initiatesystemshutdown collapse copy Code
     bool  initiatesystemshutdown  (lptstr  lpmachinename ,  //   computer name  lptstr  lpmessage ,  //   message to display  DWORD  dwtimeout 
         ,  //   length of time to display  bool  bforceappsclosed ,  //   force closed option  bool  brebootaftershutdown 
            //   Reboot option  ); 

    disadvantage: no support for Windows 9x/ME.

To provide backward sompatibility, I have written this software usingExitdomainwexAPI.

Some code snippets

To shutdown the computer in Windows 9x/me is very simple. Just callExitdomainwexWith required parameters to perform system command. I. e., this way:

Collapse Copy code
 
Exitdomainwex (ewx_shutdown,0);

Oh, I forgot to tell you about the varous flags provided to perform different tasksExitdomainwex. Here are the options:

    • Ewx_logoff: Logoff the current user.
    • Ewx_restart: Simply restart the computer.
    • Ewx_shutdown: Shut down but 'at' style.
    • Ewx_poweroff: Shut down but 'atx' style.

For Forcibly shutting off your computer, you can useEwx_forceIn combination with above four flags. now, it is not that easy to shut down your computer under Windows Multi-user systems like window 2000 and XP (built on NT technology); you have to acquire the Security Privilege knowSe_shutdown_nameTo provide successful shutdown action.

Here is a code snippet to achieve that:

Collapse Copy code
  Handle Htoken; // Handle to process token   Token_privileges Tkp; //  Pointer to token Structure   Openprocesstoken (Getcurrentprocess (), token_adjust_privileges | token_query, & htoken ); //  Get the luid for shutdown privilege.   Lookupprivilegevalue (Null, se_shutdown_name, & tkp. Privileges [ 0 ]. Luid); tkp. privilegecount = 1 ; //  One privilege to set    // Get shutdown privilege for this process. Tkp. Privileges [ 0 ]. Attributes = Se_privilege_enabled ; Adjusttokenprivileges (Htoken, false, & tkp, (ptoken_privileges) null, 0 ); //  Cannot test the return value of adjusttokenprivileges.    If ( Getlasterror () ! = Error_success) {MessageBox ( "  Adjusttokenprivileges enable failed ." ); Return ;}

Note: If you don't enableSe_shutdown_namePrivilege, your computer will just logoff afterExitdomainwexOrInitiatesystemshutdownAPI is called.

About System Tray Interface

This software has System Tray interface, I. e., it can be operated from System Tray. If you don't know where system tray is, here I locate that for you:

System Tray

Now you are wondering how to achieve that. It is very simple using the simple API namesShell_policyicon. Here is the function declarationShell_policyiconAPI.

Collapse Copy code
BoolShell_policyicon(DWORDDwmessage,PnotifyicondataLpdata );

Here are possible valuesDwmessage:

    • Nim_add: Add icon to system tray.
    • Nim_delete: Delete icon from system tray.
    • Nim_modify: Modify the system tray icon.

(For e.g., when you connect to Yahoo! Chat using Yahoo! Messenger, the icon becomes active. And when you logoff, the icon becomes inactive ).

And for Parameter Two, I. e .,Lpdata, It is a pointer toNotifyicondataStructure which is some thing like this:

Collapse Copy code
Typedef Struct _ Policyicondata{DWORDCbsize; HwndHwnd; UintUID; UintUflags; UintUcallbackmessage; HiconHicon;# If(_ Win32_ie<0x0500) tcharSztip[64];# ElseTcharSztip[128];# Endif) DWORDDwstate;}Notifyicondata, * ppolicyicondata;

Here is a little information for each:

    • cbsize : this variable is created to provide compatibility with other versions of policyicondata .
    • hicon : icon for the system tray.
    • hwnd : handle to window which will handle the system tray icon message.
    • sztip : Show tip when mouse hovers over icon.
    • callbackmessage : application-defined message identifier. the system uses this identifier to send identiications to the window identified in hwnd . these events are sent when a mouse event occurs in the bounding rectangle of the icon, or when the icon is selected or activated with the keyboard.
    • uflag : this flag notifies the system which of the above variables are to be used or not.
    • uid : Any unique ID for icon.

Now, how to use that! That's very simple. Just fill the required details and give the proper handle of the icon toHiconParameter to successfully plant an icon in the system bar. Let's say you have two handles for the icon:Hiconsleep,Hiconwake.

Now, here I initialize the structure:

Collapse Copy code
# Define My_messageWm_app + 75 ....NotifyicondataM_nidata;//Handling the icon at System Tray IconM_nidata.cbsize = sizeof (policyicondata); m_nidata.hicon = hiconsleep; m_nidata.hwnd = This->M_hwnd; sprintf (m_nidata.sztip,"Shut down alarm: My father Software Inc @ 2003"); M_nidata.ucallbackmessage = my_message; m_nidata.uflags = nif_icon | nif_message | nif_tip; m_nidata.uid = 10001 ;......

ForAddingThe icon to System Tray:

Collapse Copy code
 
Shell_policyicon(Nim_add, & m_nidata );

ForModifyingThe icon:

Collapse Copy code
 
//Now I am planting new icon just like Yahoo//Does by showing online and offline statusM_nidata.hicon = hiconwake;Shell_policyicon(Nim_modify, & m_nidata );

ForDeletingThe icon from System Tray:

Collapse Copy code
 
Shell_policyicon(Nim_delete, & m_nidata );

Now, because of you must be wondering: When we click or right click the icon of any application on the system tray, a menu becomes visible; now how to get that menu? If you read the above source code, one member ofYyiconStructure helps in this case, which is:

Collapse Copy code
 
M_nidata.ucallbackmessage = my_message;

That means, you have to handleMy_messageWindow message to handle the message by your system tray icon. Now, it's very important to note that to receive the message, it is essential to passNif_messageFlag inM_nidata.uflags.

Collapse Copy code
.....................Begin_message_map(YouClass, You parentClass)On_message(My _ message, onsystemtraymessage)End_message_map()......................

Now to show popup menu or for doing some other task, here is a function that will handle that prerequisite: A menuIdr_menu1In your resource file.

Collapse Copy code
 Void Dlgshutdown :: Onsystembarmessage (Wparam, lparam ){ // Check for window message which come in lparam Parameter Switch ( Lparam ){ Case   Wm_lbuttondown : This - > Showwindow ( Sw_restore ); Bminishow = false; Break ; Case   Wm_rbuttondown :{ //  Now we are showing the menu on right click              Cmenu MNU;//  Load the menu from resource MNU. loadmenu (idr_menu1 ); Cmenu * Popupmenu; //  Get the popup menu form the menu Popupmenu = MNU. getsubmenu ( 0 ); Cpoint PT; //  Get the current cursor position              Getcursorpos (& Pt ); //  Show the popup menu Popupmenu- > Trackpopupmenu (Tpm_rightalign, Pt. X, Pt. y, This ); Break ;}}}

You can handle the popup menu messages in your current dialog class.

How to use it? Using in �at way :-

Here is a graphic which shows how to use it in �at way:

Figure showing 'at' way

 

Here are the steps:

    • Step 1: Choose 'at' way, I. e., alarm.
    • Step 2: Set time.
    • Step 3: Set option for Logoff, restart or shutdown.
    • Step 4: Click on buttonSet timerTo activate the timer.
Using in way

Here is a graphic that showsInWay:

Figure showing 'in' way

Here are the steps:

    • Step 1: Choose 'in' way, I. e., time out option.
    • Step 2: Set time.
    • Step 3: Set option for Logoff, restart or shutdown.
    • Step 4: Click on buttonSet timerTo activate timer.
Update history
    • Version 1.1:: Some bugs fixed. Minimize is now working without problem. Force option added.
    • Version 1.0:: First release.
Special thanks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.