windows 如何以编程方式重新启动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3697984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Reboot Programmatically?
提问by lalli
How can I reboot in c++? Is there any provision in WinSDK? What kind of rights should my program(process) have to do so?
如何在 C++ 中重新启动?WinSDK 中是否有任何规定?我的程序(进程)应该拥有什么样的权利?
回答by Den Delimarsky
There is the ExitWindowsEx Functionthat can do this. You need to pass the EWX_REBOOT(0x00000002) flag to restart the system.
有可以做到这一点的ExitWindowsEx 函数。您需要通过EWX_REBOOT(0x00000002) 标志来重新启动系统。
Important note here (quote from MSDN):
这里的重要说明(引用自MSDN):
The ExitWindowsExfunction returns as soon as it has initiated the shutdown process. The shutdown or logoff then proceeds asynchronously. The function is designed to stop all processes in the caller's logon session. Therefore, if you are not the interactive user, the function can succeed without actually shutting down the computer. If you are not the interactive user, use the InitiateSystemShutdownor InitiateSystemShutdownExfunction.
该ExitWindowsEx只要它已启动关闭过程函数返回。然后关闭或注销异步进行。该函数旨在停止调用者登录会话中的所有进程。因此,如果您不是交互式用户,则该功能无需实际关闭计算机即可成功。如果您不是交互式用户,请使用InitiateSystemShutdown或InitiateSystemShutdownEx函数。
You can choose between the appropriate function depending on your situation.
您可以根据自己的情况选择合适的功能。
回答by Anders
Before calling the ExitWindowsExfunction you need to enablethe SE_SHUTDOWN_NAME privilege:
调用之前ExitWindowsEx功能,您需要启用的SE_SHUTDOWN_NAME特权:
- OpenProcessToken(GetCurrentProcess (),TOKEN_ADJUST_PRIVILEGES,...)
- LookupPrivilegeValue
- AdjustTokenPrivileges
- CloseHandle
- OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,...)
- 查找权限值
- 调整令牌权限
- 关闭句柄
回答by Johnsyweb
I presume you have a very good case for wanting to reboot a PC that may be running lots of other applications.
我想您有一个很好的案例想要重新启动可能运行许多其他应用程序的 PC。
It sounds like you are looking for InitiateShutdown()
, passing SHUTDOWN_RESTART
in dwShutdownFlags
.
这听起来像你正在寻找InitiateShutdown()
,通过SHUTDOWN_RESTART
在dwShutdownFlags
。