Advertisement
PiToLoKo

SystemRestarter for VB.NET - by Elektro

Feb 14th, 2014
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 50.18 KB | None | 0 0
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Modified : 02-15-2014
  4. ' ***********************************************************************
  5. ' <copyright file="SystemRestarter.vb" company="Elektro Studios">
  6. '     Copyright (c) Elektro Studios. All rights reserved.
  7. ' </copyright>
  8. ' ***********************************************************************
  9.  
  10. #Region " Usage Examples "
  11.  
  12. 'Sub Test()
  13.  
  14. '    ' Restart the current computer in 30 seconds and wait for applications to close.
  15. '    ' Specify that the restart operation is planned because a consecuence of an installation.
  16. '    Dim Success =
  17. '    SystemRestarter.Restart(Nothing, 30, "System is gonna be restarted quickly, save all your data...!",
  18. '                            SystemRestarter.Enums.InitiateShutdown_Force.Wait,
  19. '                            SystemRestarter.Enums.ShutdownReason.MajorOperatingSystem Or
  20. '                            SystemRestarter.Enums.ShutdownReason.MinorInstallation,
  21. '                            SystemRestarter.Enums.ShutdownPlanning.Planned)
  22.  
  23. '    Console.WriteLine(String.Format("Restart operation initiated successfully?: {0}", CStr(Success)))
  24.  
  25. '    ' Abort the current operation.
  26. '    If Success Then
  27. '        Dim IsAborted = SystemRestarter.Abort()
  28. '        Console.WriteLine(String.Format("Restart operation aborted   successfully?: {0}", CStr(IsAborted)))
  29. '    Else
  30. '        Console.WriteLine("There is any restart operation to abort.")
  31. '    End If
  32. '    Console.ReadKey()
  33.  
  34. '    ' Shutdown the current computer instantlly and force applications to close.
  35. '    ' ( When timeout is '0' the operation can't be aborted )
  36. '    SystemRestarter.Shutdown(Nothing, 0, Nothing, SystemRestarter.Enums.InitiateShutdown_Force.ForceSelf)
  37.  
  38. '    ' LogOffs the current user.
  39. '    SystemRestarter.LogOff(SystemRestarter.Enums.ExitwindowsEx_Force.Wait)
  40.  
  41. 'End Sub
  42.  
  43. #End Region
  44.  
  45. #Region " Imports "
  46.  
  47. Imports System.ComponentModel
  48. Imports System.Runtime.InteropServices
  49.  
  50. #End Region
  51.  
  52. #Region " SystemRestarter "
  53.  
  54. ''' <summary>
  55. ''' Performs different Shutdown system's operations on a local or remote machine.
  56. ''' </summary>
  57. Public Class SystemRestarter
  58.  
  59. #Region " P/Invoke "
  60.  
  61. #Region " Methods "
  62.  
  63.     ''' <summary>
  64.     ''' WinAPI methods used by the Main Class.
  65.     ''' </summary>
  66.     Private Class NativeMethods
  67.  
  68.         ''' <summary>
  69.         ''' Logs off the interactive user, shuts down the system, or shuts down and restarts the system.
  70.         ''' It sends the 'WM_QUERYENDSESSION' message to all applications to determine if they can be terminated.
  71.         ''' </summary>
  72.         ''' <param name="uFlags">
  73.         ''' Indicates the shutdown type.
  74.         ''' </param>
  75.         ''' <param name="dwReason">
  76.         ''' Indicates the reason for initiating the shutdown.
  77.         ''' </param>
  78.         ''' <returns>
  79.         ''' If the function succeeds, the return value is 'True'.
  80.         ''' The function executes asynchronously so a 'True' return value indicates that the shutdown has been initiated.
  81.         ''' It does not indicate whether the shutdown will succeed.
  82.         ''' It is possible that the system, the user, or another application will abort the shutdown.
  83.         ''' If the function fails, the return value is 'False'.
  84.         ''' </returns>
  85.         <DllImport("user32.dll", SetLastError:=True)>
  86.         Friend Shared Function ExitWindowsEx(
  87.                ByVal uFlags As Enums.ExitwindowsEx_Flags,
  88.                ByVal dwReason As Enums.ShutdownReason
  89.         ) As <MarshalAs(UnmanagedType.Bool)> Boolean
  90.         End Function
  91.  
  92.         ''' <summary>
  93.         ''' Initiates a shutdown and restart of the specified computer,
  94.         ''' and restarts any applications that have been registered for restart.
  95.         ''' </summary>
  96.         ''' <param name="lpMachineName">
  97.         ''' The name of the computer to be shut down.
  98.         ''' If the value of this parameter is 'NULL', the local computer is shut down.
  99.         ''' This parameter can be an addres, for example: '127.0.0.1'
  100.         ''' </param>
  101.         ''' <param name="lpMessage">
  102.         ''' The message to be displayed in the interactive shutdown dialog box.
  103.         ''' </param>
  104.         ''' <param name="dwGracePeriod">
  105.         ''' The number of seconds to wait before shutting down the computer.
  106.         ''' If the value of this parameter is zero, the computer is shut down immediately.
  107.         ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
  108.         ''' If the value of this parameter is greater than zero, and the 'dwShutdownFlags' parameter
  109.         ''' specifies the flag 'GRACE_OVERRIDE', the function fails and returns the error code 'ERROR_BAD_ARGUMENTS'.
  110.         ''' </param>
  111.         ''' <param name="dwShutdownFlags">
  112.         ''' Specifies options for the shutdown.
  113.         ''' </param>
  114.         ''' <param name="dwReason">
  115.         ''' The reason for initiating the shutdown.
  116.         ''' If this parameter is zero,
  117.         ''' the default is an undefined shutdown that is logged as "No title for this reason could be found".
  118.         ''' By default, it is also an 'unplanned' shutdown.
  119.         ''' </param>
  120.         ''' <returns>UInt32.</returns>
  121.         <DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
  122.         Friend Shared Function InitiateShutdown(
  123.                ByVal lpMachineName As String,
  124.                ByVal lpMessage As String,
  125.                ByVal dwGracePeriod As UInteger,
  126.                ByVal dwShutdownFlags As Enums.InitiateShutdown_Flags,
  127.                ByVal dwReason As Enums.ShutdownReason
  128.         ) As UInteger
  129.         End Function
  130.  
  131.         ''' <summary>
  132.         ''' Aborts a system shutdown that has been initiated.
  133.         ''' </summary>
  134.         ''' <param name="lpMachineName">
  135.         ''' The network name of the computer where the shutdown is to be stopped.
  136.         ''' If this parameter is 'NULL' or an empty string, the function aborts the shutdown on the local computer.
  137.         ''' </param>
  138.         ''' <returns><c>True</c> if the function succeeds, <c>False</c> otherwise.</returns>
  139.         <DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
  140.         Friend Shared Function AbortSystemShutdown(
  141.                Optional lpMachineName As String = "127.0.0.1"
  142.         ) As <MarshalAs(UnmanagedType.Bool)> Boolean
  143.         End Function
  144.  
  145.         ''' <summary>
  146.         ''' Opens the access token associated with a process.
  147.         ''' </summary>
  148.         ''' <param name="ProcessHandle">
  149.         ''' A handle to the process whose access token is opened.
  150.         ''' The process must have the 'PROCESS_QUERY_INFORMATION' access permission.
  151.         ''' </param>
  152.         ''' <param name="DesiredAccess">
  153.         ''' Specifies an access mask that specifies the requested types of access to the access token.
  154.         ''' These requested access types are compared with the discretionary access control list (DACL)
  155.         ''' of the token to determine which accesses are granted or denied.
  156.         ''' </param>
  157.         ''' <param name="TokenHandle">
  158.         ''' A pointer to a handle that identifies the newly opened access token when the function returns.
  159.         ''' </param>
  160.         ''' <returns>System.Int32.</returns>
  161.         <DllImport("advapi32.dll")>
  162.         Friend Shared Function OpenProcessToken(
  163.                ByVal ProcessHandle As IntPtr,
  164.                ByVal DesiredAccess As Enums.AccessRights,
  165.                ByRef TokenHandle As IntPtr
  166.         ) As Integer
  167.         End Function
  168.  
  169.         ''' <summary>
  170.         ''' Enables or disables privileges in the specified access token.
  171.         ''' Enabling or disabling privileges in an access token requires 'TOKEN_ADJUST_PRIVILEGES' access.
  172.         ''' </summary>
  173.         ''' <param name="TokenHandle">
  174.         ''' A handle to the access token that contains the privileges to be modified.
  175.         ''' The handle must have 'TOKEN_ADJUST_PRIVILEGES' access to the token.
  176.         ''' If the 'PreviousState' parameter is not NULL, the handle must also have 'TOKEN_QUERY' access.
  177.         ''' </param>
  178.         ''' <param name="DisableAllPrivileges">
  179.         ''' Specifies whether the function disables all of the token's privileges.
  180.         ''' If this value is 'TRUE', the function disables all privileges and ignores the 'NewState' parameter.
  181.         ''' If it is 'FALSE', the function modifies privileges based on the information pointed to by the NewState parameter.
  182.         ''' </param>
  183.         ''' <param name="NewState">
  184.         ''' A pointer to a 'TOKEN_PRIVILEGES' structure that specifies an array of privileges and their attributes.
  185.         ''' If the 'DisableAllPrivileges' parameter is 'FALSE',
  186.         ''' the 'AdjustTokenPrivileges' function enables, disables, or removes these privileges for the token.
  187.         ''' </param>
  188.         ''' <param name="BufferLength">
  189.         ''' Specifies the size, in bytes, of the buffer pointed to by the 'PreviousState' parameter.
  190.         ''' This parameter can be zero if the PreviousState parameter is 'NULL'.
  191.         ''' </param>
  192.         ''' <param name="PreviousState">
  193.         ''' A pointer to a buffer that the function fills with a 'TOKEN_PRIVILEGES' structure
  194.         ''' that contains the previous state of any privileges that the function modifies.
  195.         ''' That is, if a privilege has been modified by this function,
  196.         ''' the privilege and its previous state are contained in the 'TOKEN_PRIVILEGES' structure
  197.         ''' referenced by 'PreviousState'.
  198.         ''' If the 'PrivilegeCount' member of 'TOKEN_PRIVILEGES' is zero,
  199.         ''' then no privileges have been changed by this function.
  200.         ''' This parameter can be 'NULL'.</param>
  201.         ''' <param name="ReturnLength">
  202.         ''' A pointer to a variable that receives the required size, in bytes,
  203.         ''' of the buffer pointed to by the 'PreviousState' parameter.
  204.         ''' This parameter can be 'NULL' if 'PreviousState' is 'NULL'.
  205.         ''' </param>
  206.         ''' <returns>
  207.         ''' If the function succeeds, the return value is nonzero, otherwise, zero.
  208.         ''' To determine whether the function adjusted all of the specified privileges, call 'GetLastError'.</returns>
  209.         <DllImport("advapi32.dll", SetLastError:=True)>
  210.         Friend Shared Function AdjustTokenPrivileges(
  211.                ByVal TokenHandle As IntPtr,
  212.                <MarshalAs(UnmanagedType.Bool)> ByVal DisableAllPrivileges As Boolean,
  213.                ByRef NewState As Structures.TOKEN_PRIVILEGES,
  214.                ByVal BufferLength As UInt32,
  215.                ByVal PreviousState As IntPtr,
  216.                ByVal ReturnLength As IntPtr
  217.         ) As <MarshalAs(UnmanagedType.Bool)> Boolean
  218.         End Function
  219.  
  220.         ''' <summary>
  221.         ''' Retrieves the locally unique identifier (LUID) used on a specified system,
  222.         ''' to locally represent the specified privilege name.
  223.         ''' </summary>
  224.         ''' <param name="lpSystemName">
  225.         ''' A pointer to a null-terminated string that specifies the name of the system
  226.         ''' on which the privilege name is retrieved.
  227.         ''' If a null string is specified, the function attempts to find the privilege name on the local system
  228.         ''' </param>
  229.         ''' <param name="lpName">
  230.         ''' A pointer to a null-terminated string that specifies the name of the privilege,
  231.         ''' as defined in the Winnt.h header file.
  232.         ''' For example, this parameter could specify the constant, 'SE_SECURITY_NAME',
  233.         ''' or its corresponding string, "SeSecurityPrivilege".
  234.         ''' </param>
  235.         ''' <param name="lpLuid">
  236.         ''' A pointer to a variable that receives the LUID by which the privilege is known on
  237.         ''' the system specified by the lpSystemName parameter.
  238.         ''' </param>
  239.         ''' <returns>System.Int32.</returns>
  240.         <DllImport("advapi32.dll", CharSet:=CharSet.Unicode)>
  241.         Friend Shared Function LookupPrivilegeValue(
  242.                ByVal lpSystemName As String,
  243.                ByVal lpName As String,
  244.                ByRef lpLuid As Structures.LUID
  245.         ) As Integer
  246.         End Function
  247.  
  248.     End Class
  249.  
  250. #End Region
  251.  
  252. #Region " Read-Only Properties (Privileges) "
  253.  
  254.     ''' <summary>
  255.     ''' Privileges determine the type of system operations that a user account can perform.
  256.     ''' An administrator assigns privileges to user and group accounts.
  257.     ''' Each user's privileges include those granted to the user and to the groups to which the user belongs.
  258.     ''' </summary>
  259.     Private Class Privileges
  260.  
  261.         ''' <summary>
  262.         ''' Privilege required to shut down a system using a network request.
  263.         ''' User Right: Force shutdown from a remote system.
  264.         ''' </summary>
  265.         ''' For more Info see:
  266.         ''' http://msdn.microsoft.com/en-us/library/windows/desktop/bb530716%28v=vs.85%29.aspx
  267.         Public Shared ReadOnly Property SE_SHUTDOWN_NAME As String
  268.             Get
  269.                 Return "SeShutdownPrivilege"
  270.             End Get
  271.         End Property
  272.  
  273.         ''' <summary>
  274.         ''' Privilege required to shut down a local system.
  275.         ''' User Right: Shut down the system.
  276.         ''' </summary>
  277.         ''' For more Info see:
  278.         ''' http://msdn.microsoft.com/en-us/library/windows/desktop/bb530716%28v=vs.85%29.aspx
  279.         Public Shared ReadOnly Property SE_REMOTE_SHUTDOWN_NAME As String
  280.             Get
  281.                 Return "SeRemoteShutdownPrivilege"
  282.             End Get
  283.         End Property
  284.  
  285.     End Class
  286.  
  287. #End Region
  288.  
  289. #Region " Enumerations "
  290.  
  291.     ''' <summary>
  292.     ''' Enumerations used by the Main Class.
  293.     ''' </summary>
  294.     Public Class Enums
  295.  
  296.         ''' <summary>
  297.         ''' Indicates the shutdown type.
  298.         ''' </summary>
  299.         <Description("Enum used in the 'uFlags' parameter of 'ExitWindowsEx' Function.")>
  300.         <Flags>
  301.         Public Enum ExitwindowsEx_Flags As UInteger
  302.  
  303.             '******'
  304.             ' NOTE '
  305.             '******'    
  306.             ' This Enumeration is partially defined.
  307.  
  308.             ''' <summary>
  309.             ''' Shuts down all processes running in the logon session of the current process.
  310.             ''' Then it logs the user off.
  311.             ''' This flag can be used only by processes running in an interactive user's logon session.
  312.             ''' </summary>
  313.             LogOff = &H0UI
  314.  
  315.         End Enum
  316.  
  317.         ''' <summary>
  318.         ''' Indicates the forcing type.
  319.         ''' </summary>
  320.         <Description("Enum used in combination with the 'uFlags' parameter of 'ExitWindowsEx' Function.")>
  321.         Public Enum ExitwindowsEx_Force As UInteger
  322.  
  323.             ''' <summary>
  324.             ''' Don't force the system to close the applications.
  325.             ''' This is the default parameter.
  326.             ''' </summary>
  327.             Wait = &H0UI
  328.  
  329.             ''' <summary>
  330.             ''' This flag has no effect if terminal services is enabled.
  331.             ''' Otherwise, the system does not send the 'WM_QUERYENDSESSIO'N message.
  332.             ''' This can cause applications to lose data.
  333.             ''' Therefore, you should only use this flag in an emergency.
  334.             ''' </summary>
  335.             Force = &H4UI
  336.  
  337.             ''' <summary>
  338.             ''' Forces processes to terminate if they do not respond to the 'WM_QUERYENDSESSION',
  339.             ''' or 'WM_ENDSESSION' message within the timeout interval.
  340.             ''' </summary>
  341.             ForceIfHung = &H10UI
  342.  
  343.         End Enum
  344.  
  345.         ''' <summary>
  346.         ''' Indicates the shutdown type.
  347.         ''' </summary>
  348.         <Description("Enum used in the 'dwShutdownFlags' parameter of 'InitiateShutdown' Function.")>
  349.         <Flags>
  350.         Public Enum InitiateShutdown_Flags As UInteger
  351.  
  352.             ''' <summary>
  353.             ''' Overrides the grace period so that the computer is shut down immediately.
  354.             ''' </summary>
  355.             GraceOverride = &H20UI
  356.  
  357.             ''' <summary>
  358.             ''' Only for Windows 8/8.1
  359.             ''' Prepares the system for a faster startup by combining
  360.             ''' the 'HybridShutDown' flag with the 'ShutDown' flag.
  361.             ''' 'InitiateShutdown' always initiate a full system shutdown if the 'HybridShutdown' flag is not set.
  362.             ''' </summary>
  363.             HybridShutdown = &H200UI
  364.  
  365.             ''' <summary>
  366.             ''' The computer installs any updates before starting the shutdown.
  367.             ''' </summary>
  368.             InstallUpdates = &H40UI
  369.  
  370.             ''' <summary>
  371.             ''' The computer is shut down but is not powered down or restarted.
  372.             ''' </summary>
  373.             Shutdown = &H10UI
  374.  
  375.             ''' <summary>
  376.             ''' The computer is shut down and powered down.
  377.             ''' </summary>
  378.             PowerOff = &H8UI
  379.  
  380.             ''' <summary>
  381.             ''' The computer is shut down and restarted.
  382.             ''' </summary>
  383.             Restart = &H4UI
  384.  
  385.             ''' <summary>
  386.             ''' The system is restarted using the 'ExitWindowsEx' function with the 'RESTARTAPPS' flag.
  387.             ''' This restarts any applications that have been registered for restart
  388.             ''' using the 'RegisterApplicationRestart' function.
  389.             ''' </summary>
  390.             RestartApps = &H80UI
  391.  
  392.         End Enum
  393.  
  394.         ''' <summary>
  395.         ''' Indicates the forced shutdown type.
  396.         ''' </summary>
  397.         <Description("Enum used in combination with the 'uFlags' parameter of 'InitiateShutdown' Function.")>
  398.         Public Enum InitiateShutdown_Force As UInteger
  399.  
  400.             ''' <summary>
  401.             ''' Don't force the system to close the applications.
  402.             ''' This is the default parameter.
  403.             ''' </summary>
  404.             Wait = &H0UI
  405.  
  406.             ''' <summary>
  407.             ''' All sessions are forcefully logged off.
  408.             ''' If this flag is not set and users other than the current user are logged on to the computer
  409.             ''' specified by the 'lpMachineName' parameter.
  410.             ''' </summary>
  411.             ForceOthers = &H1UI
  412.  
  413.             ''' <summary>
  414.             ''' Specifies that the originating session is logged off forcefully.
  415.             ''' If this flag is not set, the originating session is shut down interactively,
  416.             ''' so a shutdown is not guaranteed even if the function returns successfully.
  417.             ''' </summary>
  418.             ForceSelf = &H2UI
  419.  
  420.         End Enum
  421.  
  422.         ''' <summary>
  423.         ''' Indicates the shutdown reason codes.
  424.         ''' You can specify any minor reason in combination with any major reason, but some combinations do not make sense.
  425.         ''' </summary>
  426.         ''' For more info see here:
  427.         ''' http://msdn.microsoft.com/en-us/library/windows/desktop/aa376885%28v=vs.85%29.aspx
  428.         <Description("Enum used in the 'dwReason' parameter of 'ExitWindowsEx' Function.")>
  429.         <Flags()>
  430.         Public Enum ShutdownReason As UInteger
  431.  
  432.             ''' <summary>
  433.             ''' Application issue.
  434.             ''' </summary>
  435.             MajorApplication = &H40000
  436.  
  437.             ''' <summary>
  438.             ''' Hardware issue.
  439.             ''' </summary>
  440.             MajorHardware = &H10000
  441.  
  442.             ''' <summary>
  443.             ''' The 'InitiateSystemShutdown' function was used instead of 'InitiateSystemShutdownEx'.
  444.             ''' </summary>
  445.             MajorLegacyApi = &H70000
  446.  
  447.             ''' <summary>
  448.             ''' Operating system issue.
  449.             ''' </summary>
  450.             MajorOperatingSystem = &H20000
  451.  
  452.             ''' <summary>
  453.             ''' Other issue.
  454.             ''' </summary>
  455.             MajorOther = &H0
  456.  
  457.             ''' <summary>
  458.             ''' Power failure.
  459.             ''' </summary>
  460.             MajorPower = &H60000
  461.  
  462.             ''' <summary>
  463.             ''' Software issue.
  464.             ''' </summary>
  465.             MajorSoftware = &H30000
  466.  
  467.             ''' <summary>
  468.             ''' System failure..
  469.             ''' </summary>
  470.             MajorSystem = &H50000
  471.  
  472.             ''' <summary>
  473.             ''' Blue screen crash event.
  474.             ''' </summary>
  475.             MinorBlueScreen = &HF
  476.  
  477.             ''' <summary>
  478.             ''' Unplugged.
  479.             ''' </summary>
  480.             MinorCordUnplugged = &HB
  481.  
  482.             ''' <summary>
  483.             ''' Disk.
  484.             ''' </summary>
  485.             MinorDisk = &H7
  486.  
  487.             ''' <summary>
  488.             ''' Environment.
  489.             ''' </summary>
  490.             MinorEnvironment = &HC
  491.  
  492.             ''' <summary>
  493.             ''' Driver.
  494.             ''' </summary>
  495.             MinorHardwareDriver = &HD
  496.  
  497.             ''' <summary>
  498.             ''' Hot fix.
  499.             ''' </summary>
  500.             MinorHotfix = &H11
  501.  
  502.             ''' <summary>
  503.             ''' Hot fix uninstallation.
  504.             ''' </summary>
  505.             MinorHotfixUninstall = &H17
  506.  
  507.             ''' <summary>
  508.             ''' Unresponsive.
  509.             ''' </summary>
  510.             MinorHung = &H5
  511.  
  512.             ''' <summary>
  513.             ''' Installation.
  514.             ''' </summary>
  515.             MinorInstallation = &H2
  516.  
  517.             ''' <summary>
  518.             ''' Maintenance.
  519.             ''' </summary>
  520.             MinorMaintenance = &H1
  521.  
  522.             ''' <summary>
  523.             ''' MMC issue.
  524.             ''' </summary>
  525.             MinorMMC = &H19
  526.  
  527.             ''' <summary>
  528.             ''' Network connectivity.
  529.             ''' </summary>
  530.             MinorNetworkConnectivity = &H14
  531.  
  532.             ''' <summary>
  533.             ''' Network card.
  534.             ''' </summary>
  535.             MinorNetworkCard = &H9
  536.  
  537.             ''' <summary>
  538.             ''' Other issue.
  539.             ''' </summary>
  540.             MinorOther = &H0
  541.  
  542.             ''' <summary>
  543.             ''' Other driver event.
  544.             ''' </summary>
  545.             MinorOtherDriver = &HE
  546.  
  547.             ''' <summary>
  548.             ''' Power supply.
  549.             ''' </summary>
  550.             MinorPowerSupply = &HA
  551.  
  552.             ''' <summary>
  553.             ''' Processor.
  554.             ''' </summary>
  555.             MinorProcessor = &H8
  556.  
  557.             ''' <summary>
  558.             ''' Reconfigure.
  559.             ''' </summary>
  560.             MinorReconfig = &H4
  561.  
  562.             ''' <summary>
  563.             ''' Security issue.
  564.             ''' </summary>
  565.             MinorSecurity = &H13
  566.  
  567.             ''' <summary>
  568.             ''' Security patch.
  569.             ''' </summary>
  570.             MinorSecurityFix = &H12
  571.  
  572.             ''' <summary>
  573.             ''' Security patch uninstallation.
  574.             ''' </summary>
  575.             MinorSecurityFixUninstall = &H18
  576.  
  577.             ''' <summary>
  578.             ''' Service pack.
  579.             ''' </summary>
  580.             MinorServicePack = &H10
  581.  
  582.             ''' <summary>
  583.             ''' Service pack uninstallation.
  584.             ''' </summary>
  585.             MinorServicePackUninstall = &H16
  586.  
  587.             ''' <summary>
  588.             ''' Terminal Services.
  589.             ''' </summary>
  590.             MinorTermSrv = &H20
  591.  
  592.             ''' <summary>
  593.             ''' Unstable.
  594.             ''' </summary>
  595.             MinorUnstable = &H6
  596.  
  597.             ''' <summary>
  598.             ''' Upgrade.
  599.             ''' </summary>
  600.             MinorUpgrade = &H3
  601.  
  602.             ''' <summary>
  603.             ''' WMI issue.
  604.             ''' </summary>
  605.             MinorWMI = &H15
  606.  
  607.         End Enum
  608.  
  609.         ''' <summary>
  610.         ''' Indicates the shutdown reason planning.
  611.         ''' </summary>
  612.         ''' For more info see here:
  613.         ''' http://msdn.microsoft.com/en-us/library/windows/desktop/aa376885%28v=vs.85%29.aspx
  614.         <Description("Enum used in combination with the 'dwReason' parameter of 'ExitWindowsEx' and 'InitiateShutdown' Functions.")>
  615.         Public Enum ShutdownPlanning As UInteger
  616.  
  617.             ''' <summary>
  618.             ''' The shutdown was unplanned.
  619.             ''' This is the default parameter.
  620.             ''' </summary>
  621.             Unplanned = &H0UI
  622.  
  623.             ''' <summary>
  624.             ''' The reason code is defined by the user.
  625.             ''' For more information, see Defining a Custom Reason Code.
  626.             ''' If this flag is not present, the reason code is defined by the system.
  627.             ''' </summary>
  628.             UserDefined = &H40000000UI
  629.  
  630.             ''' <summary>
  631.             ''' The shutdown was planned.
  632.             ''' The system generates a System State Data (SSD) file.
  633.             ''' This file contains system state information such as the processes, threads, memory usage, and configuration.
  634.             ''' If this flag is not present, the shutdown was unplanned.
  635.             ''' </summary>
  636.             Planned = &H80000000UI
  637.  
  638.         End Enum
  639.  
  640.         ''' <summary>
  641.         ''' The attributes of a privilege.
  642.         ''' </summary>
  643.         <Description("Enum used in the 'Privileges' parameter of 'TOKEN_PRIVILEGES' structure.")>
  644.         <Flags>
  645.         Public Enum TOKEN_PRIVILEGES_FLAGS As UInteger
  646.  
  647.             ''' <summary>
  648.             ''' The privilege is enabled by default.
  649.             ''' </summary>
  650.             SE_PRIVILEGE_ENABLED_BY_DEFAULT = &H1UI
  651.  
  652.             ''' <summary>
  653.             ''' The privilege is enabled.
  654.             ''' </summary>
  655.             SE_PRIVILEGE_ENABLED = &H2UI
  656.  
  657.             ''' <summary>
  658.             ''' Used to remove a privilege.
  659.             ''' </summary>
  660.             SE_PRIVILEGE_REMOVED = &H4UI
  661.  
  662.             ''' <summary>
  663.             ''' The privilege was used to gain access to an object or service.
  664.             ''' This flag is used to identify the relevant privileges
  665.             ''' in a set passed by a client application that may contain unnecessary privileges
  666.             ''' </summary>
  667.             SE_PRIVILEGE_USED_FOR_ACCESS = &H80000000UI
  668.  
  669.         End Enum
  670.  
  671.         ''' <summary>
  672.         ''' An application cannot change the access control list of an object unless the application has the rights to do so.
  673.         ''' These rights are controlled by a security descriptor in the access token for the object.
  674.         ''' </summary>
  675.         ''' For more info see here:
  676.         ''' http://msdn.microsoft.com/en-us/library/windows/desktop/aa374905%28v=vs.85%29.aspx
  677.         <Description("Enum used in the 'DesiredAccess' parameter of 'OpenProcessToken' Function.")>
  678.         <Flags>
  679.         Public Enum AccessRights As UInteger
  680.  
  681.             '******'
  682.             ' NOTE '
  683.             '******'    
  684.             ' This Enumeration is partially defined.
  685.  
  686.             ''' <summary>
  687.             ''' Required to enable or disable the privileges in an access token
  688.             ''' </summary>
  689.             TOKEN_ADJUST_PRIVILEGES = &H32UI
  690.  
  691.             ''' <summary>
  692.             ''' Required to query an access token
  693.             ''' </summary>
  694.             TOKEN_QUERY = &H8UI
  695.  
  696.         End Enum
  697.  
  698.     End Class
  699.  
  700. #End Region
  701.  
  702. #Region " Structures "
  703.  
  704.     ''' <summary>
  705.     ''' Structures used by the Main Class.
  706.     ''' </summary>
  707.     Private Class Structures
  708.  
  709.         ''' <summary>
  710.         ''' An 'LUID' is a 64-bit value guaranteed to be unique only on the system on which it was generated.
  711.         ''' The uniqueness of a locally unique identifier (LUID) is guaranteed only until the system is restarted.
  712.         ''' </summary>
  713.         Friend Structure LUID
  714.  
  715.             ''' <summary>
  716.             ''' The Low-order bits.
  717.             ''' </summary>
  718.             Public LowPart As Integer
  719.  
  720.             ''' <summary>
  721.             ''' The High-order bits.
  722.             ''' </summary>
  723.             Public HighPart As Integer
  724.  
  725.         End Structure
  726.  
  727.         ''' <summary>
  728.         ''' Represents a locally unique identifier (LUID) and its attributes.
  729.         ''' </summary>
  730.         Friend Structure LUID_AND_ATTRIBUTES
  731.  
  732.             ''' <summary>
  733.             ''' Specifies an 'LUID' value.
  734.             ''' </summary>
  735.             Public pLuid As LUID
  736.  
  737.             ''' <summary>
  738.             ''' Specifies attributes of the 'LUID'.
  739.             ''' This value contains up to 32 one-bit flags.
  740.             ''' Its meaning is dependent on the definition and use of the 'LUID'.
  741.             ''' </summary>
  742.             Public Attributes As Enums.TOKEN_PRIVILEGES_FLAGS
  743.  
  744.         End Structure
  745.  
  746.         ''' <summary>
  747.         ''' Contains information about a set of privileges for an access token.
  748.         ''' </summary>
  749.         Friend Structure TOKEN_PRIVILEGES
  750.  
  751.             ''' <summary>
  752.             ''' This must be set to the number of entries in the Privileges array
  753.             ''' </summary>
  754.             Public PrivilegeCount As Integer
  755.  
  756.             ''' <summary>
  757.             ''' Specifies an array of 'LUID_AND_ATTRIBUTES' structures.
  758.             ''' Each structure contains the 'LUID' and attributes of a privilege.
  759.             ''' To get the name of the privilege associated with a 'LUID', call the 'LookupPrivilegeName' function,
  760.             ''' passing the address of the 'LUID' as the value of the 'lpLuid' parameter.
  761.             ''' </summary>
  762.             Public Privileges As LUID_AND_ATTRIBUTES
  763.  
  764.         End Structure
  765.  
  766.     End Class
  767.  
  768. #End Region
  769.  
  770. #End Region
  771.  
  772. #Region " Private Methods "
  773.  
  774.     ''' <summary>
  775.     ''' Gets the necessary shutdown privileges to perform a local shutdown operation.
  776.     ''' </summary>
  777.     ''' <param name="Computer">
  778.     ''' Indicates the computer where to set the privileges.
  779.     ''' If a null string is specified, the function attempts to find the privilege name on the local system
  780.     ''' </param>
  781.     Private Shared Sub GetLocalShutdownPrivileges(ByVal Computer As String)
  782.  
  783.         Dim hToken As IntPtr
  784.         Dim tkp As Structures.TOKEN_PRIVILEGES
  785.  
  786.         NativeMethods.OpenProcessToken(Process.GetCurrentProcess.Handle,
  787.                                        Enums.AccessRights.TOKEN_ADJUST_PRIVILEGES Or Enums.AccessRights.TOKEN_QUERY,
  788.                                        hToken)
  789.  
  790.         With tkp
  791.             .PrivilegeCount = 1
  792.             .Privileges.Attributes = Enums.TOKEN_PRIVILEGES_FLAGS.SE_PRIVILEGE_ENABLED
  793.         End With
  794.  
  795.         NativeMethods.LookupPrivilegeValue(Computer,
  796.                                            Privileges.SE_SHUTDOWN_NAME,
  797.                                            tkp.Privileges.pLuid)
  798.  
  799.         NativeMethods.AdjustTokenPrivileges(hToken, False, tkp,
  800.                                             0UI, IntPtr.Zero, IntPtr.Zero)
  801.  
  802.     End Sub
  803.  
  804.     ''' <summary>
  805.     ''' Gets the necessary shutdown privileges to perform a remote shutdown operation.
  806.     ''' </summary>
  807.     ''' <param name="Computer">
  808.     ''' Indicates the computer where to set the privileges.
  809.     ''' If a null string is specified, the function attempts to find the privilege name on the local system
  810.     ''' </param>
  811.     Private Shared Sub GetRemoteShutdownPrivileges(ByVal Computer As String)
  812.  
  813.         Dim hToken As IntPtr
  814.         Dim tkp As Structures.TOKEN_PRIVILEGES
  815.  
  816.         NativeMethods.OpenProcessToken(Process.GetCurrentProcess.Handle,
  817.                                        Enums.AccessRights.TOKEN_ADJUST_PRIVILEGES Or Enums.AccessRights.TOKEN_QUERY,
  818.                                        hToken)
  819.  
  820.         With tkp
  821.             .PrivilegeCount = 1
  822.             .Privileges.Attributes = Enums.TOKEN_PRIVILEGES_FLAGS.SE_PRIVILEGE_ENABLED
  823.         End With
  824.  
  825.         NativeMethods.LookupPrivilegeValue(Computer,
  826.                                            Privileges.SE_REMOTE_SHUTDOWN_NAME,
  827.                                            tkp.Privileges.pLuid)
  828.  
  829.         NativeMethods.AdjustTokenPrivileges(hToken, False, tkp,
  830.                                             0UI, IntPtr.Zero, IntPtr.Zero)
  831.  
  832.     End Sub
  833.  
  834. #End Region
  835.  
  836. #Region " Public Methods "
  837.  
  838.     ''' <summary>
  839.     ''' Aborts a system shutdown operation that has been initiated (unless a LogOff).
  840.     ''' </summary>
  841.     ''' <param name="Computer">
  842.     ''' The network name of the computer where the shutdown is to be stopped.
  843.     ''' If this parameter is 'NULL' or an empty string, the function aborts the shutdown on the local computer.
  844.     ''' </param>
  845.     ''' <returns><c>True</c> if the function succeeds, <c>False</c> otherwise.</returns>
  846.     Public Shared Function Abort(Optional ByVal Computer As String = Nothing) As Boolean
  847.  
  848.         Return NativeMethods.AbortSystemShutdown(Computer)
  849.  
  850.     End Function
  851.  
  852.     ''' <summary>
  853.     ''' Shuts down all processes running in the logon session and then logs off the interactive user.
  854.     ''' </summary>
  855.     ''' <param name="Force">
  856.     ''' Indicates whether to force the logoff.
  857.     ''' </param>
  858.     ''' <param name="Reason">
  859.     ''' Indicates the reason for initiating the shutdown.
  860.     ''' </param>
  861.     ''' <returns>
  862.     ''' If the function succeeds, the return value is 'True'.
  863.     ''' The function executes asynchronously so a 'True' return value indicates that the shutdown has been initiated.
  864.     ''' It does not indicate whether the shutdown will succeed.
  865.     ''' It is possible that the system, the user, or another application will abort the shutdown.
  866.     ''' If the function fails, the return value is 'False'.
  867.     ''' </returns>
  868.     Public Shared Function LogOff(Optional ByVal Force As Enums.ExitwindowsEx_Force =
  869.                                                           Enums.ExitwindowsEx_Force.Wait,
  870.                                   Optional ByVal Reason As Enums.ShutdownReason =
  871.                                                            Enums.ShutdownReason.MajorOther) As Boolean
  872.  
  873.         GetLocalShutdownPrivileges(Nothing)
  874.         GetRemoteShutdownPrivileges(Nothing)
  875.  
  876.         Return NativeMethods.ExitWindowsEx(Enums.ExitwindowsEx_Flags.LogOff Or Force, Reason)
  877.  
  878.     End Function
  879.  
  880.     ''' <summary>
  881.     ''' Shutdowns the specified computer and begins powered down.
  882.     ''' </summary>
  883.     ''' <param name="Computer">
  884.     ''' The name of the computer to poweroff.
  885.     ''' If the value of this parameter is 'NULL', the local computer is shut down.
  886.     ''' This parameter can be an addres, for example: '127.0.0.1'
  887.     ''' </param>
  888.     ''' <param name="Message">
  889.     ''' The message to be displayed in the interactive poweroff dialog box.
  890.     ''' </param>
  891.     ''' <param name="Timeout">
  892.     ''' The number of seconds to wait before shutting down the computer.
  893.     ''' If the value of this parameter is zero, the computer is poweroff immediately.
  894.     ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
  895.     ''' If the value of this parameter is greater than zero, and the 'dwShutdownFlags' parameter
  896.     ''' specifies the flag 'GRACE_OVERRIDE', the function fails and returns the error code 'ERROR_BAD_ARGUMENTS'.
  897.     ''' </param>
  898.     ''' <param name="Force">
  899.     ''' Indicates whether to force the PowerOff.
  900.     ''' </param>
  901.     ''' <param name="Reason">
  902.     ''' The reason for initiating the poweroff.
  903.     ''' If this parameter is zero,
  904.     ''' the default is an undefined poweroff that is logged as "No title for this reason could be found".
  905.     ''' By default, it is also an 'unplanned' poweroff.
  906.     ''' </param>
  907.     ''' <param name="Planning">
  908.     ''' Indicates whether it's a planned or unplanned PowerOff operation.
  909.     ''' </param>
  910.     ''' <returns>
  911.     ''' <c>true</c> if the poweroff operation is initiated correctlly, <c>false</c> otherwise.
  912.     ''' </returns>
  913.     Public Shared Function PowerOff(Optional ByVal Computer As String = Nothing,
  914.                                     Optional ByVal Timeout As Integer = 0,
  915.                                     Optional ByVal Message As String = Nothing,
  916.                                     Optional ByVal Force As Enums.InitiateShutdown_Force =
  917.                                                             Enums.InitiateShutdown_Force.Wait,
  918.                                     Optional ByVal Reason As Enums.ShutdownReason =
  919.                                                              Enums.ShutdownReason.MajorOther,
  920.                                     Optional ByVal Planning As Enums.ShutdownPlanning =
  921.                                                                Enums.ShutdownPlanning.Unplanned) As Boolean
  922.  
  923.         GetLocalShutdownPrivileges(Computer)
  924.         GetRemoteShutdownPrivileges(Computer)
  925.  
  926.         Select Case Timeout
  927.  
  928.             Case Is = 0
  929.                 Return Not Convert.ToBoolean(
  930.                     NativeMethods.InitiateShutdown(Computer, Message, Timeout,
  931.                                                    Enums.InitiateShutdown_Flags.PowerOff Or
  932.                                                    Enums.InitiateShutdown_Flags.GraceOverride Or Force,
  933.                                                    Reason Or Planning))
  934.  
  935.             Case Else
  936.                 Return Not Convert.ToBoolean(
  937.                     NativeMethods.InitiateShutdown(Computer, Message, Timeout,
  938.                                                    Enums.InitiateShutdown_Flags.PowerOff Or Force,
  939.                                                    Reason Or Planning))
  940.  
  941.         End Select
  942.  
  943.     End Function
  944.  
  945.     ''' <summary>
  946.     ''' Restarts the specified computer.
  947.     ''' </summary>
  948.     ''' <param name="Computer">
  949.     ''' The name of the computer to restart.
  950.     ''' If the value of this parameter is 'NULL', the local computer is shut down.
  951.     ''' This parameter can be an addres, for example: '127.0.0.1'
  952.     ''' </param>
  953.     ''' <param name="Message">
  954.     ''' The message to be displayed in the interactive restart dialog box.
  955.     ''' </param>
  956.     ''' <param name="Timeout">
  957.     ''' The number of seconds to wait before restarting the computer.
  958.     ''' If the value of this parameter is zero, the computer is restarted immediately.
  959.     ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
  960.     ''' If the value of this parameter is greater than zero, and the 'dwShutdownFlags' parameter
  961.     ''' specifies the flag 'GRACE_OVERRIDE', the function fails and returns the error code 'ERROR_BAD_ARGUMENTS'.
  962.     ''' </param>
  963.     ''' <param name="Force">
  964.     ''' Indicates whether to force the restart.
  965.     ''' </param>
  966.     ''' <param name="Reason">
  967.     ''' The reason for initiating the restart.
  968.     ''' If this parameter is zero,
  969.     ''' the default is an undefined restart that is logged as "No title for this reason could be found".
  970.     ''' By default, it is also an 'unplanned' restart.
  971.     ''' </param>
  972.     ''' <param name="Planning">
  973.     ''' Indicates whether it's a planned or unplanned restart operation.
  974.     ''' </param>
  975.     ''' <returns>
  976.     ''' <c>true</c> if the restart operation is initiated correctlly, <c>false</c> otherwise.
  977.     ''' </returns>
  978.     Public Shared Function Restart(Optional ByVal Computer As String = Nothing,
  979.                                    Optional ByVal Timeout As Integer = 0,
  980.                                    Optional ByVal Message As String = Nothing,
  981.                                    Optional ByVal Force As Enums.InitiateShutdown_Force =
  982.                                                            Enums.InitiateShutdown_Force.Wait,
  983.                                    Optional ByVal Reason As Enums.ShutdownReason =
  984.                                                              Enums.ShutdownReason.MajorOther,
  985.                                    Optional ByVal Planning As Enums.ShutdownPlanning =
  986.                                                               Enums.ShutdownPlanning.Unplanned) As Boolean
  987.  
  988.         GetLocalShutdownPrivileges(Computer)
  989.         GetRemoteShutdownPrivileges(Computer)
  990.  
  991.         Select Case Timeout
  992.  
  993.             Case Is = 0
  994.                 Return Not Convert.ToBoolean(
  995.                     NativeMethods.InitiateShutdown(Computer, Message, Timeout,
  996.                                                    Enums.InitiateShutdown_Flags.Restart Or
  997.                                                    Enums.InitiateShutdown_Flags.GraceOverride Or Force,
  998.                                                    Reason Or Planning))
  999.  
  1000.             Case Else
  1001.                 Return Not Convert.ToBoolean(
  1002.                     NativeMethods.InitiateShutdown(Computer, Message, Timeout,
  1003.                                                    Enums.InitiateShutdown_Flags.Restart Or Force,
  1004.                                                    Reason Or Planning))
  1005.  
  1006.         End Select
  1007.  
  1008.     End Function
  1009.  
  1010.     ''' <summary>
  1011.     ''' Restarts the specified computer,
  1012.     ''' also restarts any applications that have been registered for restart
  1013.     ''' using the 'RegisterApplicationRestart' function.
  1014.     ''' </summary>
  1015.     ''' <param name="Computer">
  1016.     ''' The name of the computer to restart.
  1017.     ''' If the value of this parameter is 'NULL', the local computer is shut down.
  1018.     ''' This parameter can be an addres, for example: '127.0.0.1'
  1019.     ''' </param>
  1020.     ''' <param name="Message">
  1021.     ''' The message to be displayed in the interactive restart dialog box.
  1022.     ''' </param>
  1023.     ''' <param name="Timeout">
  1024.     ''' The number of seconds to wait before restarting the computer.
  1025.     ''' If the value of this parameter is zero, the computer is restarted immediately.
  1026.     ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
  1027.     ''' If the value of this parameter is greater than zero, and the 'dwShutdownFlags' parameter
  1028.     ''' specifies the flag 'GRACE_OVERRIDE', the function fails and returns the error code 'ERROR_BAD_ARGUMENTS'.
  1029.     ''' </param>
  1030.     ''' <param name="Force">
  1031.     ''' Indicates whether to force the restart.
  1032.     ''' </param>
  1033.     ''' <param name="Reason">
  1034.     ''' The reason for initiating the restart.
  1035.     ''' If this parameter is zero,
  1036.     ''' the default is an undefined restart that is logged as "No title for this reason could be found".
  1037.     ''' By default, it is also an 'unplanned' restart.
  1038.     ''' </param>
  1039.     ''' <param name="Planning">
  1040.     ''' Indicates whether it's a planned or unplanned restart operation.
  1041.     ''' </param>
  1042.     ''' <returns>
  1043.     ''' <c>true</c> if the restart operation is initiated correctlly, <c>false</c> otherwise.
  1044.     ''' </returns>
  1045.     Public Shared Function RestartApps(Optional ByVal Computer As String = Nothing,
  1046.                                        Optional ByVal Timeout As Integer = 0,
  1047.                                        Optional ByVal Message As String = Nothing,
  1048.                                        Optional ByVal Force As Enums.InitiateShutdown_Force =
  1049.                                                                Enums.InitiateShutdown_Force.Wait,
  1050.                                        Optional ByVal Reason As Enums.ShutdownReason =
  1051.                                                                 Enums.ShutdownReason.MajorOther,
  1052.                                        Optional ByVal Planning As Enums.ShutdownPlanning =
  1053.                                                                   Enums.ShutdownPlanning.Unplanned) As Boolean
  1054.  
  1055.         GetLocalShutdownPrivileges(Computer)
  1056.         GetRemoteShutdownPrivileges(Computer)
  1057.  
  1058.         Select Case Timeout
  1059.  
  1060.             Case Is = 0
  1061.                 Return Not Convert.ToBoolean(
  1062.                     NativeMethods.InitiateShutdown(Computer, Message, Timeout,
  1063.                                                    Enums.InitiateShutdown_Flags.RestartApps Or
  1064.                                                    Enums.InitiateShutdown_Flags.GraceOverride Or Force,
  1065.                                                    Reason Or Planning))
  1066.  
  1067.             Case Else
  1068.                 Return Not Convert.ToBoolean(
  1069.                     NativeMethods.InitiateShutdown(Computer, Message, Timeout,
  1070.                                                    Enums.InitiateShutdown_Flags.RestartApps Or Force,
  1071.                                                    Reason Or Planning))
  1072.  
  1073.         End Select
  1074.  
  1075.     End Function
  1076.  
  1077.     ''' <summary>
  1078.     ''' Shutdowns the specified computer.
  1079.     ''' </summary>
  1080.     ''' <param name="Computer">
  1081.     ''' The name of the computer to be shut down.
  1082.     ''' If the value of this parameter is 'NULL', the local computer is shut down.
  1083.     ''' This parameter can be an addres, for example: '127.0.0.1'
  1084.     ''' </param>
  1085.     ''' <param name="Message">
  1086.     ''' The message to be displayed in the interactive shutdown dialog box.
  1087.     ''' </param>
  1088.     ''' <param name="Timeout">
  1089.     ''' The number of seconds to wait before shutting down the computer.
  1090.     ''' If the value of this parameter is zero, the computer is shut down immediately.
  1091.     ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
  1092.     ''' If the value of this parameter is greater than zero, and the 'dwShutdownFlags' parameter
  1093.     ''' specifies the flag 'GRACE_OVERRIDE', the function fails and returns the error code 'ERROR_BAD_ARGUMENTS'.
  1094.     ''' </param>
  1095.     ''' <param name="Force">
  1096.     ''' Indicates whether to force the shutdown.
  1097.     ''' </param>
  1098.     ''' <param name="Reason">
  1099.     ''' The reason for initiating the shutdown.
  1100.     ''' If this parameter is zero,
  1101.     ''' the default is an undefined shutdown that is logged as "No title for this reason could be found".
  1102.     ''' By default, it is also an 'unplanned' shutdown.
  1103.     ''' </param>
  1104.     ''' <param name="Planning">
  1105.     ''' Indicates whether it's a planned or unplanned shutdoen operation.
  1106.     ''' </param>
  1107.     ''' <returns>
  1108.     ''' <c>true</c> if the shutdown operation is initiated correctlly, <c>false</c> otherwise.
  1109.     ''' </returns>
  1110.     Public Shared Function Shutdown(Optional ByVal Computer As String = Nothing,
  1111.                                     Optional ByVal Timeout As Integer = 0,
  1112.                                     Optional ByVal Message As String = Nothing,
  1113.                                     Optional ByVal Force As Enums.InitiateShutdown_Force =
  1114.                                                             Enums.InitiateShutdown_Force.Wait,
  1115.                                     Optional ByVal Reason As Enums.ShutdownReason =
  1116.                                                              Enums.ShutdownReason.MajorOther,
  1117.                                     Optional ByVal Planning As Enums.ShutdownPlanning =
  1118.                                                                Enums.ShutdownPlanning.Unplanned) As Boolean
  1119.  
  1120.         GetLocalShutdownPrivileges(Computer)
  1121.         GetRemoteShutdownPrivileges(Computer)
  1122.  
  1123.         Select Case Timeout
  1124.  
  1125.             Case Is = 0
  1126.                 Return Not Convert.ToBoolean(
  1127.                     NativeMethods.InitiateShutdown(Computer, Message, Timeout,
  1128.                                                    Enums.InitiateShutdown_Flags.Shutdown Or
  1129.                                                    Enums.InitiateShutdown_Flags.GraceOverride Or Force,
  1130.                                                    Reason Or Planning))
  1131.  
  1132.             Case Else
  1133.                 Return Not Convert.ToBoolean(
  1134.                     NativeMethods.InitiateShutdown(Computer, Message, Timeout,
  1135.                                                    Enums.InitiateShutdown_Flags.Shutdown Or Force,
  1136.                                                    Reason Or Planning))
  1137.  
  1138.         End Select
  1139.  
  1140.     End Function
  1141.  
  1142.     ''' <summary>
  1143.     ''' Use this function only for Windows 8/8.1
  1144.     ''' Shutdowns the specified computer and prepares the system for a faster startup.
  1145.     ''' </summary>
  1146.     ''' <param name="Computer">
  1147.     ''' The name of the computer to be shut down.
  1148.     ''' If the value of this parameter is 'NULL', the local computer is shut down.
  1149.     ''' This parameter can be an addres, for example: '127.0.0.1'
  1150.     ''' </param>
  1151.     ''' <param name="Message">
  1152.     ''' The message to be displayed in the interactive shutdown dialog box.
  1153.     ''' </param>
  1154.     ''' <param name="Timeout">
  1155.     ''' The number of seconds to wait before shutting down the computer.
  1156.     ''' If the value of this parameter is zero, the computer is shut down immediately.
  1157.     ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
  1158.     ''' If the value of this parameter is greater than zero, and the 'dwShutdownFlags' parameter
  1159.     ''' specifies the flag 'GRACE_OVERRIDE', the function fails and returns the error code 'ERROR_BAD_ARGUMENTS'.
  1160.     ''' </param>
  1161.     ''' <param name="Force">
  1162.     ''' Indicates whether to force the shutdown.
  1163.     ''' </param>
  1164.     ''' <param name="Reason">
  1165.     ''' The reason for initiating the shutdown.
  1166.     ''' If this parameter is zero,
  1167.     ''' the default is an undefined shutdown that is logged as "No title for this reason could be found".
  1168.     ''' By default, it is also an 'unplanned' shutdown.
  1169.     ''' </param>
  1170.     ''' <param name="Planning">
  1171.     ''' Indicates whether it's a planned or unplanned shutdoen operation.
  1172.     ''' </param>
  1173.     ''' <returns>
  1174.     ''' <c>true</c> if the shutdown operation is initiated correctlly, <c>false</c> otherwise.
  1175.     ''' </returns>
  1176.     Public Shared Function HybridShutdown(Optional ByVal Computer As String = Nothing,
  1177.                                           Optional ByVal Timeout As Integer = 0,
  1178.                                           Optional ByVal Message As String = Nothing,
  1179.                                           Optional ByVal Force As Enums.InitiateShutdown_Force =
  1180.                                                                   Enums.InitiateShutdown_Force.Wait,
  1181.                                           Optional ByVal Reason As Enums.ShutdownReason =
  1182.                                                                    Enums.ShutdownReason.MajorOther,
  1183.                                           Optional ByVal Planning As Enums.ShutdownPlanning =
  1184.                                                                      Enums.ShutdownPlanning.Unplanned) As Boolean
  1185.  
  1186.         GetLocalShutdownPrivileges(Computer)
  1187.         GetRemoteShutdownPrivileges(Computer)
  1188.  
  1189.         Select Case Timeout
  1190.  
  1191.             Case Is = 0
  1192.                 Return Not Convert.ToBoolean(
  1193.                     NativeMethods.InitiateShutdown(Computer, Message, Timeout,
  1194.                                                    Enums.InitiateShutdown_Flags.Shutdown Or
  1195.                                                    Enums.InitiateShutdown_Flags.HybridShutdown Or
  1196.                                                    Enums.InitiateShutdown_Flags.GraceOverride Or Force,
  1197.                                                    Reason Or Planning))
  1198.  
  1199.             Case Else
  1200.                 Return Not Convert.ToBoolean(
  1201.                     NativeMethods.InitiateShutdown(Computer, Message, Timeout,
  1202.                                                    Enums.InitiateShutdown_Flags.Shutdown Or
  1203.                                                    Enums.InitiateShutdown_Flags.HybridShutdown Or Force,
  1204.                                                    Reason Or Planning))
  1205.  
  1206.         End Select
  1207.  
  1208.     End Function
  1209.  
  1210. #End Region
  1211.  
  1212. End Class
  1213.  
  1214. #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement