马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 康小泡 于 2016-1-29 16:56 编辑 Windows关机程序
使用:
1. 复制可执行文件快捷方式
2.在CmdShell下执行
#include <TCHAR.H>
#include <Windows.h>
#define EWX_FORCEIFHUNG 0x00000010
BOOL EnableShutDownPriv();
BOOL ReSetWindows(DWORD dwFlags, BOOL bForce);
int WINAPI WinMain(HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow) // show state
{
if (_tcscmp(lpCmdLine, ("/r")) == 0)
{
ReSetWindows(EWX_REBOOT, FALSE);
}
else if (_tcscmp(lpCmdLine, ("/s")) == 0)
{
ReSetWindows(EWX_SHUTDOWN, FALSE);
}
else if (_tcscmp(lpCmdLine, ("/l")) == 0)
{
ReSetWindows(EWX_LOGOFF, FALSE);
}
return 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 ReSetWindows(DWORD dwFlags, BOOL bForce)
{
// Check the parameter
if (dwFlags != EWX_LOGOFF && dwFlags != EWX_REBOOT && dwFlags != EWX_SHUTDOWN)
{
return FALSE;
}
// Get the operation system version
OSVERSIONINFO osvi = {0};
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (!GetVersionEx(&osvi))
{
return FALSE;
}
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
EnableShutDownPriv();
}
if (bForce != FALSE)
{
dwFlags = dwFlags | EWX_FORCE;
}
else
{
dwFlags = dwFlags | EWX_FORCEIFHUNG;
}
return ExitWindowsEx(dwFlags, 0);
}
SDKTest.rar
(2.38 KB, 下载次数: 7)
|