This paper introduces the method of simple shutdown and restart of computer with VC + +, and the code is more practical, which has some reference value. The full instance code is as follows:
void Cwebbrowserview::onmenushutdown () {//TODO: Add Command handler code here (AfxMessageBox ("OK to shutdown Do you?
", mb_yesno) = = Idyes) {HANDLE htoken;
Token_privileges TKP;
Get a token to this process. if (! OpenProcessToken (GetCurrentProcess (), Token_adjust_privileges |
Token_query, &htoken)) {AfxMessageBox ("OpenProcessToken error!");
Return
}//Get the LUID for the 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 the shutdown privilege for this process.
AdjustTokenPrivileges (Htoken, FALSE, &TKP, sizeof (token_privileges), (ptoken_privileges) null, NULL);
if (GetLastError ()!= error_success) {AfxMessageBox ("Shutdown failed");
Return
}//Shut down the system and force all applications to close. ExitWindowsEx (Ewx_shutdown | Ewx_force, 0); Reboot only need to change Ewx_shutdown to Ewx_reboot}}