windows重启系统
2014-11-08 22:55
337 查看
下面代码演示使用InitiateSystemShutdown函数来重启本地系统。系统会显示出一个对话框,显示特定信息,通知用户在指定时间内关闭所有程序。超过指定的时间,系统会重启。
在调用InitiateSystemShutdown之前,需要获取SE_SHUTDOWN_NAME权限。
如果在 InitiateSystemShutdown指定的时间内调用了AbortSystemShutdown,系统不会重启。例如,如果在MySystemShutdown之后调用了PreventSystemShutdown,系统会闭关对话框,且不会重启系统。
====
还可以用ExitWindowsEx来重启或关闭系统。ExitWindowsEx在初始化 关闭进程 后就返回, 关闭进程 是异步进行。这个函数用来结束其调用用户的登陆会话,所以如果你不是当前的登陆用户的话,调用这个函数会返回成功但并不会实际地重启或关闭系统。这时就需要用InitiateSystemShutdown或InitiateSystemShutdownEx。
在调用InitiateSystemShutdown之前,需要获取SE_SHUTDOWN_NAME权限。
#include <windows.h> #pragma comment( lib, "advapi32.lib" ) BOOL MySystemShutdown( LPTSTR lpMsg ) { HANDLE hToken; // handle to process token TOKEN_PRIVILEGES tkp; // pointer to token structure BOOL fResult; // system shutdown flag // Get the current process token handle so we can get shutdown // privilege. if (!OpenProcessToken(GetCurrentProcess(), TOK b16d EN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return FALSE; // Get the LUID for 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 shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); // Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) return FALSE; // Display the shutdown dialog box and start the countdown. fResult = InitiateSystemShutdown( NULL, // shut down local computer lpMsg, // message for user 30, // time-out period, in seconds FALSE, // ask user to close apps TRUE); // reboot after shutdown if (!fResult) return FALSE; // Disable shutdown privilege. tkp.Privileges[0].Attributes = 0; AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); return TRUE; }
如果在 InitiateSystemShutdown指定的时间内调用了AbortSystemShutdown,系统不会重启。例如,如果在MySystemShutdown之后调用了PreventSystemShutdown,系统会闭关对话框,且不会重启系统。
BOOL PreventSystemShutdown() { HANDLE hToken; // handle to process token TOKEN_PRIVILEGES tkp; // pointer to token structure // Get the current process token handle so we can get shutdown // privilege. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return FALSE; // Get the LUID for 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 shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); if (GetLastError() != ERROR_SUCCESS) return FALSE; // Prevent the system from shutting down. if ( !AbortSystemShutdown(NULL) ) return FALSE; // Disable shutdown privilege. tkp.Privileges[0].Attributes = 0; AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); return TRUE; }
int main() { MySystemShutdown("shutdown?"); //Sleep(5000); //PreventSystemShutdown(); return 0; }
====
还可以用ExitWindowsEx来重启或关闭系统。ExitWindowsEx在初始化 关闭进程 后就返回, 关闭进程 是异步进行。这个函数用来结束其调用用户的登陆会话,所以如果你不是当前的登陆用户的话,调用这个函数会返回成功但并不会实际地重启或关闭系统。这时就需要用InitiateSystemShutdown或InitiateSystemShutdownEx。
相关文章推荐
- 设置windows系统自动重启
- windows 2003 server 升级内存后,不能进系统,自动重启
- Windows系统重启后自动登录运行指定的windows程序
- Windows系统下写bat文件重启Jetty
- SERVICE_FAILURE_ACTIONS可能导致windows系统不断重启
- 在Closing事件中,将e.Cancle设置成true,则Windows无法关机和重启系统的解决办法
- Windows硬盘映射网络磁盘目录,设置重启系统或开机后自动登录,保存用户名和密码
- ExitWindowsEX()函数用来退出、重启或注销系统
- C#实现控制Windows系统关机、重启和注销的方法
- C#创建Windows服务(Windows Services) 实战之系统定时重启服务
- 电脑不能进入xp系统,还未进入到windows进度条便自动重启,误删系统文件
- (转载整理)程序安装流程及如何实现文件在系统后自动重启(windows)
- C#创建Windows服务(Windows Services) 实战之系统定时重启服务
- Windows系统设置环境变量后无需重启系统即可让变量生效的解决办法
- 远程关闭和重启 Windows 系统
- DELPHI中调用API函数ExitWindowsEX可以实现系统的关机,注销,和重启
- Windows系统不重启而使环境变量生效的方法
- 强制windows系统重启at命令
- Windows系统不重启而使环境变量生效的方法
- Windows系统发送打印命令后,系统自动重启或出现蓝屏的解决办法