Feb 27, 2021, 11:57 AM

News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!


Shutdown, Logoff API in Visual C++, MFC and Win32

Started by thiruvasagamani, Sep 01, 2008, 09:06 PM

previous topic - next topic
Go Down

thiruvasagamani

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; // handle to process token
    TOKEN_PRIVILEGES tkp; // pointer to token structure
Code: [Select]

    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
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;   
    // Get shutdown privilege for this process.
    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
    // Cannot test the return value of AdjustTokenPrivileges. 
    if (GetLastError() != ERROR_SUCCESS)   
       MessageBox("AdjustTokenPrivileges enable failed.");


Now, ExitWindowsEx has two parameters. The function declaration in <Windows.h> looks like:

Code: [Select]
BOOL ExitWindowsEx(
  UINT uFlags,
  DWORD dwReason
);


And for uFlag, we have four options:

   1. EWX_LOGOFF: Logoff the current user.
   2. EWX_RESTART: Simply restart the computer.
   3. EWX_SHUTDOWN: Shut down but 'AT' style.
   4. 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;
    //handling the ICON at System Tray Icon
Code: [Select]
    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.

Code: [Select]
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:

   1. cbSize: this variable is created to provide compatibility with other versions of NotifyIconData.
   2. hIcon: icon for the system tray.
   3. hWnd: handle to window which will handle the system tray icon message.
   4. szTip: show tip when mouse hovers over icon.
   5. 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.
   6. uFlag: this flag notifies the system which of the above variable to use or not.
   7. uID: any unique ID for Icon.

Now, Shell_NotifyIcon adds this icon to system tray. It takes three parameters as arguments:

   1. NIM_ADD: add icon to system tray.
   2. NIM_DELETE: delete icon from system tray.
   3. 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 :

   
Code: [Select]
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);
                //CMenu *PopUpMenu;
                PopUpMenu=mnu.GetSubMenu(0);
                SetForegroundWindow();
                CPoint pt;
                GetCursorPos(&pt);
                PopUpMenu->TrackPopupMenu(TPM_RIGHTALIGN,pt.x,pt.y,this);
                //this->ShowWindow(SW_MINIMIZE);
                break;
            }
        }
    }


This code demonstrates "Mouse Right Button Click" by which I show the popup menu when user clicks on system tray icon.
Thiruvasakamani Karnan


nandagopal



Login api is not working for

i tried lot mani can you help me ????




Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:
Verification:
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens Web Designing Chennai | GinGly :: Build your Personal Website | CineBuzz :: Cinema News | My Kids Diary :: Gift your Kids Memories :: Book Website @ 349 Rs monthly
Copyright 2005 - 2021 :: IT Acumens :: All Rights Reserved. :: XML Sitemap
ITAcumens Discussion Forum with 2 lakhs post running for 15 years - Powered by IT Acumens Pro Dedicated Server

My Kids Diary