To program windows to shut down, restart, or log off, you can use the Exwindowsex API function, which has only two parameters, the first indicating the shutdown action flag, that is, whether you want to let the function shut down, or restart, or logout. You can use flag constants such as Ewx_shutdown, Ewx_reboot, Ewx_logoff, andso on to indicate shutdown, restart, and logoff, respectively. In addition, if you add ewx_force to this flag constant, it indicates that the operation is enforced. When you do this, Windows will first send a wm_queryendsession message to each running program, telling them that I'm quitting the system now, and you're going to save something! If one of the programs responds "No" to the message at this point, the system will no longer perform the above operation. If the ewx_force flag is specified, the system does not send a message to ask the individual programs, but instead directly forces all programs to shut down and exit the system. So be careful when you specify the ewx_force flag, because doing so may lose something. (for example, files may not be saved). The second parameter is a reserved parameter and can pass a value of 0 directly. In addition, when a shutdown and restart operation is performed on an operating system above Win2K, the process that calls the function first obtains the shutdown privilege, or the function fails to invoke it.
#include <windows.h>
Enable shutdown privilege function BOOL Enableshutdownprivilege () {HANDLE hprocess = NULL; HANDLE htoken = NULL; LUID UID = {0}; Token_privileges sttoken_privileges = {0};
hprocess =:: GetCurrentProcess (); Gets the current application process handle if (!::openprocesstoken (Hprocess,token_adjust_privileges,&htoken))//Opens the current process's access token handle (openprocess The token function call failed with a return value of 0) return FALSE;
if (!::lookupprivilegevalue (NULL,SE_SHUTDOWN_NAME,&UID))//Get permission name for "SeShutdownPrivilege" of Luid ( Lookupprivilegevalue function call failed return value is 0) return FALSE;
Sttoken_privileges.privilegecount = 1; Number of permissions to adjust Sttoken_privileges.privileges[0]. Luid = UID; Permissions of Luid Sttoken_privileges.privileges[0]. Attributes = se_privilege_enabled; The property of the permission, se_privilege_enabled to enable the permission if (!::adjusttokenprivileges (htoken,false,&sttoken_privileges,sizeof StTok En_privileges,null,null)//Adjust the specified permission in the access token (the AdjustTokenPrivileges function call fails with a return value of 0) return FALSE;
if (:: GetLastError ()! = ERROR_SUCCESS)//Check if the permission is adjusted successfully return FALSE;
:: CloseHandle (Htoken); return TRUE; }
shutdown function bool Shutdown (bool BForce) {Enableshutdownprivilege (); Enable shutdown privilege function if (BForce) return:: ExitWindowsEx (Ewx_shutdown | ewx_force,0); Force shutdown else return:: ExitWindowsEx (ewx_shutdown,0); }
Logoff function bool Logoff (bool BForce) {if (BForce) return:: ExitWindowsEx (Ewx_logoff | ewx_force,0); Force logoff else return:: ExitWindowsEx (ewx_logoff,0); }
Restart function bool Reboot (bool BForce) {Enableshutdownprivilege (); Enable shutdown privilege function if (BForce) return:: ExitWindowsEx (Ewx_reboot | ewx_force,0); Force restart else return:: ExitWindowsEx (ewx_reboot,0); }
int main () {Logoff (FALSE); Unregister Reboot (FALSE); Restart Shutdown (FALSE); Shutdown Logoff (TRUE); Force logoff Reboot (TRUE); Force restart Shutdown (TRUE); Force shutdown return 0; }
Also refer to: http://www.sharejs.com/codes/cpp/6256
Http://wenku.baidu.com/view/b668afd028ea81c758f578f4.html
Programming for Windows shutdown, restart, logoff