使用WinAPI中止关机操作?

时间:2014-02-14 13:49:28

标签: .net vb.net windows-8 shutdown reboot

如何中止由ExitWindowsEx功能启动的关机/重启操作?

我已经阅读This所以问题,但我不清楚如何做到这一点,我需要一个完整的书面例子来学习和理解。

我正在写一个帮助程序类来关闭系统,我想编写一个方法来取消启动的关闭/重启或注销。

我在Windows 8.1上

关于MSDN的回复信息说:

''' <returns>
''' If the function succeeds, the return value is 'True'. 
''' The function executes asynchronously so a 'True' return value indicates that the shutdown has been initiated. 
''' It does not indicate whether the shutdown will succeed. 
''' It is possible that the system, the user, or another application will abort the shutdown.
''' If the function fails, the return value is 'False'. 
''' </returns>

我使用自定义方法ExitWindows来调用ExitWindowsEx函数,因为我想传递更多参数。

<DllImport("user32.dll", SetLastError:=True)>
Private Function ExitWindowsEx(
        ByVal uFlags As ShutdownType,
        ByVal dwReason As ShutdownReason
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

Public Function ExitWindows(ByVal ShutdownType As ShutdownType,
                            ByVal ForceType As ForceType,
                            ByVal ShutdownReason As ShutdownReason,
                            ByVal PlanningType As PlanningType) As Boolean

    Return ExitWindowsEx(ShutdownType Or ForceType, ShutdownReason Or PlanningType)

End Function

这就是我重启的方式:

Sub Test()

    ' Reboot system. 
    ' Force applications to close if they hung.
    ' Specify 'Installation' reason.
    ' Specify that it's a planned shutdown.
    ExitWindows(ShutdownType.Reboot,
                ForceType.ForceIfHung,
                ShutdownReason.MajorOperatingSystem Or ShutdownReason.MinorInstallation,
                PlanningType.Planned)

End Sub

然后我怎么能中止呢?

  

更新

这是我的完整帮助类,我需要实现'Abort'方法,但我不知道从哪里开始尝试。

#Region " Imports "

Imports System.ComponentModel
Imports System.Runtime.InteropServices

#End Region

''' <summary>
''' Logs off the interactive user, shuts down the system, or restarts the system.
''' </summary>
Public Class Shutdown

#Region " P/Invoke "

#Region " Methods "

    Friend Class NativeMethods

        ''' <summary>
        ''' Logs off the interactive 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.
        ''' </summary>
        ''' <param name="uFlags">
        ''' Indicates the shutdown type.
        ''' </param>
        ''' <param name="dwReason">
        ''' Indicates the reason for initiating the shutdown.
        ''' </param>
        ''' <returns>
        ''' If the function succeeds, the return value is 'True'. 
        ''' The function executes asynchronously so a 'True' return value indicates that the shutdown has been initiated. 
        ''' It does not indicate whether the shutdown will succeed. 
        ''' It is possible that the system, the user, or another application will abort the shutdown.
        ''' If the function fails, the return value is 'False'. 
        ''' </returns>
        <DllImport("user32.dll", SetLastError:=True)>
        Friend Shared Function ExitWindowsEx(
            ByVal uFlags As ShutdownType,
            ByVal dwReason As ShutdownReason
        ) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function

    End Class

#End Region

