This Visual C++ sample project using MFC is intended to either shutdown, logoff, or
restart the computer at a given time or immediately. This is a Visual C++
Project using the concept of Win32 APIs with MFC. The Shutdown Timer application
also uses System Tray Interface to handle it with menu options.
API for ShutDown:
The ExitWindowEx is the API which can be used in Visual
C++ for shutting down a workstation. There is one more API for this purpose
named InitiateSystemShutdown, but that API works only on
Window 2000 and above. The ExitWindowEx API works directly
in Win9x/ME, but in Windows multi-user systems, it requires a special privilege
before execution.
Here is the code for enabling that privilege for Windows multi-user
platforms in Visual C++:
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES |
TOKEN_QUERY, &hToken);
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
MessageBox("AdjustTokenPrivileges enable failed.");
Now, ExitWindowsEx has two parameters. The function
declaration in <Windows.h> looks like:
BOOL ExitWindowsEx(
UINT uFlags,
DWORD dwReason
);
And for uFlag, we have four 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.
I think the above Win32 APIs are very simple, right?
System Tray Icon :
The next logical step to be understood is how to use the system tray icon and
the way it handles the messages. First, I will show you the code, after that, I
will describe to you the required details about the System Tray Icon handling.
NOTIFYICONDATA m_niData;
m_niData.cbSize=sizeof(NOTIFYICONDATA);
m_niData.hIcon=m_Icon;
m_niData.hWnd=this->m_hWnd;
sprintf(m_niData.szTip,"Shut Down Alarm :My Father Software inc @ 2003");
m_niData.uCallbackMessage=WM_USER+75;
m_niData.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;
m_niData.uID=ID_ICONDATA;
Shell_NotifyIcon(NIM_ADD,&m_niData);
Now, here you see one new variable of type NOTIFYICONDATA Structure.
BOOL Shell_NotifyIcon(
DWORD dwMessage,
PNOTIFYICONDATA lpdata);
The NOTIFYICONDATA structure according to MSDN 'Contains
information that the system needs to process taskbar status area messages'.
The variables are:
cbSize: this variable is created to provide compatibility
with other versions of NotifyIconData.
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 notifications to the window identified
in hWnd. These notifications 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
variable to use or not.
uID: any unique ID for Icon.
Now, Shell_NotifyIcon adds this icon to system tray. It takes
three parameters as arguments:
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).
Handling the system tray message:
Message Mapping: This notification is handled using
MFC
user
defined messages .
ON_MESSAGE(WM_USER+75,OnSystemBarMessage)
Now, the code to handle message in MFC library (
Visual C++ ) is :
void DlgShutDown::OnSystemBarMessage(WPARAM wParam, LPARAM lParam)
{
Switch(lParam){
case WM_LBUTTONDOWN:
this->ShowWindow(SW_RESTORE);
this- >ShowWindow(SW_SHOW);
bMiniShow=FALSE; break;
case WM_RBUTTONDOWN:
{
CMenu mnu;
mnu.LoadMenu(IDR_MENU1);
PopUpMenu=mnu.GetSubMenu(0);
SetForegroundWindow();
CPoint pt;
GetCursorPos(&pt);
PopUpMenu->TrackPopupMenu(TPM_RIGHTALIGN,pt.x,pt.y,this);
break;
}
}
}
This code demonstrates "Mouse Right Button Click" by which
I show
the popup menu when user clicks on system tray icon.
I think I have explained much about this software, but if
has any problems, please forgive me. This is my first article in CoderSource.net.
You can message me, I will try my best to help you and sort out my mistakes.
Download the Sample
Project here.