Advertisement
Guest User

Untitled

a guest
Jun 27th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 77.81 KB | None | 0 0
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Modified : 28-June-2015
  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. '' Restart the current computer in 30 seconds and wait for applications to close.
  13. '' Specify that the restart operation is planned because a consecuence of an installation.
  14. 'Dim success As Boolean =
  15. '    SystemRestarter.Restart(Nothing, 30, "System is gonna be restarted quickly, go save all your data now...!",
  16. '                            SystemRestarter.ShutdownMode.Wait,
  17. '                            SystemRestarter.ShutdownReason.MajorOperatingSystem Or
  18. '                            SystemRestarter.ShutdownReason.MinorInstallation,
  19. '                            SystemRestarter.ShutdownPlanning.Planned)
  20. '
  21. 'Console.WriteLine(String.Format("Restart operation initiated successfully?: {0}", CStr(success)))
  22. '
  23. '' Abort the current operation.
  24. 'If success Then
  25. '    Dim isAborted As Boolean = SystemRestarter.Abort()
  26. '    Console.WriteLine(String.Format("Restart operation aborted   successfully?: {0}", CStr(isAborted)))
  27. 'Else
  28. '    Console.WriteLine("There is any restart operation to abort.")
  29. 'End If
  30. 'MsgBox("Close this dialog to continue the test. A shutdown will proceed...")
  31. '
  32. '' Shutdown the current computer instantlly and force applications to close.
  33. '' ( When timeout is '0' the operation can't be aborted )
  34. 'SystemRestarter.Shutdown(Nothing, 0, Nothing, SystemRestarter.ShutdownMode.ForceSelf)
  35. '
  36. '' LogOffs the current user.
  37. 'SystemRestarter.LogOff(SystemRestarter.LogOffMode.Wait)
  38.  
  39. #End Region
  40.  
  41. #Region " Option Statements "
  42.  
  43. Option Strict On
  44. Option Explicit On
  45. Option Infer Off
  46.  
  47. #End Region
  48.  
  49. #Region " Imports "
  50.  
  51. Imports System
  52. Imports System.Linq
  53. Imports System.Runtime.InteropServices
  54. Imports System.ComponentModel
  55.  
  56. #End Region
  57.  
  58. #Region " System Restarter "
  59.  
  60. ''' ----------------------------------------------------------------------------------------------------
  61. ''' <summary>
  62. ''' Shutdown, restart or logoff the local or a remote computer.
  63. ''' </summary>
  64. ''' ----------------------------------------------------------------------------------------------------
  65. Public NotInheritable Class SystemRestarter
  66.  
  67. #Region " P/Invoking "
  68.  
  69.     ''' ----------------------------------------------------------------------------------------------------
  70.     ''' <summary>
  71.     ''' Platform Invocation methods (P/Invoke), access unmanaged code.
  72.     ''' This class does not suppress stack walks for unmanaged code permission.
  73.     ''' <see cref="System.Security.SuppressUnmanagedCodeSecurityAttribute"/> must not be applied to this class.
  74.     ''' This class is for methods that can be used anywhere because a stack walk will be performed.
  75.     ''' </summary>
  76.     ''' ----------------------------------------------------------------------------------------------------
  77.     ''' <remarks>http://msdn.microsoft.com/en-us/library/ms182161.aspx</remarks>
  78.     ''' ----------------------------------------------------------------------------------------------------
  79.     Private NotInheritable Class NativeMethods
  80.  
  81. #Region " Functions "
  82.  
  83.         ''' ----------------------------------------------------------------------------------------------------
  84.         ''' <summary>
  85.         ''' Logs off the interactive user, shuts down the system, or shuts down and restarts the system.
  86.         ''' It sends the 'WM_QUERYENDSESSION' message to all applications to determine if they can be terminated.
  87.         ''' </summary>
  88.         ''' ----------------------------------------------------------------------------------------------------
  89.         ''' <param name="uFlags">
  90.         ''' Indicates the shutdown type.
  91.         ''' </param>
  92.         '''
  93.         ''' <param name="dwReason">
  94.         ''' Indicates the reason for initiating the shutdown.
  95.         ''' </param>
  96.         ''' ----------------------------------------------------------------------------------------------------
  97.         ''' <returns>
  98.         ''' If the function succeeds, the return value is <c>True</c>.
  99.         ''' The function executes asynchronously so a <c>True</c> return value indicates that the shutdown has been initiated.
  100.         ''' It does not indicate whether the shutdown will succeed.
  101.         ''' It is possible that the system, the user, or another application will abort the shutdown.
  102.         ''' If the function fails, the return value is <c>False</c>.
  103.         ''' </returns>
  104.         ''' ----------------------------------------------------------------------------------------------------
  105.         ''' <remarks>https://msdn.microsoft.com/en-us/library/windows/desktop/aa376868%28v=vs.85%29.aspx</remarks>
  106.         ''' ----------------------------------------------------------------------------------------------------
  107.         <DllImport("user32.dll", SetLastError:=True)>
  108.         Friend Shared Function ExitWindowsEx(
  109.                                ByVal uFlags As UInteger,
  110.                                ByVal dwReason As UInteger
  111.         ) As <MarshalAs(UnmanagedType.Bool)> Boolean
  112.         End Function
  113.  
  114.         ''' ----------------------------------------------------------------------------------------------------
  115.         ''' <summary>
  116.         ''' Initiates a shutdown and restart of the specified computer,
  117.         ''' and restarts any applications that have been registered for restart.
  118.         ''' </summary>
  119.         ''' ----------------------------------------------------------------------------------------------------
  120.         ''' <param name="lpMachineName">
  121.         ''' The name of the computer to be shut down.
  122.         ''' If the value of this parameter is <c>Nothing</c>, the local computer is shut down.
  123.         ''' This parameter can be an addres, for example: '127.0.0.1'
  124.         ''' </param>
  125.         '''
  126.         ''' <param name="lpMessage">
  127.         ''' The message to be displayed in the interactive shutdown dialog box.
  128.         ''' </param>
  129.         '''
  130.         ''' <param name="dwGracePeriod">
  131.         ''' The number of seconds to wait before shutting down the computer.
  132.         ''' If the value of this parameter is zero, the computer is shut down immediately.
  133.         ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
  134.         ''' If the value of this parameter is greater than zero, and the <paramref name="dwShutdownFlags"></paramref> parameter
  135.         ''' specifies the flag 'GRACE_OVERRIDE', the function fails and returns the error code 'ERROR_BAD_ARGUMENTS'.
  136.         ''' </param>
  137.         '''
  138.         ''' <param name="dwShutdownFlags">
  139.         ''' Specifies options for the shutdown.
  140.         ''' </param>
  141.         '''
  142.         ''' <param name="dwReason">
  143.         ''' The reason for initiating the shutdown.
  144.         ''' If this parameter is zero,
  145.         ''' the default is an undefined shutdown that is logged as "No title for this reason could be found".
  146.         ''' By default, it is also an 'unplanned' shutdown.
  147.         ''' </param>
  148.         ''' ----------------------------------------------------------------------------------------------------
  149.         ''' <returns>
  150.         ''' If the function succeeds, it returns ERROR_SUCCESS.
  151.         ''' </returns>
  152.         ''' ----------------------------------------------------------------------------------------------------
  153.         ''' <remarks>https://msdn.microsoft.com/en-us/library/windows/desktop/aa376872%28v=vs.85%29.aspx</remarks>
  154.         ''' ----------------------------------------------------------------------------------------------------
  155.         <DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)>
  156.         Friend Shared Function InitiateShutdown(
  157.                                ByVal lpMachineName As String,
  158.                                ByVal lpMessage As String,
  159.                                ByVal dwGracePeriod As Integer,
  160.                                ByVal dwShutdownFlags As UInteger,
  161.                                ByVal dwReason As UInteger
  162.         ) As UInteger
  163.         End Function
  164.  
  165.         ''' ----------------------------------------------------------------------------------------------------
  166.         ''' <summary>
  167.         ''' Aborts a system shutdown that has been initiated.
  168.         ''' </summary>
  169.         ''' ----------------------------------------------------------------------------------------------------
  170.         ''' <param name="lpMachineName">
  171.         ''' The network name of the computer where the shutdown is to be stopped.
  172.         ''' If this parameter is <c>Nothing</c> or an empty string, the function aborts the shutdown on the local computer.
  173.         ''' </param>
  174.         ''' ----------------------------------------------------------------------------------------------------
  175.         ''' <returns>
  176.         ''' <c>True</c> if the function succeeds, <c>False</c> otherwise.
  177.         ''' </returns>
  178.         ''' ----------------------------------------------------------------------------------------------------
  179.         ''' <remarks>https://msdn.microsoft.com/es-es/library/windows/desktop/aa376630%28v=vs.85%29.aspx</remarks>
  180.         ''' ----------------------------------------------------------------------------------------------------
  181.         <DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)>
  182.         Friend Shared Function AbortSystemShutdown(
  183.                       Optional ByVal lpMachineName As String = "127.0.0.1"
  184.         ) As <MarshalAs(UnmanagedType.Bool)> Boolean
  185.         End Function
  186.  
  187.         ''' ----------------------------------------------------------------------------------------------------
  188.         ''' <summary>
  189.         ''' Opens the access token associated with a process.
  190.         ''' </summary>
  191.         ''' ----------------------------------------------------------------------------------------------------
  192.         ''' <param name="ProcessHandle">
  193.         ''' An <see cref="IntPtr"></see> handle to the process whose access token is opened.
  194.         ''' The process must have the 'PROCESS_QUERY_INFORMATION' access permission.
  195.         ''' </param>
  196.         '''
  197.         ''' <param name="DesiredAccess">
  198.         ''' Specifies an access mask that specifies the requested types of access to the access token.
  199.         ''' These requested access types are compared with the discretionary access control list (DACL)
  200.         ''' of the token to determine which accesses are granted or denied.
  201.         ''' </param>
  202.         '''
  203.         ''' <param name="TokenHandle">
  204.         ''' Am <see cref="IntPtr"></see> handle that identifies the newly opened access token when the function returns.
  205.         ''' </param>
  206.         ''' ----------------------------------------------------------------------------------------------------
  207.         ''' <returns>If the function succeeds, the return value is nonzero.
  208.         ''' If the function fails, the return value is zero.
  209.         ''' To get extended error information, call <see cref="Marshal.GetLastWin32Error"></see>.
  210.         ''' </returns>
  211.         ''' ----------------------------------------------------------------------------------------------------
  212.         ''' <remarks>https://msdn.microsoft.com/en-us/library/windows/desktop/aa379295%28v=vs.85%29.aspx</remarks>
  213.         ''' ----------------------------------------------------------------------------------------------------
  214.         <DllImport("advapi32.dll", SetLastError:=True)>
  215.         Friend Shared Function OpenProcessToken(
  216.                                ByVal processHandle As IntPtr,
  217.                                ByVal desiredAccess As SystemRestarter.NativeMethods.AccessRights,
  218.                                ByRef tokenHandle As IntPtr
  219.         ) As Integer
  220.         End Function
  221.  
  222.         ''' ----------------------------------------------------------------------------------------------------
  223.         ''' <summary>
  224.         ''' Enables or disables privileges in the specified access token.
  225.         ''' Enabling or disabling privileges in an access token requires 'TOKEN_ADJUST_PRIVILEGES' access.
  226.         ''' </summary>
  227.         ''' ----------------------------------------------------------------------------------------------------
  228.         ''' <param name="tokenHandle">
  229.         ''' An <see cref="IntPtr"></see> pointer handle to the access token that contains the privileges to be modified.
  230.         ''' The handle must have 'TOKEN_ADJUST_PRIVILEGES' access to the token.
  231.         ''' If the <paramref name="previousState"></paramref> parameter is not <see cref="IntPtr.Zero"></see>, the handle must also have 'TOKEN_QUERY' access.
  232.         ''' </param>
  233.         '''
  234.         ''' <param name="disableAllPrivileges">
  235.         ''' Specifies whether the function disables all of the token's privileges.
  236.         ''' If this value is <c>True</c>, the function disables all privileges and ignores the <paramref name="newState"></paramref> parameter.
  237.         ''' If it is <c>False</c>, the function modifies privileges based on the information pointed to by the <paramref name="newState"></paramref> parameter.
  238.         ''' </param>
  239.         '''
  240.         ''' <param name="newState">
  241.         ''' A <see cref="IntPtr"></see> pointer to a <see cref="SystemRestarter.NativeMethods.TokenPrivileges"></see> structure that specifies an
  242.         ''' array of privileges and their attributes.
  243.         ''' If the <paramref name="disableAllPrivileges"></paramref> parameter is <c>False</c>,
  244.         ''' the <paramref name="adjustTokenPrivileges"></paramref> function enables, disables, or removes these privileges for the token.
  245.         ''' </param>
  246.         '''
  247.         ''' <param name="bufferLength">
  248.         ''' Specifies the size, in bytes, of the buffer pointed to by the <paramref name="previousState"></paramref> parameter.
  249.         ''' This parameter can be zero if the <paramref name="previousState"></paramref> parameter is <see cref="IntPtr.Zero"></see>.
  250.         ''' </param>
  251.         '''
  252.         ''' <param name="previousState">
  253.         ''' A <see cref="IntPtr"></see> pointer to a buffer that the function fills with a <see cref="SystemRestarter.NativeMethods.TokenPrivileges"></see> structure
  254.         ''' that contains the previous state of any privileges that the function modifies.
  255.         ''' That is, if a privilege has been modified by this function,
  256.         ''' the privilege and its previous state are contained in the <see cref="SystemRestarter.NativeMethods.TokenPrivileges"></see> structure
  257.         ''' referenced by <paramref name="previousState"></paramref>.
  258.         ''' If the <paramref name="privilegeCount"></paramref> member of <see cref="SystemRestarter.NativeMethods.TokenPrivileges"></see> is zero,
  259.         ''' then no privileges have been changed by this function.
  260.         ''' This parameter can be <see cref="IntPtr.Zero"></see>.
  261.         ''' </param>
  262.         '''
  263.         ''' <param name="returnLength">
  264.         ''' A <see cref="IntPtr"></see> pointer to a variable that receives the required size, in bytes,
  265.         ''' of the buffer pointed to by the <paramref name="previousState"></paramref> parameter.
  266.         ''' This parameter can be <see cref="IntPtr.Zero"></see> if <paramref name="previousState"></paramref> is <see cref="IntPtr.Zero"></see>.
  267.         ''' </param>
  268.         ''' ----------------------------------------------------------------------------------------------------
  269.         ''' <returns>
  270.         ''' If the function succeeds, the return value is <c>True</c>, otherwise, <c>False</c>.
  271.         ''' To determine whether the function adjusted all of the specified privileges, call <see cref="Marshal.GetLastWin32Error"></see>.
  272.         ''' </returns>
  273.         ''' ----------------------------------------------------------------------------------------------------
  274.         ''' <remarks>https://msdn.microsoft.com/es-es/library/windows/desktop/aa375202%28v=vs.85%29.aspx</remarks>
  275.         ''' ----------------------------------------------------------------------------------------------------
  276.         <DllImport("advapi32.dll", SetLastError:=True)>
  277.         Friend Shared Function AdjustTokenPrivileges(
  278.                                ByVal tokenHandle As IntPtr,
  279.                                <MarshalAs(UnmanagedType.Bool)> ByVal disableAllPrivileges As Boolean,
  280.                                ByRef newState As SystemRestarter.NativeMethods.TokenPrivileges,
  281.                                ByVal bufferLength As UInteger,
  282.                                ByVal previousState As IntPtr,
  283.                                ByVal returnLength As IntPtr
  284.         ) As <MarshalAs(UnmanagedType.Bool)> Boolean
  285.         End Function
  286.  
  287.         ''' ----------------------------------------------------------------------------------------------------
  288.         ''' <summary>
  289.         ''' Retrieves the locally unique identifier (LUID) used on a specified system,
  290.         ''' to locally represent the specified privilege name.
  291.         ''' </summary>
  292.         ''' ----------------------------------------------------------------------------------------------------
  293.         ''' <param name="lpSystemName">
  294.         ''' A pointer to a null-terminated string that specifies the name of the system
  295.         ''' on which the privilege name is retrieved.
  296.         ''' If a null string is specified, the function attempts to find the privilege name on the local system
  297.         ''' </param>
  298.         '''
  299.         ''' <param name="lpName">
  300.         ''' A pointer to a null-terminated string that specifies the name of the privilege,
  301.         ''' as defined in the Winnt.h header file.
  302.         ''' For example, this parameter could specify the constant, 'SE_SECURITY_NAME',
  303.         ''' or its corresponding string, "SeSecurityPrivilege".
  304.         ''' </param>
  305.         '''
  306.         ''' <param name="lpLuid">
  307.         ''' A pointer to a variable that receives the LUID by which the privilege is known on
  308.         ''' the system specified by the <paramref name="lpSystemName"></paramref> parameter.
  309.         ''' </param>
  310.         ''' ----------------------------------------------------------------------------------------------------
  311.         ''' <returns>
  312.         ''' If the function succeeds, the function returns nonzero.
  313.         ''' If the function fails, it returns zero.
  314.         ''' To get extended error information, call <see cref="Marshal.GetLastWin32Error"></see>.
  315.         ''' </returns>
  316.         ''' ----------------------------------------------------------------------------------------------------
  317.         ''' <remarks>https://msdn.microsoft.com/en-us/library/windows/desktop/aa379180%28v=vs.85%29.aspx</remarks>
  318.         ''' ----------------------------------------------------------------------------------------------------
  319.         <DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)>
  320.         Friend Shared Function LookupPrivilegeValue(
  321.                                ByVal lpSystemName As String,
  322.                                ByVal lpName As String,
  323.                                ByRef lpLuid As SystemRestarter.NativeMethods.Luid
  324.         ) As Integer
  325.         End Function
  326.  
  327. #End Region
  328.  
  329. #Region " Enumerations "
  330.  
  331.         ''' ----------------------------------------------------------------------------------------------------
  332.         ''' <summary>
  333.         ''' Flags for <paramref name="uFlags"/> parameter of <see cref="SystemRestarter.NativeMethods.ExitWindowsEx"/> function.
  334.         ''' </summary>
  335.         ''' ----------------------------------------------------------------------------------------------------
  336.         ''' <remarks>https://msdn.microsoft.com/en-us/library/windows/desktop/aa376868%28v=vs.85%29.aspx</remarks>
  337.         ''' ----------------------------------------------------------------------------------------------------
  338.         <Flags>
  339.         Friend Enum ExitwindowsExFlags As UInteger
  340.  
  341.             ''' <summary>
  342.             ''' Shuts down all processes running in the logon session of the current process.
  343.             ''' Then it logs the user off.
  344.             ''' This flag can be used only by processes running in an interactive user's logon session.
  345.             ''' </summary>
  346.             LogOff = &H0UI
  347.  
  348.             ' ''' <summary>
  349.             ' ''' Shuts down the system to a point at which it is safe to turn off the power.
  350.             ' ''' All file buffers have been flushed to disk, and all running processes have stopped.
  351.             ' ''' The calling process must have the SE_SHUTDOWN_NAME privilege.
  352.             ' ''' Specifying this flag will not turn off the power even if the system supports the power-off feature,
  353.             ' ''' You must use <see cref="SystemRestarter.NativeMethods.ExitWindowsExFlags.PowerOff"/> to do this.
  354.             ' ''' </summary>
  355.             ' ShutDown = &H1UI
  356.  
  357.             ' ''' <summary>
  358.             ' ''' ( Beginning with Windows 8 )
  359.             ' ''' You can prepare the system for a faster startup by combining the
  360.             ' ''' <see cref="SystemRestarter.NativeMethods.ExitWindowsExFlags.HybridShutdown"/> flag with the
  361.             ' ''' <see cref="SystemRestarter.NativeMethods.ExitWindowsExFlags.Shutdown"/> flag.
  362.             ' ''' </summary>
  363.             ' HybridShutdown = &H400000UI
  364.  
  365.             ' ''' <summary>
  366.             ' ''' Shuts down the system and then restarts the system.
  367.             ' ''' The calling process must have the SE_SHUTDOWN_NAME privilege
  368.             ' ''' </summary>
  369.             ' Reboot = &H2UI
  370.  
  371.             ' ''' <summary>
  372.             ' ''' Shuts down the system and turns off the power.
  373.             ' ''' The system must support the power-off feature.
  374.             ' ''' The calling process must have the SE_SHUTDOWN_NAME privilege.
  375.             ' ''' </summary>
  376.             ' PowerOff = &H8UI
  377.  
  378.             ' ''' <summary>
  379.             ' ''' Shuts down the system and then restarts it,
  380.             ' ''' as well as any applications that have been registered for restart using the RegisterApplicationRestart function.
  381.             ' ''' These application receive the WM_QUERYENDSESSION message with lParam set to the ENDSESSION_CLOSEAPP value.
  382.             ' ''' For more information, see Guidelines for Applications: https://msdn.microsoft.com/en-us/library/windows/desktop/aa373651%28v=vs.85%29.aspx
  383.             ' ''' </summary>
  384.             ' RestartApps = &H40UI
  385.  
  386.             ''' <summary>
  387.             ''' This flag has no effect if terminal services is enabled.
  388.             ''' Otherwise, the system does not send the WM_QUERYENDSESSION message.
  389.             ''' This can cause applications to lose data.
  390.             ''' Therefore, you should only use this flag in an emergency.
  391.             ''' </summary>
  392.             Force = &H4UI
  393.  
  394.             ''' <summary>
  395.             ''' Forces processes to terminate if they do not respond to the WM_QUERYENDSESSION or WM_ENDSESSION message within the timeout interval.
  396.             ''' </summary>
  397.             ForceIfHung = &H10UI
  398.  
  399.         End Enum
  400.  
  401.         ''' ----------------------------------------------------------------------------------------------------
  402.         ''' <summary>
  403.         ''' Flags for <paramref name="dwShutdownFlags"/> parameter of <see cref="SystemRestarter.NativeMethods.InitiateShutdown"/> function.
  404.         ''' </summary>
  405.         ''' ----------------------------------------------------------------------------------------------------
  406.         ''' <remarks>https://msdn.microsoft.com/en-us/library/windows/desktop/aa376872%28v=vs.85%29.aspx</remarks>
  407.         ''' ----------------------------------------------------------------------------------------------------
  408.         <Flags>
  409.         Friend Enum InitiateShutdownFlags As UInteger
  410.  
  411.             ''' <summary>
  412.             ''' Overrides the grace period so that the computer is shut down immediately.
  413.             ''' </summary>
  414.             GraceOverride = &H20UI
  415.  
  416.             ' ''' <summary>
  417.             ' ''' The computer installs any updates before starting the shutdown.
  418.             ' ''' </summary>
  419.             ' InstallUpdates = &H40UI
  420.  
  421.             ''' <summary>
  422.             ''' The computer is shut down but is not powered down or restarted.
  423.             ''' </summary>
  424.             Shutdown = &H10UI
  425.  
  426.             ''' <summary>
  427.             ''' ( Beginning with Windows 8 )
  428.             ''' Prepares the system for a faster startup by combining
  429.             ''' the <see cref="SystemRestarter.NativeMethods.InitiateShutdownFlags.HybridShutdown"/> flag with the
  430.             ''' <see cref="SystemRestarter.NativeMethods.InitiateShutdownFlags.Shutdown"/> flag.
  431.             ''' <see cref="SystemRestarter.NativeMethods.InitiateShutdown"/> always initiate a full system shutdown if the
  432.             ''' <see cref="SystemRestarter.NativeMethods.InitiateShutdownFlags.HybridShutdown"/> flag is not set.
  433.             ''' </summary>
  434.             HybridShutdown = &H200UI
  435.  
  436.             ''' <summary>
  437.             ''' The computer is shut down and powered down.
  438.             ''' </summary>
  439.             PowerOff = &H8UI
  440.  
  441.             ''' <summary>
  442.             ''' The computer is shut down and restarted.
  443.             ''' </summary>
  444.             Restart = &H4UI
  445.  
  446.             ''' <summary>
  447.             ''' The system is restarted using the <see cref="SystemRestarter.NativeMethods.ExitWindowsEx"/> function with the
  448.             ''' <see cref="SystemRestarter.NativeMethods.InitiateShutdownFlags.RestartApps"/> flag.
  449.             ''' This restarts any applications that have been registered for restart
  450.             ''' using the 'RegisterApplicationRestart' function.
  451.             ''' </summary>
  452.             RestartApps = &H80UI
  453.  
  454.             ''' <summary>
  455.             ''' Don't force the system to close the applications.
  456.             ''' This is the default parameter.
  457.             ''' </summary>
  458.             Wait = &H0UI
  459.  
  460.             ''' <summary>
  461.             ''' All sessions are forcefully logged off.
  462.             ''' If this flag is not set and users other than the current user are logged on to the computer
  463.             ''' specified by the <paramref name="lpMachineName"/> parameter.
  464.             ''' </summary>
  465.             ForceOthers = &H1UI
  466.  
  467.             ''' <summary>
  468.             ''' Specifies that the originating session is logged off forcefully.
  469.             ''' If this flag is not set, the originating session is shut down interactively,
  470.             ''' so a shutdown is not guaranteed even if the function returns successfully.
  471.             ''' </summary>
  472.             ForceSelf = &H2UI
  473.  
  474.         End Enum
  475.  
  476.         ''' ----------------------------------------------------------------------------------------------------
  477.         ''' <summary>
  478.         ''' Flags for <paramref name="dwReason"/> parameter of <see cref="SystemRestarter.NativeMethods.ExitWindowsEx"/> function.
  479.         ''' </summary>
  480.         ''' ----------------------------------------------------------------------------------------------------
  481.         ''' <remarks>http://msdn.microsoft.com/en-us/library/windows/desktop/aa376885%28v=vs.85%29.aspx</remarks>
  482.         ''' ----------------------------------------------------------------------------------------------------
  483.         <Flags>
  484.         Friend Enum ShutdownReason As UInteger
  485.  
  486.             ''' <summary>
  487.             ''' Application issue.
  488.             ''' </summary>
  489.             MajorApplication = &H40000
  490.  
  491.             ''' <summary>
  492.             ''' Hardware issue.
  493.             ''' </summary>
  494.             MajorHardware = &H10000
  495.  
  496.             ' ''' <summary>
  497.             ' ''' The <see cref="SystemRestarter.NativeMethods.InitiateShutdown"/> function was used instead of 'InitiateSystemShutdownEx' function.
  498.             ' ''' </summary>
  499.             ' MajorLegacyApi = &H70000
  500.  
  501.             ''' <summary>
  502.             ''' Operating system issue.
  503.             ''' </summary>
  504.             MajorOperatingSystem = &H20000
  505.  
  506.             ''' <summary>
  507.             ''' Other issue.
  508.             ''' </summary>
  509.             MajorOther = &H0
  510.  
  511.             ''' <summary>
  512.             ''' Power failure.
  513.             ''' </summary>
  514.             MajorPower = &H60000
  515.  
  516.             ''' <summary>
  517.             ''' Software issue.
  518.             ''' </summary>
  519.             MajorSoftware = &H30000
  520.  
  521.             ''' <summary>
  522.             ''' System failure..
  523.             ''' </summary>
  524.             MajorSystem = &H50000
  525.  
  526.             ''' <summary>
  527.             ''' Blue screen crash event.
  528.             ''' </summary>
  529.             MinorBlueScreen = &HF
  530.  
  531.             ''' <summary>
  532.             ''' Unplugged.
  533.             ''' </summary>
  534.             MinorCordUnplugged = &HB
  535.  
  536.             ''' <summary>
  537.             ''' Disk.
  538.             ''' </summary>
  539.             MinorDisk = &H7
  540.  
  541.             ''' <summary>
  542.             ''' Environment.
  543.             ''' </summary>
  544.             MinorEnvironment = &HC
  545.  
  546.             ''' <summary>
  547.             ''' Driver.
  548.             ''' </summary>
  549.             MinorHardwareDriver = &HD
  550.  
  551.             ''' <summary>
  552.             ''' Hot fix.
  553.             ''' </summary>
  554.             MinorHotfix = &H11
  555.  
  556.             ''' <summary>
  557.             ''' Hot fix uninstallation.
  558.             ''' </summary>
  559.             MinorHotfixUninstall = &H17
  560.  
  561.             ''' <summary>
  562.             ''' Unresponsive.
  563.             ''' </summary>
  564.             MinorHung = &H5
  565.  
  566.             ''' <summary>
  567.             ''' Installation.
  568.             ''' </summary>
  569.             MinorInstallation = &H2
  570.  
  571.             ''' <summary>
  572.             ''' Maintenance.
  573.             ''' </summary>
  574.             MinorMaintenance = &H1
  575.  
  576.             ''' <summary>
  577.             ''' MMC issue.
  578.             ''' </summary>
  579.             MinorMmc = &H19
  580.  
  581.             ''' <summary>
  582.             ''' Network connectivity.
  583.             ''' </summary>
  584.             MinorNetworkConnectivity = &H14
  585.  
  586.             ''' <summary>
  587.             ''' Network card.
  588.             ''' </summary>
  589.             MinorNetworkCard = &H9
  590.  
  591.             ''' <summary>
  592.             ''' Other issue.
  593.             ''' </summary>
  594.             MinorOther = &H0
  595.  
  596.             ''' <summary>
  597.             ''' Other driver event.
  598.             ''' </summary>
  599.             MinorOtherDriver = &HE
  600.  
  601.             ''' <summary>
  602.             ''' Power supply.
  603.             ''' </summary>
  604.             MinorPowerSupply = &HA
  605.  
  606.             ''' <summary>
  607.             ''' Processor.
  608.             ''' </summary>
  609.             MinorProcessor = &H8
  610.  
  611.             ''' <summary>
  612.             ''' Reconfigure.
  613.             ''' </summary>
  614.             MinorReconfig = &H4
  615.  
  616.             ''' <summary>
  617.             ''' Security issue.
  618.             ''' </summary>
  619.             MinorSecurity = &H13
  620.  
  621.             ''' <summary>
  622.             ''' Security patch.
  623.             ''' </summary>
  624.             MinorSecurityFix = &H12
  625.  
  626.             ''' <summary>
  627.             ''' Security patch uninstallation.
  628.             ''' </summary>
  629.             MinorSecurityFixUninstall = &H18
  630.  
  631.             ''' <summary>
  632.             ''' Service pack.
  633.             ''' </summary>
  634.             MinorServicePack = &H10
  635.  
  636.             ''' <summary>
  637.             ''' Service pack uninstallation.
  638.             ''' </summary>
  639.             MinorServicePackUninstall = &H16
  640.  
  641.             ''' <summary>
  642.             ''' Terminal Services.
  643.             ''' </summary>
  644.             MinorTermSrv = &H20
  645.  
  646.             ''' <summary>
  647.             ''' Unstable.
  648.             ''' </summary>
  649.             MinorUnstable = &H6
  650.  
  651.             ''' <summary>
  652.             ''' Upgrade.
  653.             ''' </summary>
  654.             MinorUpgrade = &H3
  655.  
  656.             ''' <summary>
  657.             ''' WMI issue.
  658.             ''' </summary>
  659.             MinorWmi = &H15
  660.  
  661.         End Enum
  662.  
  663.         ''' ----------------------------------------------------------------------------------------------------
  664.         ''' <summary>
  665.         ''' Flags combination for <paramref name="dwReason"/> parameter of <see cref="SystemRestarter.NativeMethods.ExitWindowsEx"/>
  666.         ''' and <see cref="SystemRestarter.NativeMethods.InitiateShutdown"/> functions.
  667.         ''' </summary>
  668.         ''' ----------------------------------------------------------------------------------------------------
  669.         ''' <remarks>http://msdn.microsoft.com/en-us/library/windows/desktop/aa376885%28v=vs.85%29.aspx</remarks>
  670.         ''' ----------------------------------------------------------------------------------------------------
  671.         Friend Enum ShutdownPlanning As UInteger
  672.  
  673.             ''' <summary>
  674.             ''' The shutdown was unplanned.
  675.             ''' This is the default parameter.
  676.             ''' </summary>
  677.             Unplanned = &H0UI
  678.  
  679.             ''' <summary>
  680.             ''' The reason code is defined by the user.
  681.             ''' For more information, see Defining a Custom Reason Code.
  682.             ''' If this flag is not present, the reason code is defined by the system.
  683.             ''' </summary>
  684.             UserDefined = &H40000000UI
  685.  
  686.             ''' <summary>
  687.             ''' The shutdown was planned.
  688.             ''' The system generates a System State Data (SSD) file.
  689.             ''' This file contains system state information such as the processes, threads, memory usage, and configuration.
  690.             ''' If this flag is not present, the shutdown was unplanned.
  691.             ''' </summary>
  692.             Planned = &H80000000UI
  693.  
  694.         End Enum
  695.  
  696.         ''' ----------------------------------------------------------------------------------------------------
  697.         ''' <summary>
  698.         ''' Flags combination for <paramref name="privileges"/> parameter of <see cref="SystemRestarter.NativeMethods.TokenPrivileges"/> structure.
  699.         ''' </summary>
  700.         ''' ----------------------------------------------------------------------------------------------------
  701.         ''' <remarks>http://msdn.microsoft.com/en-us/library/windows/desktop/aa376885%28v=vs.85%29.aspx</remarks>
  702.         ''' ----------------------------------------------------------------------------------------------------
  703.         <Flags>
  704.         Friend Enum TokenPrivilegesFlags As UInteger
  705.  
  706.             '''' <summary>
  707.             '''' The privilege is enabled by default.
  708.             '''' </summary>
  709.             PrivilegeEnabledBYDefault = &H1UI
  710.  
  711.             ''' <summary>
  712.             ''' The privilege is enabled.
  713.             ''' </summary>
  714.             PrivilegeEnabled = &H2UI
  715.  
  716.             ''' <summary>
  717.             ''' Used to remove a privilege.
  718.             ''' </summary>
  719.             PrivilegeRemoved = &H4UI
  720.  
  721.             ''' <summary>
  722.             ''' The privilege was used to gain access to an object or service.
  723.             ''' This flag is used to identify the relevant privileges
  724.             ''' in a set passed by a client application that may contain unnecessary privileges
  725.             ''' </summary>
  726.             PrivilegeUsedForAccess = &H80000000UI
  727.  
  728.         End Enum
  729.  
  730.         ''' ----------------------------------------------------------------------------------------------------
  731.         ''' <summary>
  732.         ''' Flags combination for <paramref name="desiredAccess"/> parameter of <see cref="SystemRestarter.NativeMethods.OpenProcessToken"/> function.
  733.         ''' </summary>
  734.         ''' ----------------------------------------------------------------------------------------------------
  735.         ''' <remarks>http://msdn.microsoft.com/en-us/library/windows/desktop/aa374905%28v=vs.85%29.aspx</remarks>
  736.         ''' ----------------------------------------------------------------------------------------------------
  737.         <Flags>
  738.         Friend Enum AccessRights As UInteger
  739.  
  740.             ' *****************************************************************************
  741.             '                            WARNING!, NEED TO KNOW...
  742.             '
  743.             '  THIS ENUMERATION IS PARTIALLY DEFINED JUST FOR THE PURPOSES OF THIS PROJECT
  744.             ' *****************************************************************************
  745.  
  746.             ''' <summary>
  747.             ''' Required to enable or disable the privileges in an access token.
  748.             ''' </summary>
  749.             TokenAdjustPrivileges = &H32UI
  750.  
  751.             ''' <summary>
  752.             ''' Required to query an access token.
  753.             ''' </summary>
  754.             TokenQuery = &H8UI
  755.  
  756.         End Enum
  757.  
  758. #End Region
  759.  
  760. #Region " Structures "
  761.  
  762.         ''' <summary>
  763.         ''' An 'LUID' is a 64-bit value guaranteed to be unique only on the system on which it was generated.
  764.         ''' The uniqueness of a locally unique identifier (LUID) is guaranteed only until the system is restarted.
  765.         ''' </summary>
  766.         Friend Structure Luid
  767.  
  768.             ''' <summary>
  769.             ''' The Low-order bits.
  770.             ''' </summary>
  771.             Public LowPart As Integer
  772.  
  773.             ''' <summary>
  774.             ''' The High-order bits.
  775.             ''' </summary>
  776.             Public HighPart As Integer
  777.  
  778.         End Structure
  779.  
  780.         ''' <summary>
  781.         ''' Represents a locally unique identifier (LUID) and its attributes.
  782.         ''' </summary>
  783.         Friend Structure LuIdAndAttributes
  784.  
  785.             ''' <summary>
  786.             ''' Specifies a 'LUID' value.
  787.             ''' </summary>
  788.             Public pLuid As SystemRestarter.NativeMethods.Luid
  789.  
  790.             ''' <summary>
  791.             ''' Specifies attributes of the 'LUID'.
  792.             ''' This value contains up to 32 one-bit flags.
  793.             ''' Its meaning is dependent on the definition and use of the 'LUID'.
  794.             ''' </summary>
  795.             Public Attributes As SystemRestarter.NativeMethods.TokenPrivilegesFlags
  796.  
  797.         End Structure
  798.  
  799.         ''' <summary>
  800.         ''' Contains information about a set of privileges for an access token.
  801.         ''' </summary>
  802.         Friend Structure TokenPrivileges
  803.  
  804.             ''' <summary>
  805.             ''' This must be set to the number of entries in the Privileges array
  806.             ''' </summary>
  807.             Public PrivilegeCount As Integer
  808.  
  809.             ''' <summary>
  810.             ''' Specifies an array of 'LUID_AND_ATTRIBUTES' structures.
  811.             ''' Each structure contains the 'LUID' and attributes of a privilege.
  812.             ''' To get the name of the privilege associated with a 'LUID', call the 'LookupPrivilegeName' function,
  813.             ''' passing the address of the 'LUID' as the value of the 'lpLuid' parameter.
  814.             ''' </summary>
  815.             Public Privileges As SystemRestarter.NativeMethods.LuIdAndAttributes
  816.  
  817.         End Structure
  818.  
  819. #End Region
  820.  
  821.     End Class
  822.  
  823. #End Region
  824.  
  825. #Region " Read-Only Variable Members "
  826.  
  827.     ''' ----------------------------------------------------------------------------------------------------
  828.     ''' <summary>
  829.     ''' SE_REMOTE_SHUTDOWN
  830.     '''
  831.     ''' privilege required to shut down a local system.
  832.     ''' User Right: Shut down the system.
  833.     ''' </summary>
  834.     ''' ----------------------------------------------------------------------------------------------------
  835.     ''' <remarks>http://msdn.microsoft.com/en-us/library/windows/desktop/bb530716%28v=vs.85%29.aspx</remarks>
  836.     ''' ----------------------------------------------------------------------------------------------------
  837.     Private Shared ReadOnly privilegeNameOfShutdown As String = "SeShutdownPrivilege"
  838.  
  839.     ''' ----------------------------------------------------------------------------------------------------
  840.     ''' <summary>
  841.     ''' SE_REMOTE_SHUTDOWN
  842.     '''
  843.     ''' privilege required to shut down a local system.
  844.     ''' User Right: Shut down the system.
  845.     ''' </summary>
  846.     ''' ----------------------------------------------------------------------------------------------------
  847.     ''' <remarks>http://msdn.microsoft.com/en-us/library/windows/desktop/bb530716%28v=vs.85%29.aspx</remarks>
  848.     ''' ----------------------------------------------------------------------------------------------------
  849.     Private Shared ReadOnly privilegeNameOfRemoteShutdown As String = "SeRemoteShutdownPrivilege"
  850.  
  851.  
  852. #End Region
  853.  
  854. #Region " Enumerations "
  855.  
  856.     ''' <summary>
  857.     ''' Specifies the mode to logoff an user session.
  858.     ''' </summary>
  859.     Public Enum LogOffMode As UInteger
  860.  
  861.         ''' <summary>
  862.         ''' Don't force the system to close the applications.
  863.         ''' </summary>
  864.         Wait = 0UI
  865.  
  866.         ''' <summary>
  867.         ''' This flag has no effect if terminal services is enabled.
  868.         ''' Otherwise, the system does not send the WM_QUERYENDSESSION message.
  869.         ''' This can cause applications to lose data.
  870.         ''' Therefore, you should only use this flag in an emergency.
  871.         ''' </summary>
  872.         Force = SystemRestarter.NativeMethods.ExitwindowsExFlags.Force
  873.  
  874.         ''' <summary>
  875.         ''' Forces processes to terminate if they do not respond to the WM_QUERYENDSESSION or WM_ENDSESSION message within the timeout interval.
  876.         ''' </summary>
  877.         ForceIfHung = SystemRestarter.NativeMethods.ExitwindowsExFlags.ForceIfHung
  878.  
  879.     End Enum
  880.  
  881.     ''' <summary>
  882.     ''' Specifies the mode to shutdown/restart the system.
  883.     ''' </summary>
  884.     Public Enum ShutdownMode As UInteger
  885.  
  886.         ''' <summary>
  887.         ''' Don't force the system to close the applications.
  888.         ''' </summary>
  889.         Wait = SystemRestarter.NativeMethods.InitiateShutdownFlags.Wait
  890.  
  891.         ''' <summary>
  892.         ''' All sessions are forcefully logged off.
  893.         ''' </summary>
  894.         ForceOthers = SystemRestarter.NativeMethods.InitiateShutdownFlags.ForceOthers
  895.  
  896.         ''' <summary>
  897.         ''' Specifies that the originating session is logged off forcefully.
  898.         ''' If this flag is not set, the originating session is shut down interactively,
  899.         ''' so a shutdown is not guaranteed.
  900.         ''' </summary>
  901.         ForceSelf = SystemRestarter.NativeMethods.InitiateShutdownFlags.ForceSelf
  902.  
  903.     End Enum
  904.  
  905.     ''' <summary>
  906.     ''' Specifies a shutdown/restart reason.
  907.     ''' </summary>
  908.     <Flags>
  909.     Public Enum ShutdownReason As UInteger
  910.  
  911.         ''' <summary>
  912.         ''' Application issue.
  913.         ''' </summary>
  914.         MajorApplication = SystemRestarter.NativeMethods.ShutdownReason.MajorApplication
  915.  
  916.         ''' <summary>
  917.         ''' Hardware issue.
  918.         ''' </summary>
  919.         MajorHardware = SystemRestarter.NativeMethods.ShutdownReason.MajorHardware
  920.  
  921.         ''' <summary>
  922.         ''' Operating system issue.
  923.         ''' </summary>
  924.         MajorOperatingSystem = SystemRestarter.NativeMethods.ShutdownReason.MajorOperatingSystem
  925.  
  926.         ''' <summary>
  927.         ''' Other issue.
  928.         ''' </summary>
  929.         MajorOther = SystemRestarter.NativeMethods.ShutdownReason.MajorOther
  930.  
  931.         ''' <summary>
  932.         ''' Power failure.
  933.         ''' </summary>
  934.         MajorPower = SystemRestarter.NativeMethods.ShutdownReason.MajorPower
  935.  
  936.         ''' <summary>
  937.         ''' Software issue.
  938.         ''' </summary>
  939.         MajorSoftware = SystemRestarter.NativeMethods.ShutdownReason.MajorSoftware
  940.  
  941.         ''' <summary>
  942.         ''' System failure..
  943.         ''' </summary>
  944.         MajorSystem = SystemRestarter.NativeMethods.ShutdownReason.MajorSystem
  945.  
  946.         ''' <summary>
  947.         ''' Blue screen crash event.
  948.         ''' </summary>
  949.         MinorBlueScreen = SystemRestarter.NativeMethods.ShutdownReason.MinorBlueScreen
  950.  
  951.         ''' <summary>
  952.         ''' Unplugged.
  953.         ''' </summary>
  954.         MinorCordUnplugged = SystemRestarter.NativeMethods.ShutdownReason.MinorCordUnplugged
  955.  
  956.         ''' <summary>
  957.         ''' Disk.
  958.         ''' </summary>
  959.         MinorDisk = SystemRestarter.NativeMethods.ShutdownReason.MinorDisk
  960.  
  961.         ''' <summary>
  962.         ''' Environment.
  963.         ''' </summary>
  964.         MinorEnvironment = SystemRestarter.NativeMethods.ShutdownReason.MinorEnvironment
  965.  
  966.         ''' <summary>
  967.         ''' Driver.
  968.         ''' </summary>
  969.         MinorHardwareDriver = SystemRestarter.NativeMethods.ShutdownReason.MinorHardwareDriver
  970.  
  971.         ''' <summary>
  972.         ''' Hot fix.
  973.         ''' </summary>
  974.         MinorHotfix = SystemRestarter.NativeMethods.ShutdownReason.MinorHotfix
  975.  
  976.         ''' <summary>
  977.         ''' Hot fix uninstallation.
  978.         ''' </summary>
  979.         MinorHotfixUninstall = SystemRestarter.NativeMethods.ShutdownReason.MinorHotfixUninstall
  980.  
  981.         ''' <summary>
  982.         ''' Unresponsive.
  983.         ''' </summary>
  984.         MinorHung = SystemRestarter.NativeMethods.ShutdownReason.MinorHung
  985.  
  986.         ''' <summary>
  987.         ''' Installation.
  988.         ''' </summary>
  989.         MinorInstallation = SystemRestarter.NativeMethods.ShutdownReason.MinorInstallation
  990.  
  991.         ''' <summary>
  992.         ''' Maintenance.
  993.         ''' </summary>
  994.         MinorMaintenance = SystemRestarter.NativeMethods.ShutdownReason.MinorMaintenance
  995.  
  996.         ''' <summary>
  997.         ''' MMC issue.
  998.         ''' </summary>
  999.         MinorMmc = SystemRestarter.NativeMethods.ShutdownReason.MinorMmc
  1000.  
  1001.         ''' <summary>
  1002.         ''' Network connectivity.
  1003.         ''' </summary>
  1004.         MinorNetworkConnectivity = SystemRestarter.NativeMethods.ShutdownReason.MinorNetworkConnectivity
  1005.  
  1006.         ''' <summary>
  1007.         ''' Network card.
  1008.         ''' </summary>
  1009.         MinorNetworkCard = SystemRestarter.NativeMethods.ShutdownReason.MinorNetworkCard
  1010.  
  1011.         ''' <summary>
  1012.         ''' Other issue.
  1013.         ''' </summary>
  1014.         MinorOther = SystemRestarter.NativeMethods.ShutdownReason.MinorOther
  1015.  
  1016.         ''' <summary>
  1017.         ''' Other driver event.
  1018.         ''' </summary>
  1019.         MinorOtherDriver = SystemRestarter.NativeMethods.ShutdownReason.MinorOtherDriver
  1020.  
  1021.         ''' <summary>
  1022.         ''' Power supply.
  1023.         ''' </summary>
  1024.         MinorPowerSupply = SystemRestarter.NativeMethods.ShutdownReason.MinorPowerSupply
  1025.  
  1026.         ''' <summary>
  1027.         ''' Processor.
  1028.         ''' </summary>
  1029.         MinorProcessor = SystemRestarter.NativeMethods.ShutdownReason.MinorProcessor
  1030.  
  1031.         ''' <summary>
  1032.         ''' Reconfigure.
  1033.         ''' </summary>
  1034.         MinorReconfig = SystemRestarter.NativeMethods.ShutdownReason.MinorReconfig
  1035.  
  1036.         ''' <summary>
  1037.         ''' Security issue.
  1038.         ''' </summary>
  1039.         MinorSecurity = SystemRestarter.NativeMethods.ShutdownReason.MinorSecurity
  1040.  
  1041.         ''' <summary>
  1042.         ''' Security patch.
  1043.         ''' </summary>
  1044.         MinorSecurityFix = SystemRestarter.NativeMethods.ShutdownReason.MinorSecurityFix
  1045.  
  1046.         ''' <summary>
  1047.         ''' Security patch uninstallation.
  1048.         ''' </summary>
  1049.         MinorSecurityFixUninstall = SystemRestarter.NativeMethods.ShutdownReason.MinorSecurityFixUninstall
  1050.  
  1051.         ''' <summary>
  1052.         ''' Service pack.
  1053.         ''' </summary>
  1054.         MinorServicePack = SystemRestarter.NativeMethods.ShutdownReason.MinorServicePack
  1055.  
  1056.         ''' <summary>
  1057.         ''' Service pack uninstallation.
  1058.         ''' </summary>
  1059.         MinorServicePackUninstall = SystemRestarter.NativeMethods.ShutdownReason.MinorServicePackUninstall
  1060.  
  1061.         ''' <summary>
  1062.         ''' Terminal Services.
  1063.         ''' </summary>
  1064.         MinorTermSrv = SystemRestarter.NativeMethods.ShutdownReason.MinorTermSrv
  1065.  
  1066.         ''' <summary>
  1067.         ''' Unstable.
  1068.         ''' </summary>
  1069.         MinorUnstable = SystemRestarter.NativeMethods.ShutdownReason.MinorUnstable
  1070.  
  1071.         ''' <summary>
  1072.         ''' Upgrade.
  1073.         ''' </summary>
  1074.         MinorUpgrade = SystemRestarter.NativeMethods.ShutdownReason.MinorUpgrade
  1075.  
  1076.         ''' <summary>
  1077.         ''' WMI issue.
  1078.         ''' </summary>
  1079.         MinorWmi = SystemRestarter.NativeMethods.ShutdownReason.MinorWmi
  1080.  
  1081.     End Enum
  1082.  
  1083.     ''' <summary>
  1084.     ''' Specifies a shutdown planning.
  1085.     ''' </summary>
  1086.     Public Enum ShutdownPlanning As UInteger
  1087.  
  1088.         ''' <summary>
  1089.         ''' The shutdown was unplanned.
  1090.         ''' </summary>
  1091.         Unplanned = SystemRestarter.NativeMethods.ShutdownPlanning.Unplanned
  1092.  
  1093.         ''' <summary>
  1094.         ''' The reason code is defined by the user.
  1095.         ''' If this flag is not present, the reason code is defined by the system.
  1096.         ''' </summary>
  1097.         UserDefined = SystemRestarter.NativeMethods.ShutdownPlanning.UserDefined
  1098.  
  1099.         ''' <summary>
  1100.         ''' The shutdown was planned.
  1101.         ''' The system generates a System State Data (SSD) file.
  1102.         ''' This file contains system state information such as the processes, threads, memory usage, and configuration.
  1103.         ''' If this flag is not present, the shutdown was unplanned.
  1104.         ''' </summary>
  1105.         Planned = SystemRestarter.NativeMethods.ShutdownPlanning.Planned
  1106.  
  1107.     End Enum
  1108.  
  1109. #End Region
  1110.  
  1111. #Region " Constructors "
  1112.  
  1113.     ''' ----------------------------------------------------------------------------------------------------
  1114.     ''' <summary>
  1115.     ''' Prevents a default instance of the <see cref="SystemRestarter"/> class from being created.
  1116.     ''' </summary>
  1117.     ''' ----------------------------------------------------------------------------------------------------
  1118.     Private Sub New()
  1119.     End Sub
  1120.  
  1121. #End Region
  1122.  
  1123. #Region " Public Methods "
  1124.  
  1125.     ''' ----------------------------------------------------------------------------------------------------
  1126.     ''' <summary>
  1127.     ''' Aborts a system shutdown operation that has been initiated (unless a LogOff).
  1128.     ''' </summary>
  1129.     ''' ----------------------------------------------------------------------------------------------------
  1130.     ''' <param name="computer">
  1131.     ''' The network name of the computer where the shutdown is to be stopped.
  1132.     ''' If this parameter is an empty string, the function aborts the shutdown on the local computer.
  1133.     ''' </param>
  1134.     '''
  1135.     ''' <param name="ignoreErrors">
  1136.     ''' If <c>True</c>, a <see cref="Win32Exception"></see> exception will be thrown if error found.
  1137.     ''' </param>
  1138.     ''' ----------------------------------------------------------------------------------------------------
  1139.     ''' <exception cref="Win32Exception">
  1140.     ''' </exception>
  1141.     ''' ----------------------------------------------------------------------------------------------------
  1142.     ''' <returns>
  1143.     ''' <c>True</c> if the function succeeds, <c>False</c> otherwise.
  1144.     ''' </returns>
  1145.     ''' ----------------------------------------------------------------------------------------------------
  1146.     <DebuggerStepThrough>
  1147.     Public Shared Function Abort(Optional ByVal computer As String = Nothing,
  1148.                                  Optional ByVal ignoreErrors As Boolean = True) As Boolean
  1149.  
  1150.         SystemRestarter.GetShutdownPrivileges(Nothing, SystemRestarter.privilegeNameOfShutdown)
  1151.         SystemRestarter.GetShutdownPrivileges(Nothing, SystemRestarter.privilegeNameOfRemoteShutdown)
  1152.  
  1153.         If Not ignoreErrors AndAlso Not SystemRestarter.NativeMethods.AbortSystemShutdown(computer) Then
  1154.             Throw New Win32Exception(Marshal.GetLastWin32Error)
  1155.  
  1156.         Else
  1157.             Return True
  1158.  
  1159.         End If
  1160.  
  1161.     End Function
  1162.  
  1163.     ''' ----------------------------------------------------------------------------------------------------
  1164.     ''' <summary>
  1165.     ''' Shuts down all processes running in the logon session and then logs off the interactive user.
  1166.     ''' </summary>
  1167.     ''' ----------------------------------------------------------------------------------------------------
  1168.     ''' <param name="mode">
  1169.     ''' Indicates whether to force the logoff.
  1170.     ''' </param>
  1171.     '''
  1172.     ''' <param name="reason">
  1173.     ''' Indicates the reason for initiating the shutdown.
  1174.     ''' </param>
  1175.     '''
  1176.     ''' <param name="ignoreErrors">
  1177.     ''' If <c>True</c>, a <see cref="Win32Exception"></see> exception will be thrown if error found.
  1178.     ''' </param>
  1179.     ''' ----------------------------------------------------------------------------------------------------
  1180.     ''' <exception cref="Win32Exception">
  1181.     ''' </exception>
  1182.     ''' ----------------------------------------------------------------------------------------------------
  1183.     ''' <returns>
  1184.     ''' If the function succeeds, the return value is <c>True</c>.
  1185.     ''' The function executes asynchronously so a <c>True</c> return value indicates that the shutdown has been initiated.
  1186.     ''' It does not indicate whether the shutdown will succeed.
  1187.     ''' It is possible that the system, the user, or another application will abort the shutdown.
  1188.     ''' If the function fails, the return value is <c>False</c>.
  1189.     ''' </returns>
  1190.     ''' ----------------------------------------------------------------------------------------------------
  1191.     <DebuggerStepThrough>
  1192.     Public Shared Function LogOff(Optional ByVal mode As SystemRestarter.LogOffMode = SystemRestarter.LogOffMode.ForceIfHung,
  1193.                                   Optional ByVal reason As SystemRestarter.ShutdownReason = SystemRestarter.ShutdownReason.MajorOther,
  1194.                                   Optional ByVal ignoreErrors As Boolean = True) As Boolean
  1195.  
  1196.         SystemRestarter.GetShutdownPrivileges(Nothing, SystemRestarter.privilegeNameOfShutdown)
  1197.         SystemRestarter.GetShutdownPrivileges(Nothing, SystemRestarter.privilegeNameOfRemoteShutdown)
  1198.  
  1199.         If Not ignoreErrors AndAlso Not NativeMethods.ExitWindowsEx(SystemRestarter.NativeMethods.ExitwindowsExFlags.LogOff Or mode, reason) Then
  1200.             Throw New Win32Exception(Marshal.GetLastWin32Error)
  1201.  
  1202.         Else
  1203.             Return True
  1204.  
  1205.         End If
  1206.  
  1207.     End Function
  1208.  
  1209.     ''' ----------------------------------------------------------------------------------------------------
  1210.     ''' <summary>
  1211.     ''' Shutdowns the specified computer and begins powered down.
  1212.     ''' </summary>
  1213.     ''' ----------------------------------------------------------------------------------------------------
  1214.     ''' <param name="computer">
  1215.     ''' The name of the computer to poweroff.
  1216.     ''' If the value of this parameter is an empty string, the local computer is shut down.
  1217.     ''' This parameter can be an addres, for example: '127.0.0.1'
  1218.     ''' </param>
  1219.     '''
  1220.     ''' <param name="message">
  1221.     ''' The message to be displayed in the interactive poweroff dialog box.
  1222.     ''' </param>
  1223.     '''
  1224.     ''' <param name="timeout">
  1225.     ''' The number of seconds to wait before shutting down the computer.
  1226.     ''' If the value of this parameter is zero, the computer is poweroff immediately.
  1227.     ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
  1228.     ''' </param>
  1229.     '''
  1230.     ''' <param name="mode">
  1231.     ''' Indicates whether to force the PowerOff.
  1232.     ''' </param>
  1233.     '''
  1234.     ''' <param name="reason">
  1235.     ''' The reason for initiating the PowerOff.
  1236.     ''' If this parameter is zero,
  1237.     ''' the default is an undefined PowerOff that is logged as "No title for this reason could be found".
  1238.     ''' By default, it is also an 'unplanned' PowerOff.
  1239.     ''' </param>
  1240.     '''
  1241.     ''' <param name="planning">
  1242.     ''' Indicates whether it's a planned or unplanned PowerOff operation.
  1243.     ''' </param>
  1244.     '''
  1245.     ''' <param name="ignoreErrors">
  1246.     ''' If <c>True</c>, a <see cref="Win32Exception"></see> exception will be thrown if error found.
  1247.     ''' </param>
  1248.     ''' ----------------------------------------------------------------------------------------------------
  1249.     ''' <exception cref="ArgumentException">
  1250.     ''' Timeout should be zero or greater than zero.;timeout
  1251.     ''' </exception>
  1252.     '''
  1253.     ''' <exception cref="Win32Exception">
  1254.     ''' </exception>
  1255.     ''' ----------------------------------------------------------------------------------------------------
  1256.     ''' <returns>
  1257.     ''' <c>true</c> if the PowerOff operation is initiated correctlly, <c>false</c> otherwise.
  1258.     ''' </returns>
  1259.     ''' ----------------------------------------------------------------------------------------------------
  1260.     <DebuggerStepThrough>
  1261.     Public Shared Function PowerOff(Optional ByVal computer As String = Nothing,
  1262.                                     Optional ByVal timeout As Integer = 0,
  1263.                                     Optional ByVal message As String = Nothing,
  1264.                                     Optional ByVal mode As SystemRestarter.ShutdownMode = SystemRestarter.ShutdownMode.Wait,
  1265.                                     Optional ByVal reason As SystemRestarter.ShutdownReason = SystemRestarter.ShutdownReason.MajorOther,
  1266.                                     Optional ByVal planning As SystemRestarter.ShutdownPlanning = SystemRestarter.ShutdownPlanning.Unplanned,
  1267.                                     Optional ByVal ignoreErrors As Boolean = True) As Boolean
  1268.  
  1269.         SystemRestarter.GetShutdownPrivileges(computer, SystemRestarter.privilegeNameOfShutdown)
  1270.         SystemRestarter.GetShutdownPrivileges(computer, SystemRestarter.privilegeNameOfRemoteShutdown)
  1271.  
  1272.         Dim exitCode As UInteger
  1273.  
  1274.         Select Case timeout
  1275.  
  1276.             Case Is < 0
  1277.                 Throw New ArgumentException(message:="Timeout should be zero or greater than zero.", paramName:="timeout")
  1278.  
  1279.             Case Is = 0
  1280.                 exitCode = NativeMethods.InitiateShutdown(computer, message, timeout,
  1281.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.PowerOff Or
  1282.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.GraceOverride Or mode,
  1283.                                                           reason Or planning)
  1284.  
  1285.             Case Else
  1286.                 exitCode = NativeMethods.InitiateShutdown(computer, message, timeout,
  1287.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.PowerOff Or mode,
  1288.                                                           reason Or planning)
  1289.  
  1290.         End Select
  1291.  
  1292.         If (exitCode <> 0) AndAlso Not ignoreErrors Then
  1293.             Throw New Win32Exception([error]:=Convert.ToInt32(exitCode))
  1294.  
  1295.         Else
  1296.             Return True
  1297.  
  1298.         End If
  1299.  
  1300.     End Function
  1301.  
  1302.     ''' ----------------------------------------------------------------------------------------------------
  1303.     ''' <summary>
  1304.     ''' Restarts the specified computer.
  1305.     ''' </summary>
  1306.     ''' ----------------------------------------------------------------------------------------------------
  1307.     ''' <param name="computer">
  1308.     ''' The name of the computer to restart.
  1309.     ''' If the value of this parameter is an empty string, the local computer is shut down.
  1310.     ''' This parameter can be an addres, for example: '127.0.0.1'
  1311.     ''' </param>
  1312.     '''
  1313.     ''' <param name="message">
  1314.     ''' The message to be displayed in the interactive restart dialog box.
  1315.     ''' </param>
  1316.     '''
  1317.     ''' <param name="timeout">
  1318.     ''' The number of seconds to wait before restarting the computer.
  1319.     ''' If the value of this parameter is zero, the computer is restarted immediately.
  1320.     ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
  1321.     ''' </param>
  1322.     '''
  1323.     ''' <param name="mode">
  1324.     ''' Indicates whether to force the restart.
  1325.     ''' </param>
  1326.     '''
  1327.     ''' <param name="reason">
  1328.     ''' The reason for initiating the restart.
  1329.     ''' If this parameter is zero,
  1330.     ''' the default is an undefined restart that is logged as "No title for this reason could be found".
  1331.     ''' By default, it is also an 'unplanned' restart.
  1332.     ''' </param>
  1333.     '''
  1334.     ''' <param name="planning">
  1335.     ''' Indicates whether it's a planned or unplanned restart operation.
  1336.     ''' </param>
  1337.     '''
  1338.     ''' <param name="ignoreErrors">
  1339.     ''' If <c>True</c>, a <see cref="Win32Exception"></see> exception will be thrown if error found.
  1340.     ''' </param>
  1341.     ''' ----------------------------------------------------------------------------------------------------
  1342.     ''' <exception cref="ArgumentException">
  1343.     ''' Timeout should be zero or greater than zero.;timeout
  1344.     ''' </exception>
  1345.     '''
  1346.     ''' <exception cref="Win32Exception">
  1347.     ''' </exception>
  1348.     ''' ----------------------------------------------------------------------------------------------------
  1349.     ''' <returns>
  1350.     ''' <c>true</c> if the restart operation is initiated correctlly, <c>false</c> otherwise.
  1351.     ''' </returns>
  1352.     ''' ----------------------------------------------------------------------------------------------------
  1353.     <DebuggerStepThrough>
  1354.     Public Shared Function Restart(Optional ByVal computer As String = Nothing,
  1355.                                    Optional ByVal timeout As Integer = 0,
  1356.                                    Optional ByVal message As String = Nothing,
  1357.                                    Optional ByVal mode As SystemRestarter.ShutdownMode = SystemRestarter.ShutdownMode.Wait,
  1358.                                    Optional ByVal reason As SystemRestarter.ShutdownReason = SystemRestarter.ShutdownReason.MajorOther,
  1359.                                    Optional ByVal planning As SystemRestarter.ShutdownPlanning = SystemRestarter.ShutdownPlanning.Unplanned,
  1360.                                    Optional ByVal ignoreErrors As Boolean = True) As Boolean
  1361.  
  1362.         SystemRestarter.GetShutdownPrivileges(computer, SystemRestarter.privilegeNameOfShutdown)
  1363.         SystemRestarter.GetShutdownPrivileges(computer, SystemRestarter.privilegeNameOfRemoteShutdown)
  1364.  
  1365.         Dim exitCode As UInteger
  1366.  
  1367.         Select Case timeout
  1368.  
  1369.             Case Is < 0
  1370.                 Throw New ArgumentException(message:="Timeout should be zero or greater than zero.", paramName:="timeout")
  1371.  
  1372.             Case Is = 0
  1373.                 exitCode = NativeMethods.InitiateShutdown(computer, message, timeout,
  1374.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.Restart Or
  1375.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.GraceOverride Or mode,
  1376.                                                           reason Or planning)
  1377.  
  1378.             Case Else
  1379.                 exitCode = NativeMethods.InitiateShutdown(computer, message, timeout,
  1380.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.Restart Or mode,
  1381.                                                           reason Or planning)
  1382.  
  1383.  
  1384.         End Select
  1385.  
  1386.         If (exitCode <> 0) AndAlso Not ignoreErrors Then
  1387.             Throw New Win32Exception([error]:=Convert.ToInt32(exitCode))
  1388.  
  1389.         Else
  1390.             Return True
  1391.  
  1392.         End If
  1393.  
  1394.     End Function
  1395.  
  1396.     ''' ----------------------------------------------------------------------------------------------------
  1397.     ''' <summary>
  1398.     ''' Restarts the specified computer,
  1399.     ''' also restarts any applications that have been registered for restart in the system.
  1400.     ''' </summary>
  1401.     ''' ----------------------------------------------------------------------------------------------------
  1402.     ''' <param name="computer">
  1403.     ''' The name of the computer to restart.
  1404.     ''' If the value of this parameter is an empty string, the local computer is shut down.
  1405.     ''' This parameter can be an addres, for example: '127.0.0.1'
  1406.     ''' </param>
  1407.     '''
  1408.     ''' <param name="message">
  1409.     ''' The message to be displayed in the interactive restart dialog box.
  1410.     ''' </param>
  1411.     '''
  1412.     ''' <param name="timeout">
  1413.     ''' The number of seconds to wait before restarting the computer.
  1414.     ''' If the value of this parameter is zero, the computer is restarted immediately.
  1415.     ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
  1416.     ''' </param>
  1417.     '''
  1418.     ''' <param name="mode">
  1419.     ''' Indicates whether to force the restart.
  1420.     ''' </param>
  1421.     '''
  1422.     ''' <param name="reason">
  1423.     ''' The reason for initiating the restart.
  1424.     ''' If this parameter is zero,
  1425.     ''' the default is an undefined restart that is logged as "No title for this reason could be found".
  1426.     ''' By default, it is also an 'unplanned' restart.
  1427.     ''' </param>
  1428.     '''
  1429.     ''' <param name="planning">
  1430.     ''' Indicates whether it's a planned or unplanned restart operation.
  1431.     ''' </param>
  1432.     '''
  1433.     ''' <param name="ignoreErrors">
  1434.     ''' If <c>True</c>, a <see cref="Win32Exception"></see> exception will be thrown if error found.
  1435.     ''' </param>
  1436.     ''' ----------------------------------------------------------------------------------------------------
  1437.     ''' <exception cref="ArgumentException">
  1438.     ''' Timeout should be zero or greater than zero.;timeout
  1439.     ''' </exception>
  1440.     '''
  1441.     ''' <exception cref="Win32Exception">
  1442.     ''' </exception>
  1443.     ''' ----------------------------------------------------------------------------------------------------
  1444.     ''' <returns>
  1445.     ''' <c>true</c> if the restart operation is initiated correctlly, <c>false</c> otherwise.
  1446.     ''' </returns>
  1447.     ''' ----------------------------------------------------------------------------------------------------
  1448.     <DebuggerStepThrough>
  1449.     Public Shared Function RestartApps(Optional ByVal computer As String = Nothing,
  1450.                                        Optional ByVal timeout As Integer = 0,
  1451.                                        Optional ByVal message As String = Nothing,
  1452.                                        Optional ByVal mode As SystemRestarter.ShutdownMode = SystemRestarter.ShutdownMode.Wait,
  1453.                                        Optional ByVal reason As SystemRestarter.ShutdownReason = SystemRestarter.ShutdownReason.MajorOther,
  1454.                                        Optional ByVal planning As SystemRestarter.ShutdownPlanning = SystemRestarter.ShutdownPlanning.Unplanned,
  1455.                                        Optional ByVal ignoreErrors As Boolean = True) As Boolean
  1456.  
  1457.         SystemRestarter.GetShutdownPrivileges(computer, SystemRestarter.privilegeNameOfShutdown)
  1458.         SystemRestarter.GetShutdownPrivileges(computer, SystemRestarter.privilegeNameOfRemoteShutdown)
  1459.  
  1460.         Dim exitCode As UInteger
  1461.  
  1462.         Select Case timeout
  1463.  
  1464.             Case Is < 0
  1465.                 Throw New ArgumentException(message:="Timeout should be zero or greater than zero.", paramName:="timeout")
  1466.  
  1467.             Case Is = 0
  1468.                 exitCode = NativeMethods.InitiateShutdown(computer, message, timeout,
  1469.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.RestartApps Or
  1470.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.GraceOverride Or mode,
  1471.                                                           reason Or planning)
  1472.  
  1473.             Case Else
  1474.                 exitCode = NativeMethods.InitiateShutdown(computer, message, timeout,
  1475.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.RestartApps Or mode,
  1476.                                                           reason Or planning)
  1477.  
  1478.         End Select
  1479.  
  1480.         If (exitCode <> 0) AndAlso Not ignoreErrors Then
  1481.             Throw New Win32Exception([error]:=Convert.ToInt32(exitCode))
  1482.  
  1483.         Else
  1484.             Return True
  1485.  
  1486.         End If
  1487.  
  1488.     End Function
  1489.  
  1490.     ''' ----------------------------------------------------------------------------------------------------
  1491.     ''' <summary>
  1492.     ''' Shutdowns the specified computer.
  1493.     ''' </summary>
  1494.     ''' ----------------------------------------------------------------------------------------------------
  1495.     ''' <param name="computer">
  1496.     ''' The name of the computer to be shut down.
  1497.     ''' If the value of this parameter is an empty string, the local computer is shut down.
  1498.     ''' This parameter can be an addres, for example: '127.0.0.1'
  1499.     ''' </param>
  1500.     '''
  1501.     ''' <param name="message">
  1502.     ''' The message to be displayed in the interactive shutdown dialog box.
  1503.     ''' </param>
  1504.     '''
  1505.     ''' <param name="timeout">
  1506.     ''' The number of seconds to wait before shutting down the computer.
  1507.     ''' If the value of this parameter is zero, the computer is shut down immediately.
  1508.     ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
  1509.     ''' </param>
  1510.     '''
  1511.     ''' <param name="mode">
  1512.     ''' Indicates whether to force the shutdown.
  1513.     ''' </param>
  1514.     '''
  1515.     ''' <param name="reason">
  1516.     ''' The reason for initiating the shutdown.
  1517.     ''' If this parameter is zero,
  1518.     ''' the default is an undefined shutdown that is logged as "No title for this reason could be found".
  1519.     ''' By default, it is also an 'unplanned' shutdown.
  1520.     ''' </param>
  1521.     '''
  1522.     ''' <param name="planning">
  1523.     ''' Indicates whether it's a planned or unplanned shutdoen operation.
  1524.     ''' </param>
  1525.     '''
  1526.     ''' <param name="ignoreErrors">
  1527.     ''' If <c>True</c>, a <see cref="Win32Exception"></see> exception will be thrown if error found.
  1528.     ''' </param>
  1529.     ''' ----------------------------------------------------------------------------------------------------
  1530.     ''' <exception cref="ArgumentException">
  1531.     ''' Timeout should be zero or greater than zero.;timeout
  1532.     ''' </exception>
  1533.     '''
  1534.     ''' <exception cref="Win32Exception">
  1535.     ''' </exception>
  1536.     ''' ----------------------------------------------------------------------------------------------------
  1537.     ''' <returns>
  1538.     ''' <c>true</c> if the shutdown operation is initiated correctlly, <c>false</c> otherwise.
  1539.     ''' </returns>
  1540.     ''' ----------------------------------------------------------------------------------------------------
  1541.     <DebuggerStepThrough>
  1542.     Public Shared Function Shutdown(Optional ByVal computer As String = Nothing,
  1543.                                     Optional ByVal timeout As Integer = 0,
  1544.                                     Optional ByVal message As String = Nothing,
  1545.                                     Optional ByVal mode As SystemRestarter.ShutdownMode = SystemRestarter.ShutdownMode.Wait,
  1546.                                     Optional ByVal reason As SystemRestarter.ShutdownReason = SystemRestarter.ShutdownReason.MajorOther,
  1547.                                     Optional ByVal planning As SystemRestarter.ShutdownPlanning = SystemRestarter.ShutdownPlanning.Unplanned,
  1548.                                     Optional ByVal ignoreErrors As Boolean = True) As Boolean
  1549.  
  1550.         SystemRestarter.GetShutdownPrivileges(computer, SystemRestarter.privilegeNameOfShutdown)
  1551.         SystemRestarter.GetShutdownPrivileges(computer, SystemRestarter.privilegeNameOfRemoteShutdown)
  1552.  
  1553.         Dim exitCode As UInteger
  1554.  
  1555.         Select Case timeout
  1556.  
  1557.             Case Is < 0
  1558.                 Throw New ArgumentException(message:="Timeout should be zero or greater than zero.", paramName:="timeout")
  1559.  
  1560.             Case Is = 0
  1561.                 exitCode = NativeMethods.InitiateShutdown(computer, message, timeout,
  1562.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.Shutdown Or
  1563.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.GraceOverride Or mode,
  1564.                                                           reason Or planning)
  1565.  
  1566.             Case Else
  1567.                 exitCode = NativeMethods.InitiateShutdown(computer, message, timeout,
  1568.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.Shutdown Or mode,
  1569.                                                           reason Or planning)
  1570.  
  1571.         End Select
  1572.  
  1573.         If (exitCode <> 0) AndAlso Not ignoreErrors Then
  1574.             Throw New Win32Exception([error]:=Convert.ToInt32(exitCode))
  1575.  
  1576.         Else
  1577.             Return True
  1578.  
  1579.         End If
  1580.  
  1581.     End Function
  1582.  
  1583.     ''' ----------------------------------------------------------------------------------------------------
  1584.     ''' <summary>
  1585.     ''' Shutdowns the specified computer and prepares the system for a faster startup.
  1586.     ''' Use this function only for Windows 8/8.1
  1587.     ''' </summary>
  1588.     ''' ----------------------------------------------------------------------------------------------------
  1589.     ''' <param name="computer">
  1590.     ''' The name of the computer to be shut down.
  1591.     ''' If the value of this parameter is an empty string, the local computer is shut down.
  1592.     ''' This parameter can be an addres, for example: '127.0.0.1'
  1593.     ''' </param>
  1594.     '''
  1595.     ''' <param name="message">
  1596.     ''' The message to be displayed in the interactive shutdown dialog box.
  1597.     ''' </param>
  1598.     '''
  1599.     ''' <param name="timeout">
  1600.     ''' The number of seconds to wait before shutting down the computer.
  1601.     ''' If the value of this parameter is zero, the computer is shut down immediately.
  1602.     ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
  1603.     ''' </param>
  1604.     '''
  1605.     ''' <param name="mode">
  1606.     ''' Indicates whether to force the shutdown.
  1607.     ''' </param>
  1608.     '''
  1609.     ''' <param name="reason">
  1610.     ''' The reason for initiating the shutdown.
  1611.     ''' If this parameter is zero,
  1612.     ''' the default is an undefined shutdown that is logged as "No title for this reason could be found".
  1613.     ''' By default, it is also an 'unplanned' shutdown.
  1614.     ''' </param>
  1615.     '''
  1616.     ''' <param name="planning">
  1617.     ''' Indicates whether it's a planned or unplanned shutdoen operation.
  1618.     ''' </param>
  1619.     '''
  1620.     ''' <param name="ignoreErrors">
  1621.     ''' If <c>True</c>, a <see cref="Win32Exception"></see> exception will be thrown if error found.
  1622.     ''' </param>
  1623.     ''' ----------------------------------------------------------------------------------------------------
  1624.     ''' <exception cref="ArgumentException">
  1625.     ''' Timeout should be zero or greater than zero.;timeout
  1626.     ''' </exception>
  1627.     '''
  1628.     ''' <exception cref="Win32Exception">
  1629.     ''' </exception>
  1630.     ''' ----------------------------------------------------------------------------------------------------
  1631.     ''' <returns>
  1632.     ''' <c>true</c> if the hydrid shutdown operation is initiated correctlly, <c>false</c> otherwise.
  1633.     ''' </returns>
  1634.     ''' ----------------------------------------------------------------------------------------------------
  1635.     <DebuggerStepThrough>
  1636.     Public Shared Function HybridShutdown(Optional ByVal computer As String = Nothing,
  1637.                                           Optional ByVal timeout As Integer = 0,
  1638.                                           Optional ByVal message As String = Nothing,
  1639.                                           Optional ByVal mode As SystemRestarter.ShutdownMode = SystemRestarter.ShutdownMode.Wait,
  1640.                                           Optional ByVal reason As SystemRestarter.ShutdownReason = SystemRestarter.ShutdownReason.MajorOther,
  1641.                                           Optional ByVal planning As SystemRestarter.ShutdownPlanning = SystemRestarter.ShutdownPlanning.Unplanned,
  1642.                                           Optional ByVal ignoreErrors As Boolean = True) As Boolean
  1643.  
  1644.         SystemRestarter.GetShutdownPrivileges(computer, SystemRestarter.privilegeNameOfShutdown)
  1645.         SystemRestarter.GetShutdownPrivileges(computer, SystemRestarter.privilegeNameOfRemoteShutdown)
  1646.  
  1647.         Dim exitCode As UInteger
  1648.  
  1649.         Select Case timeout
  1650.  
  1651.             Case Is < 0
  1652.                 Throw New ArgumentException(message:="Timeout should be zero or greater than zero.", paramName:="timeout")
  1653.  
  1654.             Case Is = 0
  1655.                 exitCode = NativeMethods.InitiateShutdown(computer, message, timeout,
  1656.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.Shutdown Or
  1657.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.HybridShutdown Or
  1658.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.GraceOverride Or mode,
  1659.                                                           reason Or planning)
  1660.  
  1661.             Case Else
  1662.                 exitCode = NativeMethods.InitiateShutdown(computer, message, timeout,
  1663.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.Shutdown Or
  1664.                                                           SystemRestarter.NativeMethods.InitiateShutdownFlags.HybridShutdown Or mode,
  1665.                                                           reason Or planning)
  1666.  
  1667.         End Select
  1668.  
  1669.         If (exitCode <> 0) AndAlso Not ignoreErrors Then
  1670.             Throw New Win32Exception([error]:=Convert.ToInt32(exitCode))
  1671.  
  1672.         Else
  1673.             Return True
  1674.  
  1675.         End If
  1676.  
  1677.     End Function
  1678.  
  1679. #End Region
  1680.  
  1681. #Region " Private Methods "
  1682.  
  1683.     ''' ----------------------------------------------------------------------------------------------------
  1684.     ''' <summary>
  1685.     ''' Gets the necessary shutdown privileges to perform a local shutdown operation.
  1686.     ''' </summary>
  1687.     ''' ----------------------------------------------------------------------------------------------------
  1688.     ''' <param name="Computer">
  1689.     ''' Indicates the computer where to set the privileges.
  1690.     ''' If an empty string is specified, the function attempts to find the privilege name on the local system.
  1691.     ''' </param>
  1692.     '''
  1693.     ''' <param name="privilegeName">
  1694.     ''' The privilege name.
  1695.     ''' </param>
  1696.     ''' ----------------------------------------------------------------------------------------------------
  1697.     <DebuggerStepThrough>
  1698.     Private Shared Sub GetShutdownPrivileges(ByVal computer As String,
  1699.                                              ByVal privilegeName As String)
  1700.  
  1701.         Dim hToken As IntPtr
  1702.         Dim tkp As New SystemRestarter.NativeMethods.TokenPrivileges
  1703.  
  1704.         SystemRestarter.NativeMethods.OpenProcessToken(Process.GetCurrentProcess.Handle,
  1705.                                                        SystemRestarter.NativeMethods.AccessRights.TokenAdjustPrivileges Or
  1706.                                                        SystemRestarter.NativeMethods.AccessRights.TokenQuery,
  1707.                                                        hToken)
  1708.  
  1709.         With tkp
  1710.             .PrivilegeCount = 1
  1711.             .Privileges.Attributes = SystemRestarter.NativeMethods.TokenPrivilegesFlags.PrivilegeEnabled
  1712.         End With
  1713.  
  1714.         SystemRestarter.NativeMethods.LookupPrivilegeValue(computer, privilegeName, tkp.Privileges.pLuid)
  1715.         SystemRestarter.NativeMethods.AdjustTokenPrivileges(hToken, False, tkp, 0UI, IntPtr.Zero, IntPtr.Zero)
  1716.  
  1717.     End Sub
  1718.  
  1719. #End Region
  1720.  
  1721. End Class
  1722.  
  1723. #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement