C++ 让电脑关机,重启,注销
原创
©著作权归作者所有:来自51CTO博客作者流楚丶格念的原创作品,请联系作者获取转载授权,否则将追究法律责任
不展示了,一运行关机了就
代码
#include <windows.h>
#include <iostream>
#include <string.h>
using namespace std;
BOOL ReSetWindows(DWORD dwFlags, BOOL bForce)
{
if (dwFlags != EWX_LOGOFF && dwFlags != EWX_REBOOT && dwFlags != EWX_SHUTDOWN)
return FALSE;
OSVERSIONINFO osvi = { 0 };
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
}
dwFlags |= (bForce != FALSE) ? EWX_FORCE : EWX_FORCEIFHUNG;
return ExitWindowsEx(dwFlags, 0);
}
BOOL EnableShutDownPriv()
{
HANDLE hToken = NULL;
TOKEN_PRIVILEGES tkp = { 0 };
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
return FALSE;
}
if (!LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid))
{
CloseHandle(hToken);
return FALSE;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
{
CloseHandle(hToken);
return FALSE;
}
return TRUE;
}
BOOL Judge(int key)
{
EnableShutDownPriv();
switch (key)
{
case 1001://关机
ReSetWindows(EWX_SHUTDOWN, true);
break;
case 1002://重启
ReSetWindows(EWX_REBOOT, true);
break;
case 1003://注销
ReSetWindows(EWX_LOGOFF, false);
break;
}
return 0;
}
int main(int argc, char *argv[])
{
char *tmp = argv[0];
int key = atoi(tmp);
Judge(key);
}