ExitWindowsEx 实现关机、重启
2008-03-04 17:42
651 查看
ExitWindowsEx
The ExitWindowsEx function either logs off the current user, shuts down the system, or shuts down and restarts the system. It sends the WM_QUERYENDSESSION message to all applications to determine if they can be terminated.BOOL ExitWindowsEx(
UINT uFlags,
DWORD dwReason
);
uFlags [in] Shutdown type. This parameter must include one of the following values. 关机类型EWX_LOGOFFEWX_POWEROFFEWX_REBOOTEWX_SHUTDOWN可选的参数EWX_FORCE 强制进程终止 不发送WM_QUERYENDSESSION and WM_ENDSESSION消息EWX_FORCEIFHUNG 发送WM_QUERYENDSESSION and WM_ENDSESSION消息,如果没有响应强制终止进程。dwReason [in] Reason for initiating the shutdown. If this parameter is zero, the SHTDN_REASON_FLAG_PLANNED reason code will not be set, and therefore the default action is an undefined shutdown that is logged as "No title for this reason could be found". By default, it is also an unplanned shutdown. Depending on how the system is configured, an unplanned shutdown triggers the creation of a file that contains the system state information, which can delay shutdown. Therefore, do not use zero for this parameter.
示例:
The following example enables the SE_SHUTDOWN_NAME privilege and then shuts down the system.
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)
return FALSE;
// Shut down the system and force all applications to close.
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
return FALSE;
return TRUE;
}
注意,如果不实现设置权限 会导致调用失败。
相关文章推荐
- DELPHI中调用API函数ExitWindowsEX可以实现系统的关机,注销,和重启
- 在c#中怎么调用ExitWindowsEx函数实现XP的关机、注销、重启操作?
- 在c#中调用ExitWindowsEx函数实现XP的关机、注销
- 关机,注销,重启函数:ExitWindowsEx
- API函数ExitWindowsEx关机
- Window关机函数ExitWindowsEx详解 - dwdm - 博客园
- C#实现控制Windows系统关机、重启和注销的方法:
- C#实现控制Windows系统关机、重启和注销的方法
- 简单实现windows系统普通或强制关机重启注销的功能
- windows C++实现注销、重启、关机 logoff reboot shutdown
- golang 调用win32 api 实现windows注销、重启、关机
- Window关机函数ExitWindowsEx详解
- Window关机函数ExitWindowsEx详解
- 补昨日博客-使用putty登录Windows实现虚拟机中的系统的开机、关机、重启
- 编程实现Windows关机、重启、注销
- Window关机函数ExitWindowsEx详解
- 编程实现Windows关机、重启、注销
- 重启PPC或smartphone调用ExitWindowsEx的问题
- ExitWindowsEX()函数用来退出、重启或注销系统
- Window关机函数ExitWindowsEx详解