Advertisement
Guest User

Untitled

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