How can I reboot in c++? Is there any provision in WinSDK? What kind of rights should my program(process) have to do so?
There is the ExitWindowsEx Function that can do this. You need to pass the EWX_REBOOT (0x00000002) flag to restart the system.
Important note here (quote from MSDN):
The ExitWindowsEx function 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 InitiateSystemShutdown or InitiateSystemShutdownEx function.
You can choose between the appropriate function depending on your situation.
I presume you have a very good case for wanting to reboot a PC that may be running lots of other applications.
It sounds like you are looking for InitiateShutdown()
, passing SHUTDOWN_RESTART
in dwShutdownFlags
.
Before calling the ExitWindowsEx function you need to enable the SE_SHUTDOWN_NAME privilege:
- OpenProcessToken(GetCurrentProcess (),TOKEN_ADJUST_PRIVILEGES,...)
- LookupPrivilegeValue
- AdjustTokenPrivileges
- CloseHandle