Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Reboot Programmatically?

How can I reboot in c++? Is there any provision in WinSDK? What kind of rights should my program(process) have to do so?

like image 928
lalli Avatar asked Sep 13 '10 05:09

lalli


People also ask

How to restart Android device programmatically?

getRuntime(). exec(new String[] { "su", "-c", "reboot" }); proc = Runtime. getRuntime(). exec(new String[]{"/system/bin/su","-c","reboot now"}); proc.

How do you restart your Android phone?

Use Your Phone's Power Button Almost all brands and models of Android smartphones have a power button. Press and hold the Power button for 5-10 seconds to reveal the power menu. Afterward, select Restart to shut down the device and power it back on.

What is reboot in CP?

Rebooting is the same as restarting, and close enough to powering off and then turning off your device. The purpose is to close and reopen the operating system. Resetting, on the other hand, means taking the device back to the state in which it left the factory. Resetting wipes all your personal data.


2 Answers

Before calling the ExitWindowsEx function you need to enable the SE_SHUTDOWN_NAME privilege:

  1. OpenProcessToken(GetCurrentProcess (),TOKEN_ADJUST_PRIVILEGES,...)
  2. LookupPrivilegeValue
  3. AdjustTokenPrivileges
  4. CloseHandle
like image 69
Anders Avatar answered Oct 06 '22 23:10

Anders


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.

like image 20
Den Delimarsky Avatar answered Oct 07 '22 00:10

Den Delimarsky