#Region " Enumerations "

    ''' <summary>
    ''' Indicates the shutdown type.
    ''' </summary>
    <Description("Enum used in the 'uFlags' parameter of 'ExitWindowsEx' Function.")>
    <Flags()>
    Enum ShutdownType As UInteger

        ''' <summary>
        ''' 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.
        ''' </summary>
        LogOff = &H0

        ''' <summary>
        ''' 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. 
        ''' Specifying this flag will not turn off the power even if the system supports the power-off feature, 
        ''' You must specify 'PowerOff' to do this.
        ''' </summary>
        ShutDown = &H1

        ''' <summary>
        ''' Shuts down the system and then restarts it.
        ''' The calling process must have the 'SE_SHUTDOWN_NAME' privilege. 
        ''' </summary>
        Reboot = &H2

        ''' <summary>
        ''' 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.
        ''' </summary>
        PowerOff = &H8

        ''' <summary>
        ''' Shuts down the system and then restarts it,
        ''' as well as any applications that have been registered for restart using the
        ''' 'RegisterApplicationRestart' function.
        ''' </summary>
        RestartApps = &H40

    End Enum

    ''' <summary>
    ''' Indicates the forcing type.
    ''' </summary>
    <Description("Enum used in combination with the 'uFlags' parameter of 'ExitWindowsEx' Function.")>
    Enum ForceType As UInteger

        ''' <summary>
        ''' Don't force the system to close the applications.
        ''' This is the default parameter.
        ''' </summary>
        Wait = &H0

        ''' <summary>
        ''' This flag has no effect if terminal services is enabled. 
        ''' Otherwise, the system does not send the 'WM_QUERYENDSESSIO'N message. 
        ''' This can cause applications to lose data. 
        ''' Therefore, you should only use this flag in an emergency.
        ''' </summary>
        Force = &H4

        ''' <summary>
        ''' Forces processes to terminate if they do not respond to the 'WM_QUERYENDSESSION',
        ''' or 'WM_ENDSESSION' message within the timeout interval.
        ''' </summary>
        ForceIfHung = &H10

    End Enum

    ''' <summary>
    ''' Indicates the shutdown reason codes. 
    ''' You can specify any minor reason in combination with any major reason, but some combinations do not make sense.
    ''' </summary>
    <Description("Enum used in the 'dwReason' parameter of 'ExitWindowsEx' Function.")>
    <Flags()>
    Enum ShutdownReason As UInteger

        ''' <summary>
        ''' Application issue.
        ''' </summary>
        MajorApplication = &H40000

        ''' <summary>
        ''' Hardware issue.
        ''' </summary>
        MajorHardware = &H10000

        ''' <summary>
        ''' The 'InitiateSystemShutdown' function was used instead of 'InitiateSystemShutdownEx'.
        ''' </summary>
        MajorLegacyApi = &H70000

        ''' <summary>
        ''' Operating system issue.
        ''' </summary>
        MajorOperatingSystem = &H20000

        ''' <summary>
        ''' Other issue.
        ''' </summary>
        MajorOther = &H0

        ''' <summary>
        ''' Power failure.
        ''' </summary>
        MajorPower = &H60000

        ''' <summary>
        ''' Software issue.
        ''' </summary>
        MajorSoftware = &H30000

        ''' <summary>
        ''' System failure..
        ''' </summary>
        MajorSystem = &H50000

        ''' <summary>
        ''' Blue screen crash event.
        ''' </summary>
        MinorBlueScreen = &HF

        ''' <summary>
        ''' Unplugged.
        ''' </summary>
        MinorCordUnplugged = &HB

        ''' <summary>
        ''' Disk.
        ''' </summary>
        MinorDisk = &H7

        ''' <summary>
        ''' Environment.
        ''' </summary>
        MinorEnvironment = &HC

        ''' <summary>
        ''' Driver.
        ''' </summary>
        MinorHardwareDriver = &HD

        ''' <summary>
        ''' Hot fix.
        ''' </summary>
        MinorHotfix = &H11

        ''' <summary>
        ''' Hot fix uninstallation.
        ''' </summary>
        MinorHotfixUninstall = &H17

        ''' <summary>
        ''' Unresponsive.
        ''' </summary>
        MinorHung = &H5

        ''' <summary>
        ''' Installation.
        ''' </summary>
        MinorInstallation = &H2

        ''' <summary>
        ''' Maintenance.
        ''' </summary>
        MinorMaintenance = &H1

        ''' <summary>
        ''' MMC issue.
        ''' </summary>
        MinorMMC = &H19

        ''' <summary>
        ''' Network connectivity.
        ''' </summary>
        MinorNetworkConnectivity = &H14

        ''' <summary>
        ''' Network card.
        ''' </summary>
        MinorNetworkCard = &H9

        ''' <summary>
        ''' Other issue.
        ''' </summary>
        MinorOther = &H0

        ''' <summary>
        ''' Other driver event.
        ''' </summary>
        MinorOtherDriver = &HE

        ''' <summary>
        ''' Power supply.
        ''' </summary>
        MinorPowerSupply = &HA

        ''' <summary>
        ''' Processor.
        ''' </summary>
        MinorProcessor = &H8

        ''' <summary>
        ''' Reconfigure.
        ''' </summary>
        MinorReconfig = &H4

        ''' <summary>
        ''' Security issue.
        ''' </summary>
        MinorSecurity = &H13

        ''' <summary>
        ''' Security patch.
        ''' </summary>
        MinorSecurityFix = &H12

        ''' <summary>
        ''' Security patch uninstallation.
        ''' </summary>
        MinorSecurityFixUninstall = &H18

        ''' <summary>
        ''' Service pack.
        ''' </summary>
        MinorServicePack = &H10

        ''' <summary>
        ''' Service pack uninstallation.
        ''' </summary>
        MinorServicePackUninstall = &H16

        ''' <summary>
        ''' Terminal Services.
        ''' </summary>
        MinorTermSrv = &H20

        ''' <summary>
        ''' Unstable.
        ''' </summary>
        MinorUnstable = &H6

        ''' <summary>
        ''' Upgrade.
        ''' </summary>
        MinorUpgrade = &H3

        ''' <summary>
        ''' WMI issue.
        ''' </summary>
        MinorWMI = &H15

    End Enum

    ''' <summary>
    ''' Indicates the planning type.
    ''' </summary>
    <Description("Enum used in combination with the 'dwReason' parameter of 'ExitWindowsEx' Function.")>
    Enum PlanningType As UInteger

        ''' <summary>
        ''' The shutdown was unplanned.
        ''' This is the default parameter.
        ''' </summary>
        Unplanned = &H0

        ''' <summary>
        ''' The reason code is defined by the user. 
        ''' For more information, see Defining a Custom Reason Code.
        ''' If this flag is not present, the reason code is defined by the system.
        ''' </summary>
        UserDefined = &H40000000

        ''' <summary>
        ''' The shutdown was planned. 
        ''' The system generates a System State Data (SSD) file. 
        ''' This file contains system state information such as the processes, threads, memory usage, and configuration.
        ''' If this flag is not present, the shutdown was unplanned.
        ''' </summary>
        Planned = &H80000000&

    End Enum

#End Region

#End Region

#Region " Public Methods "

    ''' <summary>
    ''' Shuts down all processes running in the logon session and then logs off the interactive user. 
    ''' </summary>
    ''' <param name="Force">
    ''' Indicates whether it's a forced shutdown.
    ''' </param>
    ''' <param name="Reason">
    ''' Indicates the reason for initiating the shutdown.
    ''' </param>
    ''' <param name="Planning">
    ''' Indicates the shutdown planning.
    ''' </param>
    ''' <returns>
    ''' If the function succeeds, the return value is 'True'. 
    ''' The function executes asynchronously so a 'True' return value indicates that the shutdown has been initiated. 
    ''' It does not indicate whether the shutdown will succeed. 
    ''' It is possible that the system, the user, or another application will abort the shutdown.
    ''' If the function fails, the return value is 'False'. 
    ''' </returns>
    Public Shared Function LogOff(ByVal Force As ForceType,
                                  ByVal Reason As ShutdownReason,
                                  ByVal Planning As PlanningType) As Boolean

        Return NativeMethods.ExitWindowsEx(ShutdownType.LogOff Or Force, Reason Or Planning)

    End Function

    ''' <summary>
    ''' Shuts down the system and turns off the power. 
    ''' </summary>
    ''' <param name="Force">
    ''' Indicates whether it's a forced shutdown.
    ''' </param>
    ''' <param name="Reason">
    ''' Indicates the reason for initiating the shutdown.
    ''' </param>
    ''' <param name="Planning">
    ''' Indicates the shutdown planning.
    ''' </param>
    ''' <returns>
    ''' If the function succeeds, the return value is 'True'. 
    ''' The function executes asynchronously so a 'True' return value indicates that the shutdown has been initiated. 
    ''' It does not indicate whether the shutdown will succeed. 
    ''' It is possible that the system, the user, or another application will abort the shutdown.
    ''' If the function fails, the return value is 'False'. 
    ''' </returns>
    Public Shared Function PowerOff(ByVal Force As ForceType,
                                    ByVal Reason As ShutdownReason,
                                    ByVal Planning As PlanningType) As Boolean

        Return NativeMethods.ExitWindowsEx(ShutdownType.PowerOff Or Force, Reason Or Planning)

    End Function

    ''' <summary>
    ''' Shuts down the system and then restarts it. 
    ''' </summary>
    ''' <param name="Force">
    ''' Indicates whether it's a forced shutdown.
    ''' </param>
    ''' <param name="Reason">
    ''' Indicates the reason for initiating the shutdown.
    ''' </param>
    ''' <param name="Planning">
    ''' Indicates the shutdown planning.
    ''' </param>
    ''' <returns>
    ''' If the function succeeds, the return value is 'True'. 
    ''' The function executes asynchronously so a 'True' return value indicates that the shutdown has been initiated. 
    ''' It does not indicate whether the shutdown will succeed. 
    ''' It is possible that the system, the user, or another application will abort the shutdown.
    ''' If the function fails, the return value is 'False'. 
    ''' </returns>
    Public Shared Function Reboot(ByVal Force As ForceType,
                                  ByVal Reason As ShutdownReason,
                                  ByVal Planning As PlanningType) As Boolean

        Return NativeMethods.ExitWindowsEx(ShutdownType.Reboot Or Force, Reason Or Planning)

    End Function

    ''' <summary>
    ''' Shuts down the system and then restarts it,
    ''' as well as any applications that have been registered for restart. 
    ''' </summary>
    ''' <param name="Force">
    ''' Indicates whether it's a forced shutdown.
    ''' </param>
    ''' <param name="Reason">
    ''' Indicates the reason for initiating the shutdown.
    ''' </param>
    ''' <param name="Planning">
    ''' Indicates the shutdown planning.
    ''' </param>
    ''' <returns>
    ''' If the function succeeds, the return value is 'True'. 
    ''' The function executes asynchronously so a 'True' return value indicates that the shutdown has been initiated. 
    ''' It does not indicate whether the shutdown will succeed. 
    ''' It is possible that the system, the user, or another application will abort the shutdown.
    ''' If the function fails, the return value is 'False'. 
    ''' </returns>
    Public Shared Function RestartApps(ByVal Force As ForceType,
                                      ByVal Reason As ShutdownReason,
                                      ByVal Planning As PlanningType) As Boolean

        Return NativeMethods.ExitWindowsEx(ShutdownType.RestartApps Or Force, Reason Or Planning)

    End Function

    ''' <summary>
    ''' Shuts down the system to a point at which it is safe to turn off the power. 
    ''' </summary>
    ''' <param name="Force">
    ''' Indicates whether it's a forced shutdown.
    ''' </param>
    ''' <param name="Reason">
    ''' Indicates the reason for initiating the shutdown.
    ''' </param>
    ''' <param name="Planning">
    ''' Indicates the shutdown planning.
    ''' </param>
    ''' <returns>
    ''' If the function succeeds, the return value is 'True'. 
    ''' The function executes asynchronously so a 'True' return value indicates that the shutdown has been initiated. 
    ''' It does not indicate whether the shutdown will succeed. 
    ''' It is possible that the system, the user, or another application will abort the shutdown.
    ''' If the function fails, the return value is 'False'. 
    ''' </returns>
    Public Shared Function ShutDown(ByVal Force As ForceType,
                                    ByVal Reason As ShutdownReason,
                                    ByVal Planning As PlanningType) As Boolean

        Return NativeMethods.ExitWindowsEx(ShutdownType.ShutDown Or Force, Reason Or Planning)

    End Function

#End Region

End Class

2 个答案:

答案 0 :(得分:3)

所以这是我对Windows NT如何处理它的关闭程序的理解:

这将启动一个关闭过程,该过程将WM_QUERYENDSESSION发送到每个正在运行的应用程序。如果异步关闭过程的初始化成功,它将返回非零值。

在无人值守终止时丢失数据的应用程序可能阻止/否决关闭并显示在用户界面上的列表中。

系统将提示用户决定是否继续关机,处理需要用户注意的应用程序或完全中止关机。 (自Vista起)

请注意,ExitWindowsEx仅限于互动用户

中止

此功能的MSDN文章实际上表明,非交互式用户应该调用另一个函数:

这要求调用线程具有SE_SHUTDOWN_NAMESE_REMOTE_SHUTDOWN_NAME权限,以分别启动本地或远程计算机的关闭。

成功启动关机程序时,它也会返回非零值。

此类关闭可以通过AbortSystemShutdown

中止

Trivia :可执行的shutdown.exe实际上使用了这种类型的关闭程序(事实上调用了Winuser函数)

答案 1 :(得分:0)

最后,这是我想要分享的方法,调查和所有那些XML文档编写非常困难。

这个助手类可以使用WinAPI注销,重启,关闭/关闭和中止操作。享受吧!

〜&GT; SystemRestarter for VB.NET - by Elektro

PS:也许它无法正常工作重启/中止远程机器,我无法以这种方式进行测试。

用法示例:

Sub Test()

   ' Restart the current computer in 30 seconds and wait for applications to close.
   ' Specify that the restart operation is planned because a consecuence of an installation.
   Dim Success =
   SystemRestarter.Restart(Nothing, 30, "System is gonna be restarted quickly, save all your data...!",
                           SystemRestarter.Enums.InitiateShutdown_Force.Wait,
                           SystemRestarter.Enums.ShutdownReason.MajorOperatingSystem Or
                           SystemRestarter.Enums.ShutdownReason.MinorInstallation,
                           SystemRestarter.Enums.ShutdownPlanning.Planned)

   Console.WriteLine(String.Format("Restart operation initiated successfully?: {0}", CStr(Success)))

   ' Abort the current operation.
   If Success Then
       Dim IsAborted = SystemRestarter.Abort()
       Console.WriteLine(String.Format("Restart operation aborted   successfully?: {0}", CStr(IsAborted)))
   Else
       Console.WriteLine("There is any restart operation to abort.")
   End If
   Console.ReadKey()

   ' Shutdown the current computer instantlly and force applications to close.
   ' ( When timeout is '0' the operation can't be aborted )
   SystemRestarter.Shutdown(Nothing, 0, Nothing, SystemRestarter.Enums.InitiateShutdown_Force.ForceSelf)

   ' LogOffs the current user.
   SystemRestarter.LogOff(SystemRestarter.Enums.ExitwindowsEx_Force.Wait)

End Sub