#include <tchar.h>
#include <Windows.h>
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)) { //其他权限 只需要修改SE_SHUTDWON_NAME即可
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 ReSetWindows(DWORD dwFlags, BOOL bForce)
{
//Check the param;
if (dwFlags != EWX_LOGOFF && dwFlags != EWX_REBOOT && dwFlags != EWX_SHUTDOWN) {
return FALSE;
}
//Get the os version;
OSVERSIONINFO osvi = { 0 };
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (!GetVersionEx(&osvi)) {
return FALSE;
}
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) {
EnableShutDownPriv();
}
dwFlags |= (bForce != FALSE) ? EWX_FORCE : EWX_FORCEIFHUNG;
return ExitWindowsEx(dwFlags, 0);
}
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
if (_tcscmp(lpCmdLine, _T("/r")) == 0) {
ReSetWindows(EWX_REBOOT, FALSE);
}
else if (_tcscmp(lpCmdLine, _T("/s")) == 0) {
ReSetWindows(EWX_SHUTDOWN, FALSE);
}
else if (_tcscmp(lpCmdLine, _T("/l")) == 0) {
ReSetWindows(EWX_LOGOFF, FALSE);
}
return 0;
#include <Windows.h>
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)) { //其他权限 只需要修改SE_SHUTDWON_NAME即可
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 ReSetWindows(DWORD dwFlags, BOOL bForce)
{
//Check the param;
if (dwFlags != EWX_LOGOFF && dwFlags != EWX_REBOOT && dwFlags != EWX_SHUTDOWN) {
return FALSE;
}
//Get the os version;
OSVERSIONINFO osvi = { 0 };
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (!GetVersionEx(&osvi)) {
return FALSE;
}
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) {
EnableShutDownPriv();
}
dwFlags |= (bForce != FALSE) ? EWX_FORCE : EWX_FORCEIFHUNG;
return ExitWindowsEx(dwFlags, 0);
}
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
if (_tcscmp(lpCmdLine, _T("/r")) == 0) {
ReSetWindows(EWX_REBOOT, FALSE);
}
else if (_tcscmp(lpCmdLine, _T("/s")) == 0) {
ReSetWindows(EWX_SHUTDOWN, FALSE);
}
else if (_tcscmp(lpCmdLine, _T("/l")) == 0) {
ReSetWindows(EWX_LOGOFF, FALSE);
}
return 0;
}
附录:
如果开发的应用程序在别的电脑上提示:没有正确配置,简便方法一:
(1) 项目右键 -》 清单工具 -》 输入和输出 -》嵌入清单=》否
(2)release重新编译
如果提示:不是有效的win32程序:
项目右键-》配置属性-》常规-》平台工具集-》Visual Studio 2013 - Windows XP (v120_xp)