#include#include /* 系统特权提升 @lpName: SE_SHUTDOWN_NAME 关机,重启权限 */BOOL PrivilegePromote(LPCTSTR lpName) { HANDLE hToken = NULL; TOKEN_PRIVILEGES tkp = {0}; // 打开进程令牌 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken)) { return FALSE; } // 查找系统权限的特权值 if (!LookupPrivilegeValue(NULL,lpName,&tkp.Privileges[0].Luid)) { CloseHandle(hToken); return TRUE; } // 调整令牌特权 if(!AdjustTokenPrivileges(hToken,FALSE,&tkp,sizeof(TOKEN_PRIVILEGES),NULL,NULL)) { CloseHandle(hToken); return FALSE; } return TRUE;}/* 系统启动控制:注销,重启,关机 @dwFlags:EWX_LOGOFF | EWX_REBOOT | EWX_SHUTDOWN @bForce:是否强制 */BOOL StartUpContrl(DWORD dwFlags,BOOL bForce) { // 参数校验 if (dwFlags != EWX_LOGOFF && dwFlags != EWX_REBOOT && dwFlags != EWX_SHUTDOWN) { return FALSE; } // 获取系统版本,ExitWindowsEx 在NT平台上需要提升权限 OSVERSIONINFO osvi = {0}; osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); // 版本信息获取 if ( !GetVersionEx(&osvi)) { return FALSE; } // 是NT系统 需要进行权限提升 if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) { PrivilegePromote(SE_SHUTDOWN_NAME); } dwFlags |= (bForce != FALSE)? EWX_FORCE : EWX_FORCEIFHUNG; return ExitWindowsEx(dwFlags, 0);}int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd ) { if (_tcscmp(lpCmdLine,_T("/r")) == 0){ StartUpContrl(EWX_REBOOT,TRUE); } else if (_tcscmp(lpCmdLine,_T("/s")) == 0){ StartUpContrl(EWX_SHUTDOWN,TRUE); } else if (_tcscmp(lpCmdLine,_T("/l")) == 0){ StartUpContrl(EWX_LOGOFF,TRUE); } return 0;}