Come scrivere una API MFC per Workstation Shutdown

November 6

Al fine di spegnimento una workstation utilizzando un'API è necessario effettuare un timer di spegnimento in MFC. Questa API utilizzando MFC è fatto per spegnere, disconnettere o riavviare il computer immediatamente o in un momento programmato. Seguire questa procedura per scrivere una API MFC per lo spegnimento della workstation.

istruzione

1 Utilizzare l'API "ExitWindowsEx" per lo spegnimento workstation. Per fare questo tipo nel seguente codice: "manico hToken; // gestisce per elaborare gettone PRIVILEGI TOKEN TKP; // puntatore a gettone struttura OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, e hToken); // Prendi il LUID per privilegio arresto . LookupPrivilegeValue (NULL, SE_SHUTDOWN_NAME, e tkp.Privleges {0} .Luid); tkp.PrivilegeCount = 1; // un privilegio per impostare tkp.Privileges [0] = .Attributes SE_PRIVILEGE_ENABLED; //. Ottenere privilegio di arresto per questo processo AdjustTokenPrivileges (h.Token, FALSE, e TKP, 0, (PTOKEN_PRIVILEGES) NULL, 0); // non è possibile verificare il valore di ritorno di AdjustTokenPrivileges se (GetLastError () = ERROR_SUCCESS!) MessageBox ( "AdjustTokenPrivileges consentono fallito."); ".

2 Inserire il seguente parametro ExitWindowsEx in <Windows.h>; "BOOL ExitWindowsEx (UINT uFlags, DWORD dwReason);"

3 Mettere in una di queste quattro seguenti dichiarazioni se si lavora con uFlag: "EWX_LOGOFF: disconnessione l'utente corrente", "EWX_RESTART: è sufficiente riavviare il computer," EWX_SHUTDOWN: Spegnere ma 'AT' stile "e" EWX_POWEROFF: Spegnere ma ' "stile". ATX

4 Rendere l'icona nella barra di sistema e programmarlo per gestire i messaggi digitando questo codice; NOTIFYICONDATE m_niDate; // Gestione della ICON AR System Tray Icon m_niData.cbSize = sizeofNOTIFYICONDATA); m_niData.hIcon = m_Icon; m_niData.hWnd = this-> m_hWnd; sprintf (m_niData.szTip, "Arresta il sistema di allarme: 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, e m_niData);

5 Inserisci il codice per gestire i messaggi nella libreria MFC: void DIgShutDown :: OnSystemBarMessage (WPARAM wParam, LPARAN IParam) {switch (IParam) {case WM_LBUTTONDOWN: this-> ShowWindow (SW_RESTORE); this-> ShowWindow (SW_SHOW); bMiniShow = FALSE; break; caso 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, questo); // This-> ShowWindow (SW_MINIMIZE); rompere; }}}