InitiateShutdownA function (winreg.h)
Initiates a shutdown and restart of the specified computer, and restarts any applications that have been registered for restart.
Syntax
DWORD InitiateShutdownA(
[in, optional] LPSTR lpMachineName,
[in, optional] LPSTR lpMessage,
[in] DWORD dwGracePeriod,
[in] DWORD dwShutdownFlags,
[in] DWORD dwReason
);
Parameters
[in, optional] lpMachineName
The name of the computer to be shut down. If the value of this parameter is NULL, the local computer is shut down.
[in, optional] lpMessage
The message to be displayed in the interactive shutdown dialog box.
[in] dwGracePeriod
The number of seconds to wait before shutting down the computer. If the value of this parameter is zero, the computer is shut down immediately. This value is limited to MAX_SHUTDOWN_TIMEOUT.
If the value of this parameter is greater than zero, and the dwShutdownFlags parameter specifies the flag SHUTDOWN_GRACE_OVERRIDE, the function fails and returns the error code ERROR_BAD_ARGUMENTS.
[in] dwShutdownFlags
One or more bit flags that specify options for the shutdown. The following values are defined.
Value | Meaning |
---|---|
|
All sessions are forcefully logged off. If this flag is not set and users other than the current user are logged on to the computer specified by the lpMachineName parameter, this function fails with a return value of ERROR_SHUTDOWN_USERS_LOGGED_ON. |
|
Specifies that the originating session is logged off forcefully. If this flag is not set, the originating session is shut down interactively, so a shutdown is not guaranteed even if the function returns successfully. |
|
Overrides the grace period so that the computer is shut down immediately. |
|
Beginning with InitiateShutdown running on Windows 8, you must include the SHUTDOWN_HYBRID flag with one or more of the flags in this table to specify options for the shutdown.
Beginning with Windows 8, InitiateShutdown always initiate a full system shutdown if the SHUTDOWN_HYBRID flag is absent. |
|
The computer installs any updates before starting the shutdown. |
|
The computer is shut down but is not powered down or rebooted. |
|
The computer is shut down and powered down. |
|
The computer is shut down and rebooted. |
|
The system is rebooted using the ExitWindowsEx function with the EWX_RESTARTAPPS flag. This restarts any applications that have been registered for restart using the RegisterApplicationRestart function. |
[in] dwReason
The reason for initiating the shutdown. This parameter must be one of the system shutdown reason codes. If this parameter is zero, the default is an undefined shutdown that is logged as "No title for this reason could be found". By default, it is also an unplanned shutdown. Depending on how the system is configured, an unplanned shutdown triggers the creation of a file that contains the system state information, which can delay shutdown. Therefore, do not use zero for this parameter.
Return value
If the function succeeds, it returns ERROR_SUCCESS.
If the function fails, it returns one of the following error codes.
Return code | Description |
---|---|
|
The caller does not have the required privilege (SE_SHUTDOWN_PRIVILEGE or SE_REMOTE_SHUTDOWN_PRIVILEGE) to perform this operation. |
|
The specified computer does not exist or is not accessible. |
|
The specified computer name is not a valid computer name. |
|
The specified computer does not support a shutdown interface. |
|
An invalid set of parameters was passed. This includes the following combinations.
|
|
A shutdown has already been started on the specified computer. |
|
A shutdown for the specified computer has been scheduled but not started. For this function to succeed, the SHUTDOWN_GRACE_OVERRIDE flag must be set. |
|
One or more users other than the current user are logged on the specified machine, and the SHUTDOWN_FORCE_OTHERS flag was not set. |
Remarks
To shut down the local computer, the calling thread must have the SE_SHUTDOWN_NAME privilege. To shut down a remote computer, the calling thread must have the SE_REMOTE_SHUTDOWN_NAME privilege on the remote computer. By default, users can enable the SE_SHUTDOWN_NAME privilege on the computer they are logged onto, and administrators can enable the SE_REMOTE_SHUTDOWN_NAME privilege on remote computers. For more information, see Running with Special Privileges.
Common reasons for failure include an invalid or inaccessible computer name or insufficient privilege. The error ERROR_SHUTDOWN_IN_PROGRESS is returned if a shutdown is already in progress on the specified computer. The error ERROR_NOT_READY can be returned if fast-user switching is enabled but no user is logged on.
A non-zero return value does not mean the logoff was or will be successful. The shutdown is an asynchronous process, and it can occur long after the API call has returned, or not at all. Even if the timeout value is zero, the shutdown can still be aborted by applications, services, or even the system. The non-zero return value indicates that the validation of the rights and parameters was successful and that the system accepted the shutdown request.
Note
The winreg.h header defines InitiateShutdown as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes.
Requirements
Requirement | Value |
---|---|
Minimum supported client | Windows Vista [desktop apps | UWP apps] |
Minimum supported server | Windows Server 2008 [desktop apps | UWP apps] |
Target Platform | Windows |
Header | winreg.h (include Windows.h) |
Library | Advapi32.lib |
DLL | Advapi32.dll |