Wednesday, September 23, 2015

Write An Mfc Api For Workstation Shutdown

In form to shutdown a workstation using an API you compulsion to constitute a shutdown timer in MFC. Benefit the "ExitWindowsEx" API for workstation shutdown. To do this type in the following 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.Privleges{0}.Luid); tkp.PrivilegeCount=1;// one privilege to set tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED; //Get shutdown privilege for this process.



This API using MFC is prepared to shutdown, log off or restart the pc either now or at a programmed period. Proceed from these steps to communicate an MFC API for workstation shutdown.

Instructions

1. AdjustTokenPrivileges(h.Token, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); // Cannot test the return value of AdjustTokenPrivileges. if(GetLastError()!=ERROR_SUCCESS) MessageBox("AdjustTokenPrivileges enable failed.");"


2. Input the following ExitWindowsEx parameter in ; "BOOL ExitWindowsEX( UINT uFlags, DWORD dwReason);"


3. Put in one of these four following declarations if you are working with uFlag: "EWX_LOGOFF: Logoff the current user," "EWX_RESTART: Simply restart the computer," EWX_SHUTDOWN: Shut down but 'AT' style," and "EWX_POWEROFF: Shut down but 'ATX" style."


4. Make the system tray icon and program it to handle the messages by typing this code; NOTIFYICONDATE m_niDate; //handling the ICON ar System Tray Icon m_niData.cbSize=sizeofNOTIFYICONDATA); 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|HIF_TIP; m_niData.uID=ID_ICONDATA; Shell_NotifyIcon(NIM_ADD,&m_niData);


5. Enter this code to deal with messages in the MFC library: void DIgShutDown::OnSystemBarMessage(WPARAM wParam, LPARAN IParam){ Switch(IParam){ case WM_LBUTTONDOWN: this->ShowWIndow(SW_RESTORE);this->ShowWindow(SW_SHOW); bMiniShow=FALSE;break; case WM_RBUTTONDOWN: { CMenu mnu; mnu.LoadMenu(IDR_MENU 1); //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; } } }