using System; using System.Drawing; using System.Runtime.InteropServices; using System.Text; // ReSharper disable FieldCanBeMadeReadOnly.Global // ReSharper disable InconsistentNaming namespace Vanara.PInvoke { public static partial class User32 { /// /// The shutdown type for the method. /// [Flags] public enum ExitWindowsFlags { /// /// Shuts down all processes running in the logon session of the process that called the ExitWindowsEx function. Then it logs the user off. /// This flag can be used only by processes running in an interactive user's logon session. /// EWX_LOGOFF = 0x00000000, /// /// Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped. /// The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section. /// Specifying this flag will not turn off the power even if the system supports the power-off feature. You must specify EWX_POWEROFF to do this. /// Windows XP with SP1: If the system supports the power-off feature, specifying this flag turns off the power. /// EWX_SHUTDOWN = 0x00000001, /// /// Shuts down the system and then restarts the system. /// The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section. /// EWX_REBOOT = 0x00000002, /// /// This flag has no effect if terminal services is enabled. Otherwise, the system does not send the WM_QUERYENDSESSION message. This can cause applications to lose data. Therefore, you should only use this flag in an emergency. /// EWX_FORCE = 0x00000004, /// /// Shuts down the system and turns off the power. The system must support the power-off feature. /// The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section. /// EWX_POWEROFF = 0x00000008, /// /// Forces processes to terminate if they do not respond to the WM_QUERYENDSESSION or WM_ENDSESSION message within the timeout interval. For more information, see the Remarks. /// EWX_FORCEIFHUNG = 0x00000010, /// /// The ewx quickresolve /// EWX_QUICKRESOLVE = 0x00000020, /// /// Shuts down the system and then restarts it, as well as any applications that have been registered for restart using the RegisterApplicationRestart function. These application receive the WM_QUERYENDSESSION message with lParam set to the ENDSESSION_CLOSEAPP value. For more information, see Guidelines for Applications. /// EWX_RESTARTAPPS = 0x00000040, /// /// 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. /// EWX_HYBRID_SHUTDOWN = 0x00400000, /// /// When combined with the EWX_REBOOT flag, will reboot to the boot options. /// EWX_BOOTOPTIONS = 0x01000000, } /// /// The ExitWindowsEx function either logs off the current user, shuts down the system, or shuts down and restarts the system. It sends the WM_QUERYENDSESSION message to all applications to determine if they can be terminated. /// /// Specifies the type of shutdown. /// The reason for initiating the shutdown. /// If the function succeeds, the return value is nonzero.


If the function fails, the return value is zero. To get extended error information, call Marshal.GetLastWin32Error.
[DllImport(Lib.User32, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ExitWindowsEx(ExitWindowsFlags uFlags, SystemShutDownReason dwReason); /// /// Locks the workstation's display, protecting it from unauthorized use. /// /// 0 on failure, non-zero for success [DllImport(Lib.User32, ExactSpelling = true, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool LockWorkStation(); /// /// Indicates that the system cannot be shut down and sets a reason string to be displayed to the user if system shutdown is initiated. /// /// A handle to the main window of the application. /// The reason the application must block system shutdown. This string will be truncated for display purposes after MAX_STR_BLOCKREASON characters. /// If the call succeeds, the return value is nonzero. If the call fails, the return value is zero. To get extended error information, call GetLastError. [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ShutdownBlockReasonCreate(HandleRef hWnd, [MarshalAs(UnmanagedType.LPWStr)] string reason); /// /// Indicates that the system can be shut down and frees the reason string. /// /// A handle to the main window of the application. /// If the call succeeds, the return value is nonzero. If the call fails, the return value is zero. To get extended error information, call GetLastError. [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ShutdownBlockReasonDestroy(HandleRef hWnd); /// Retrieves the reason string set by the function. /// A handle to the main window of the application. /// /// A pointer to a buffer that receives the reason string. If this parameter is NULL, the function retrieves the number of characters in the reason string. /// /// /// A pointer to a variable that specifies the size of the pwszBuff buffer, in characters. If the function succeeds, this variable receives the number of /// characters copied into the buffer, including the null-terminating character. If the buffer is too small, the variable receives the required buffer /// size, in characters, not including the null-terminating character. /// /// /// If the call succeeds, the return value is nonzero. If the call fails, the return value is zero. To get extended error information, call GetLastError. /// [DllImport(Lib.User32, SetLastError = true, ExactSpelling = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ShutdownBlockReasonQuery(HandleRef hWnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pwszBuff, ref uint pcchBuff); /// Retrieves the reason string set by the function. /// A handle to the main window of the application. /// On success, receives the reason string. /// If the call succeeds, the return value is nonzero. If the call fails, the return value is zero. To get extended error information, call GetLastError. public static bool ShutdownBlockReasonQuery(HandleRef hWnd, out string reason) { uint sz = 0; reason = null; if (!ShutdownBlockReasonQuery(hWnd, null, ref sz)) return false; var sb = new StringBuilder((int)sz); if (ShutdownBlockReasonQuery(hWnd, sb, ref sz)) { reason = sb.ToString(); return true; } return false; } } }