Home > Mobile >  How to do the same as clicking Start, then the power icon, then "Shut down" in Win32
How to do the same as clicking Start, then the power icon, then "Shut down" in Win32

Time:05-01

In Windows 10 and 11 (I'm not sure about previous versions) if you click Start, then the power icon, then Shut down Windows shuts down then on the next power on it:

  • does a fast start

  • automatically logs in the last interactive user and locks the session

  • starts any apps in the Start menu Startup folder but does not start any other apps that were running at the time of the shutdown.

I am trying to do this using the Win32 api but I cannot find out how to do it. The ExitWindowsEx() function has an option for fast start but no option to log the user back in at the next boot. The InitiateSystemShutdownEx() function is even more limited.

I note that the command shutdown /sg shuts down then automatically logs in the last user at the next boot, but it does not support fast start (hybrid shutdown) and it restarts all apps that were previously running, not just apps from the Startup folder.

I assume Start, power, Shutdown must be using some other method to shutdown Windows. If anyone knows how to reproduce this please let me know.

CodePudding user response:

Fast startup and hibernation

First of all, note that hibernate and fast startup are different. In short, fast startup is some hibrid state between hibernation and normal shutdown. More about that in this article.

To enter the computer to hibernation use SetSuspendState win api.

Fast startup can be performed by passing the EWX_HYBRID_SHUTDOWN to the ExitWindowsEx.

Application restart

You may register applications for restart using RegisterApplicationRestart and then pass the flag EWX_RESTARTAPPS to ExitWindowsEx as this article suggests.

Automatic logon

There are different ways to logon the current user after a restart. For example using the registry - HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\AutomaticRestartSignOnConfig. For more about it and on other options that may suits your use case better refer to this article.

CodePudding user response:

In case anyone else has run into this problem the solution is the use the (undocumented) EWX_ARSO flag with ExitWindowsEx().

The documentation for ExitWindowsEx() states:

Syntax
BOOL ExitWindowsEx(
  [in] UINT  uFlags,
  [in] DWORD dwReason
);
Parameters
[in] uFlags

The shutdown type. This parameter must include one of the following values.

EWX_HYBRID_SHUTDOWN
0x00400000
Beginning with Windows 8:  You can prepare the system for a faster startup by combining the EWX_HYBRID_SHUTDOWN flag with the EWX_SHUTDOWN flag.
etc

It describes half a dozen flags that determine how Windows is shut down, but it does not mention the EWX_ARSO flag and indeed Googling for it finds no useful information. However the flag is defined in winuser.h and including it gives exactly the same behaviour as clicking Start, power, Shut down in Windows.

  • Related