Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' ***********************************************************************
- ' Author : Elektro
- ' Modified : 02-15-2014
- ' ***********************************************************************
- ' <copyright file="SystemRestarter.vb" company="Elektro Studios">
- ' Copyright (c) Elektro Studios. All rights reserved.
- ' </copyright>
- ' ***********************************************************************
- #Region " Usage Examples "
- 'Sub Test()
- ' ' Restart the current computer in 30 seconds and wait for applications to close.
- ' ' Specify that the restart operation is planned because a consecuence of an installation.
- ' Dim Success =
- ' SystemRestarter.Restart(Nothing, 30, "System is gonna be restarted quickly, save all your data...!",
- ' SystemRestarter.Enums.InitiateShutdown_Force.Wait,
- ' SystemRestarter.Enums.ShutdownReason.MajorOperatingSystem Or
- ' SystemRestarter.Enums.ShutdownReason.MinorInstallation,
- ' SystemRestarter.Enums.ShutdownPlanning.Planned)
- ' Console.WriteLine(String.Format("Restart operation initiated successfully?: {0}", CStr(Success)))
- ' ' Abort the current operation.
- ' If Success Then
- ' Dim IsAborted = SystemRestarter.Abort()
- ' Console.WriteLine(String.Format("Restart operation aborted successfully?: {0}", CStr(IsAborted)))
- ' Else
- ' Console.WriteLine("There is any restart operation to abort.")
- ' End If
- ' Console.ReadKey()
- ' ' Shutdown the current computer instantlly and force applications to close.
- ' ' ( When timeout is '0' the operation can't be aborted )
- ' SystemRestarter.Shutdown(Nothing, 0, Nothing, SystemRestarter.Enums.InitiateShutdown_Force.ForceSelf)
- ' ' LogOffs the current user.
- ' SystemRestarter.LogOff(SystemRestarter.Enums.ExitwindowsEx_Force.Wait)
- 'End Sub
- #End Region
- #Region " Imports "
- Imports System.ComponentModel
- Imports System.Runtime.InteropServices
- #End Region
- #Region " SystemRestarter "
- ''' <summary>
- ''' Performs different Shutdown system's operations on a local or remote machine.
- ''' </summary>
- Public Class SystemRestarter
- #Region " P/Invoke "
- #Region " Methods "
- ''' <summary>
- ''' WinAPI methods used by the Main Class.
- ''' </summary>
- Private Class NativeMethods
- ''' <summary>
- ''' Logs off the interactive user, shuts down the system, or shuts down and restarts the system.
- ''' It sends the 'WM_QUERYENDSESSION' message to all applications to determine if they can be terminated.
- ''' </summary>
- ''' <param name="uFlags">
- ''' Indicates the shutdown type.
- ''' </param>
- ''' <param name="dwReason">
- ''' Indicates the reason for initiating the shutdown.
- ''' </param>
- ''' <returns>
- ''' If the function succeeds, the return value is 'True'.
- ''' The function executes asynchronously so a 'True' return value indicates that the shutdown has been initiated.
- ''' It does not indicate whether the shutdown will succeed.
- ''' It is possible that the system, the user, or another application will abort the shutdown.
- ''' If the function fails, the return value is 'False'.
- ''' </returns>
- <DllImport("user32.dll", SetLastError:=True)>
- Friend Shared Function ExitWindowsEx(
- ByVal uFlags As Enums.ExitwindowsEx_Flags,
- ByVal dwReason As Enums.ShutdownReason
- ) As <MarshalAs(UnmanagedType.Bool)> Boolean
- End Function
- ''' <summary>
- ''' Initiates a shutdown and restart of the specified computer,
- ''' and restarts any applications that have been registered for restart.
- ''' </summary>
- ''' <param name="lpMachineName">
- ''' The name of the computer to be shut down.
- ''' If the value of this parameter is 'NULL', the local computer is shut down.
- ''' This parameter can be an addres, for example: '127.0.0.1'
- ''' </param>
- ''' <param name="lpMessage">
- ''' The message to be displayed in the interactive shutdown dialog box.
- ''' </param>
- ''' <param name="dwGracePeriod">
- ''' The number of seconds to wait before shutting down the computer.
- ''' If the value of this parameter is zero, the computer is shut down immediately.
- ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
- ''' If the value of this parameter is greater than zero, and the 'dwShutdownFlags' parameter
- ''' specifies the flag 'GRACE_OVERRIDE', the function fails and returns the error code 'ERROR_BAD_ARGUMENTS'.
- ''' </param>
- ''' <param name="dwShutdownFlags">
- ''' Specifies options for the shutdown.
- ''' </param>
- ''' <param name="dwReason">
- ''' The reason for initiating the shutdown.
- ''' If this parameter is zero,
- ''' the default is an undefined shutdown that is logged as "No title for this reason could be found".
- ''' By default, it is also an 'unplanned' shutdown.
- ''' </param>
- ''' <returns>UInt32.</returns>
- <DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
- Friend Shared Function InitiateShutdown(
- ByVal lpMachineName As String,
- ByVal lpMessage As String,
- ByVal dwGracePeriod As UInteger,
- ByVal dwShutdownFlags As Enums.InitiateShutdown_Flags,
- ByVal dwReason As Enums.ShutdownReason
- ) As UInteger
- End Function
- ''' <summary>
- ''' Aborts a system shutdown that has been initiated.
- ''' </summary>
- ''' <param name="lpMachineName">
- ''' The network name of the computer where the shutdown is to be stopped.
- ''' If this parameter is 'NULL' or an empty string, the function aborts the shutdown on the local computer.
- ''' </param>
- ''' <returns><c>True</c> if the function succeeds, <c>False</c> otherwise.</returns>
- <DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
- Friend Shared Function AbortSystemShutdown(
- Optional lpMachineName As String = "127.0.0.1"
- ) As <MarshalAs(UnmanagedType.Bool)> Boolean
- End Function
- ''' <summary>
- ''' Opens the access token associated with a process.
- ''' </summary>
- ''' <param name="ProcessHandle">
- ''' A handle to the process whose access token is opened.
- ''' The process must have the 'PROCESS_QUERY_INFORMATION' access permission.
- ''' </param>
- ''' <param name="DesiredAccess">
- ''' Specifies an access mask that specifies the requested types of access to the access token.
- ''' These requested access types are compared with the discretionary access control list (DACL)
- ''' of the token to determine which accesses are granted or denied.
- ''' </param>
- ''' <param name="TokenHandle">
- ''' A pointer to a handle that identifies the newly opened access token when the function returns.
- ''' </param>
- ''' <returns>System.Int32.</returns>
- <DllImport("advapi32.dll")>
- Friend Shared Function OpenProcessToken(
- ByVal ProcessHandle As IntPtr,
- ByVal DesiredAccess As Enums.AccessRights,
- ByRef TokenHandle As IntPtr
- ) As Integer
- End Function
- ''' <summary>
- ''' Enables or disables privileges in the specified access token.
- ''' Enabling or disabling privileges in an access token requires 'TOKEN_ADJUST_PRIVILEGES' access.
- ''' </summary>
- ''' <param name="TokenHandle">
- ''' A handle to the access token that contains the privileges to be modified.
- ''' The handle must have 'TOKEN_ADJUST_PRIVILEGES' access to the token.
- ''' If the 'PreviousState' parameter is not NULL, the handle must also have 'TOKEN_QUERY' access.
- ''' </param>
- ''' <param name="DisableAllPrivileges">
- ''' Specifies whether the function disables all of the token's privileges.
- ''' If this value is 'TRUE', the function disables all privileges and ignores the 'NewState' parameter.
- ''' If it is 'FALSE', the function modifies privileges based on the information pointed to by the NewState parameter.
- ''' </param>
- ''' <param name="NewState">
- ''' A pointer to a 'TOKEN_PRIVILEGES' structure that specifies an array of privileges and their attributes.
- ''' If the 'DisableAllPrivileges' parameter is 'FALSE',
- ''' the 'AdjustTokenPrivileges' function enables, disables, or removes these privileges for the token.
- ''' </param>
- ''' <param name="BufferLength">
- ''' Specifies the size, in bytes, of the buffer pointed to by the 'PreviousState' parameter.
- ''' This parameter can be zero if the PreviousState parameter is 'NULL'.
- ''' </param>
- ''' <param name="PreviousState">
- ''' A pointer to a buffer that the function fills with a 'TOKEN_PRIVILEGES' structure
- ''' that contains the previous state of any privileges that the function modifies.
- ''' That is, if a privilege has been modified by this function,
- ''' the privilege and its previous state are contained in the 'TOKEN_PRIVILEGES' structure
- ''' referenced by 'PreviousState'.
- ''' If the 'PrivilegeCount' member of 'TOKEN_PRIVILEGES' is zero,
- ''' then no privileges have been changed by this function.
- ''' This parameter can be 'NULL'.</param>
- ''' <param name="ReturnLength">
- ''' A pointer to a variable that receives the required size, in bytes,
- ''' of the buffer pointed to by the 'PreviousState' parameter.
- ''' This parameter can be 'NULL' if 'PreviousState' is 'NULL'.
- ''' </param>
- ''' <returns>
- ''' If the function succeeds, the return value is nonzero, otherwise, zero.
- ''' To determine whether the function adjusted all of the specified privileges, call 'GetLastError'.</returns>
- <DllImport("advapi32.dll", SetLastError:=True)>
- Friend Shared Function AdjustTokenPrivileges(
- ByVal TokenHandle As IntPtr,
- <MarshalAs(UnmanagedType.Bool)> ByVal DisableAllPrivileges As Boolean,
- ByRef NewState As Structures.TOKEN_PRIVILEGES,
- ByVal BufferLength As UInt32,
- ByVal PreviousState As IntPtr,
- ByVal ReturnLength As IntPtr
- ) As <MarshalAs(UnmanagedType.Bool)> Boolean
- End Function
- ''' <summary>
- ''' Retrieves the locally unique identifier (LUID) used on a specified system,
- ''' to locally represent the specified privilege name.
- ''' </summary>
- ''' <param name="lpSystemName">
- ''' A pointer to a null-terminated string that specifies the name of the system
- ''' on which the privilege name is retrieved.
- ''' If a null string is specified, the function attempts to find the privilege name on the local system
- ''' </param>
- ''' <param name="lpName">
- ''' A pointer to a null-terminated string that specifies the name of the privilege,
- ''' as defined in the Winnt.h header file.
- ''' For example, this parameter could specify the constant, 'SE_SECURITY_NAME',
- ''' or its corresponding string, "SeSecurityPrivilege".
- ''' </param>
- ''' <param name="lpLuid">
- ''' A pointer to a variable that receives the LUID by which the privilege is known on
- ''' the system specified by the lpSystemName parameter.
- ''' </param>
- ''' <returns>System.Int32.</returns>
- <DllImport("advapi32.dll", CharSet:=CharSet.Unicode)>
- Friend Shared Function LookupPrivilegeValue(
- ByVal lpSystemName As String,
- ByVal lpName As String,
- ByRef lpLuid As Structures.LUID
- ) As Integer
- End Function
- End Class
- #End Region
- #Region " Read-Only Properties (Privileges) "
- ''' <summary>
- ''' Privileges determine the type of system operations that a user account can perform.
- ''' An administrator assigns privileges to user and group accounts.
- ''' Each user's privileges include those granted to the user and to the groups to which the user belongs.
- ''' </summary>
- Private Class Privileges
- ''' <summary>
- ''' Privilege required to shut down a system using a network request.
- ''' User Right: Force shutdown from a remote system.
- ''' </summary>
- ''' For more Info see:
- ''' http://msdn.microsoft.com/en-us/library/windows/desktop/bb530716%28v=vs.85%29.aspx
- Public Shared ReadOnly Property SE_SHUTDOWN_NAME As String
- Get
- Return "SeShutdownPrivilege"
- End Get
- End Property
- ''' <summary>
- ''' Privilege required to shut down a local system.
- ''' User Right: Shut down the system.
- ''' </summary>
- ''' For more Info see:
- ''' http://msdn.microsoft.com/en-us/library/windows/desktop/bb530716%28v=vs.85%29.aspx
- Public Shared ReadOnly Property SE_REMOTE_SHUTDOWN_NAME As String
- Get
- Return "SeRemoteShutdownPrivilege"
- End Get
- End Property
- End Class
- #End Region
- #Region " Enumerations "
- ''' <summary>
- ''' Enumerations used by the Main Class.
- ''' </summary>
- Public Class Enums
- ''' <summary>
- ''' Indicates the shutdown type.
- ''' </summary>
- <Description("Enum used in the 'uFlags' parameter of 'ExitWindowsEx' Function.")>
- <Flags>
- Public Enum ExitwindowsEx_Flags As UInteger
- '******'
- ' NOTE '
- '******'
- ' This Enumeration is partially defined.
- ''' <summary>
- ''' Shuts down all processes running in the logon session of the current process.
- ''' Then it logs the user off.
- ''' This flag can be used only by processes running in an interactive user's logon session.
- ''' </summary>
- LogOff = &H0UI
- End Enum
- ''' <summary>
- ''' Indicates the forcing type.
- ''' </summary>
- <Description("Enum used in combination with the 'uFlags' parameter of 'ExitWindowsEx' Function.")>
- Public Enum ExitwindowsEx_Force As UInteger
- ''' <summary>
- ''' Don't force the system to close the applications.
- ''' This is the default parameter.
- ''' </summary>
- Wait = &H0UI
- ''' <summary>
- ''' This flag has no effect if terminal services is enabled.
- ''' Otherwise, the system does not send the 'WM_QUERYENDSESSIO'N message.
- ''' This can cause applications to lose data.
- ''' Therefore, you should only use this flag in an emergency.
- ''' </summary>
- Force = &H4UI
- ''' <summary>
- ''' Forces processes to terminate if they do not respond to the 'WM_QUERYENDSESSION',
- ''' or 'WM_ENDSESSION' message within the timeout interval.
- ''' </summary>
- ForceIfHung = &H10UI
- End Enum
- ''' <summary>
- ''' Indicates the shutdown type.
- ''' </summary>
- <Description("Enum used in the 'dwShutdownFlags' parameter of 'InitiateShutdown' Function.")>
- <Flags>
- Public Enum InitiateShutdown_Flags As UInteger
- ''' <summary>
- ''' Overrides the grace period so that the computer is shut down immediately.
- ''' </summary>
- GraceOverride = &H20UI
- ''' <summary>
- ''' Only for Windows 8/8.1
- ''' Prepares the system for a faster startup by combining
- ''' the 'HybridShutDown' flag with the 'ShutDown' flag.
- ''' 'InitiateShutdown' always initiate a full system shutdown if the 'HybridShutdown' flag is not set.
- ''' </summary>
- HybridShutdown = &H200UI
- ''' <summary>
- ''' The computer installs any updates before starting the shutdown.
- ''' </summary>
- InstallUpdates = &H40UI
- ''' <summary>
- ''' The computer is shut down but is not powered down or restarted.
- ''' </summary>
- Shutdown = &H10UI
- ''' <summary>
- ''' The computer is shut down and powered down.
- ''' </summary>
- PowerOff = &H8UI
- ''' <summary>
- ''' The computer is shut down and restarted.
- ''' </summary>
- Restart = &H4UI
- ''' <summary>
- ''' The system is restarted using the 'ExitWindowsEx' function with the 'RESTARTAPPS' flag.
- ''' This restarts any applications that have been registered for restart
- ''' using the 'RegisterApplicationRestart' function.
- ''' </summary>
- RestartApps = &H80UI
- End Enum
- ''' <summary>
- ''' Indicates the forced shutdown type.
- ''' </summary>
- <Description("Enum used in combination with the 'uFlags' parameter of 'InitiateShutdown' Function.")>
- Public Enum InitiateShutdown_Force As UInteger
- ''' <summary>
- ''' Don't force the system to close the applications.
- ''' This is the default parameter.
- ''' </summary>
- Wait = &H0UI
- ''' <summary>
- ''' All sessions are forcefully logged off.
- ''' If this flag is not set and users other than the current user are logged on to the computer
- ''' specified by the 'lpMachineName' parameter.
- ''' </summary>
- ForceOthers = &H1UI
- ''' <summary>
- ''' Specifies that the originating session is logged off forcefully.
- ''' If this flag is not set, the originating session is shut down interactively,
- ''' so a shutdown is not guaranteed even if the function returns successfully.
- ''' </summary>
- ForceSelf = &H2UI
- End Enum
- ''' <summary>
- ''' Indicates the shutdown reason codes.
- ''' You can specify any minor reason in combination with any major reason, but some combinations do not make sense.
- ''' </summary>
- ''' For more info see here:
- ''' http://msdn.microsoft.com/en-us/library/windows/desktop/aa376885%28v=vs.85%29.aspx
- <Description("Enum used in the 'dwReason' parameter of 'ExitWindowsEx' Function.")>
- <Flags()>
- Public Enum ShutdownReason As UInteger
- ''' <summary>
- ''' Application issue.
- ''' </summary>
- MajorApplication = &H40000
- ''' <summary>
- ''' Hardware issue.
- ''' </summary>
- MajorHardware = &H10000
- ''' <summary>
- ''' The 'InitiateSystemShutdown' function was used instead of 'InitiateSystemShutdownEx'.
- ''' </summary>
- MajorLegacyApi = &H70000
- ''' <summary>
- ''' Operating system issue.
- ''' </summary>
- MajorOperatingSystem = &H20000
- ''' <summary>
- ''' Other issue.
- ''' </summary>
- MajorOther = &H0
- ''' <summary>
- ''' Power failure.
- ''' </summary>
- MajorPower = &H60000
- ''' <summary>
- ''' Software issue.
- ''' </summary>
- MajorSoftware = &H30000
- ''' <summary>
- ''' System failure..
- ''' </summary>
- MajorSystem = &H50000
- ''' <summary>
- ''' Blue screen crash event.
- ''' </summary>
- MinorBlueScreen = &HF
- ''' <summary>
- ''' Unplugged.
- ''' </summary>
- MinorCordUnplugged = &HB
- ''' <summary>
- ''' Disk.
- ''' </summary>
- MinorDisk = &H7
- ''' <summary>
- ''' Environment.
- ''' </summary>
- MinorEnvironment = &HC
- ''' <summary>
- ''' Driver.
- ''' </summary>
- MinorHardwareDriver = &HD
- ''' <summary>
- ''' Hot fix.
- ''' </summary>
- MinorHotfix = &H11
- ''' <summary>
- ''' Hot fix uninstallation.
- ''' </summary>
- MinorHotfixUninstall = &H17
- ''' <summary>
- ''' Unresponsive.
- ''' </summary>
- MinorHung = &H5
- ''' <summary>
- ''' Installation.
- ''' </summary>
- MinorInstallation = &H2
- ''' <summary>
- ''' Maintenance.
- ''' </summary>
- MinorMaintenance = &H1
- ''' <summary>
- ''' MMC issue.
- ''' </summary>
- MinorMMC = &H19
- ''' <summary>
- ''' Network connectivity.
- ''' </summary>
- MinorNetworkConnectivity = &H14
- ''' <summary>
- ''' Network card.
- ''' </summary>
- MinorNetworkCard = &H9
- ''' <summary>
- ''' Other issue.
- ''' </summary>
- MinorOther = &H0
- ''' <summary>
- ''' Other driver event.
- ''' </summary>
- MinorOtherDriver = &HE
- ''' <summary>
- ''' Power supply.
- ''' </summary>
- MinorPowerSupply = &HA
- ''' <summary>
- ''' Processor.
- ''' </summary>
- MinorProcessor = &H8
- ''' <summary>
- ''' Reconfigure.
- ''' </summary>
- MinorReconfig = &H4
- ''' <summary>
- ''' Security issue.
- ''' </summary>
- MinorSecurity = &H13
- ''' <summary>
- ''' Security patch.
- ''' </summary>
- MinorSecurityFix = &H12
- ''' <summary>
- ''' Security patch uninstallation.
- ''' </summary>
- MinorSecurityFixUninstall = &H18
- ''' <summary>
- ''' Service pack.
- ''' </summary>
- MinorServicePack = &H10
- ''' <summary>
- ''' Service pack uninstallation.
- ''' </summary>
- MinorServicePackUninstall = &H16
- ''' <summary>
- ''' Terminal Services.
- ''' </summary>
- MinorTermSrv = &H20
- ''' <summary>
- ''' Unstable.
- ''' </summary>
- MinorUnstable = &H6
- ''' <summary>
- ''' Upgrade.
- ''' </summary>
- MinorUpgrade = &H3
- ''' <summary>
- ''' WMI issue.
- ''' </summary>
- MinorWMI = &H15
- End Enum
- ''' <summary>
- ''' Indicates the shutdown reason planning.
- ''' </summary>
- ''' For more info see here:
- ''' http://msdn.microsoft.com/en-us/library/windows/desktop/aa376885%28v=vs.85%29.aspx
- <Description("Enum used in combination with the 'dwReason' parameter of 'ExitWindowsEx' and 'InitiateShutdown' Functions.")>
- Public Enum ShutdownPlanning As UInteger
- ''' <summary>
- ''' The shutdown was unplanned.
- ''' This is the default parameter.
- ''' </summary>
- Unplanned = &H0UI
- ''' <summary>
- ''' The reason code is defined by the user.
- ''' For more information, see Defining a Custom Reason Code.
- ''' If this flag is not present, the reason code is defined by the system.
- ''' </summary>
- UserDefined = &H40000000UI
- ''' <summary>
- ''' The shutdown was planned.
- ''' The system generates a System State Data (SSD) file.
- ''' This file contains system state information such as the processes, threads, memory usage, and configuration.
- ''' If this flag is not present, the shutdown was unplanned.
- ''' </summary>
- Planned = &H80000000UI
- End Enum
- ''' <summary>
- ''' The attributes of a privilege.
- ''' </summary>
- <Description("Enum used in the 'Privileges' parameter of 'TOKEN_PRIVILEGES' structure.")>
- <Flags>
- Public Enum TOKEN_PRIVILEGES_FLAGS As UInteger
- ''' <summary>
- ''' The privilege is enabled by default.
- ''' </summary>
- SE_PRIVILEGE_ENABLED_BY_DEFAULT = &H1UI
- ''' <summary>
- ''' The privilege is enabled.
- ''' </summary>
- SE_PRIVILEGE_ENABLED = &H2UI
- ''' <summary>
- ''' Used to remove a privilege.
- ''' </summary>
- SE_PRIVILEGE_REMOVED = &H4UI
- ''' <summary>
- ''' The privilege was used to gain access to an object or service.
- ''' This flag is used to identify the relevant privileges
- ''' in a set passed by a client application that may contain unnecessary privileges
- ''' </summary>
- SE_PRIVILEGE_USED_FOR_ACCESS = &H80000000UI
- End Enum
- ''' <summary>
- ''' An application cannot change the access control list of an object unless the application has the rights to do so.
- ''' These rights are controlled by a security descriptor in the access token for the object.
- ''' </summary>
- ''' For more info see here:
- ''' http://msdn.microsoft.com/en-us/library/windows/desktop/aa374905%28v=vs.85%29.aspx
- <Description("Enum used in the 'DesiredAccess' parameter of 'OpenProcessToken' Function.")>
- <Flags>
- Public Enum AccessRights As UInteger
- '******'
- ' NOTE '
- '******'
- ' This Enumeration is partially defined.
- ''' <summary>
- ''' Required to enable or disable the privileges in an access token
- ''' </summary>
- TOKEN_ADJUST_PRIVILEGES = &H32UI
- ''' <summary>
- ''' Required to query an access token
- ''' </summary>
- TOKEN_QUERY = &H8UI
- End Enum
- End Class
- #End Region
- #Region " Structures "
- ''' <summary>
- ''' Structures used by the Main Class.
- ''' </summary>
- Private Class Structures
- ''' <summary>
- ''' An 'LUID' is a 64-bit value guaranteed to be unique only on the system on which it was generated.
- ''' The uniqueness of a locally unique identifier (LUID) is guaranteed only until the system is restarted.
- ''' </summary>
- Friend Structure LUID
- ''' <summary>
- ''' The Low-order bits.
- ''' </summary>
- Public LowPart As Integer
- ''' <summary>
- ''' The High-order bits.
- ''' </summary>
- Public HighPart As Integer
- End Structure
- ''' <summary>
- ''' Represents a locally unique identifier (LUID) and its attributes.
- ''' </summary>
- Friend Structure LUID_AND_ATTRIBUTES
- ''' <summary>
- ''' Specifies an 'LUID' value.
- ''' </summary>
- Public pLuid As LUID
- ''' <summary>
- ''' Specifies attributes of the 'LUID'.
- ''' This value contains up to 32 one-bit flags.
- ''' Its meaning is dependent on the definition and use of the 'LUID'.
- ''' </summary>
- Public Attributes As Enums.TOKEN_PRIVILEGES_FLAGS
- End Structure
- ''' <summary>
- ''' Contains information about a set of privileges for an access token.
- ''' </summary>
- Friend Structure TOKEN_PRIVILEGES
- ''' <summary>
- ''' This must be set to the number of entries in the Privileges array
- ''' </summary>
- Public PrivilegeCount As Integer
- ''' <summary>
- ''' Specifies an array of 'LUID_AND_ATTRIBUTES' structures.
- ''' Each structure contains the 'LUID' and attributes of a privilege.
- ''' To get the name of the privilege associated with a 'LUID', call the 'LookupPrivilegeName' function,
- ''' passing the address of the 'LUID' as the value of the 'lpLuid' parameter.
- ''' </summary>
- Public Privileges As LUID_AND_ATTRIBUTES
- End Structure
- End Class
- #End Region
- #End Region
- #Region " Private Methods "
- ''' <summary>
- ''' Gets the necessary shutdown privileges to perform a local shutdown operation.
- ''' </summary>
- ''' <param name="Computer">
- ''' Indicates the computer where to set the privileges.
- ''' If a null string is specified, the function attempts to find the privilege name on the local system
- ''' </param>
- Private Shared Sub GetLocalShutdownPrivileges(ByVal Computer As String)
- Dim hToken As IntPtr
- Dim tkp As Structures.TOKEN_PRIVILEGES
- NativeMethods.OpenProcessToken(Process.GetCurrentProcess.Handle,
- Enums.AccessRights.TOKEN_ADJUST_PRIVILEGES Or Enums.AccessRights.TOKEN_QUERY,
- hToken)
- With tkp
- .PrivilegeCount = 1
- .Privileges.Attributes = Enums.TOKEN_PRIVILEGES_FLAGS.SE_PRIVILEGE_ENABLED
- End With
- NativeMethods.LookupPrivilegeValue(Computer,
- Privileges.SE_SHUTDOWN_NAME,
- tkp.Privileges.pLuid)
- NativeMethods.AdjustTokenPrivileges(hToken, False, tkp,
- 0UI, IntPtr.Zero, IntPtr.Zero)
- End Sub
- ''' <summary>
- ''' Gets the necessary shutdown privileges to perform a remote shutdown operation.
- ''' </summary>
- ''' <param name="Computer">
- ''' Indicates the computer where to set the privileges.
- ''' If a null string is specified, the function attempts to find the privilege name on the local system
- ''' </param>
- Private Shared Sub GetRemoteShutdownPrivileges(ByVal Computer As String)
- Dim hToken As IntPtr
- Dim tkp As Structures.TOKEN_PRIVILEGES
- NativeMethods.OpenProcessToken(Process.GetCurrentProcess.Handle,
- Enums.AccessRights.TOKEN_ADJUST_PRIVILEGES Or Enums.AccessRights.TOKEN_QUERY,
- hToken)
- With tkp
- .PrivilegeCount = 1
- .Privileges.Attributes = Enums.TOKEN_PRIVILEGES_FLAGS.SE_PRIVILEGE_ENABLED
- End With
- NativeMethods.LookupPrivilegeValue(Computer,
- Privileges.SE_REMOTE_SHUTDOWN_NAME,
- tkp.Privileges.pLuid)
- NativeMethods.AdjustTokenPrivileges(hToken, False, tkp,
- 0UI, IntPtr.Zero, IntPtr.Zero)
- End Sub
- #End Region
- #Region " Public Methods "
- ''' <summary>
- ''' Aborts a system shutdown operation that has been initiated (unless a LogOff).
- ''' </summary>
- ''' <param name="Computer">
- ''' The network name of the computer where the shutdown is to be stopped.
- ''' If this parameter is 'NULL' or an empty string, the function aborts the shutdown on the local computer.
- ''' </param>
- ''' <returns><c>True</c> if the function succeeds, <c>False</c> otherwise.</returns>
- Public Shared Function Abort(Optional ByVal Computer As String = Nothing) As Boolean
- Return NativeMethods.AbortSystemShutdown(Computer)
- End Function
- ''' <summary>
- ''' Shuts down all processes running in the logon session and then logs off the interactive user.
- ''' </summary>
- ''' <param name="Force">
- ''' Indicates whether to force the logoff.
- ''' </param>
- ''' <param name="Reason">
- ''' Indicates the reason for initiating the shutdown.
- ''' </param>
- ''' <returns>
- ''' If the function succeeds, the return value is 'True'.
- ''' The function executes asynchronously so a 'True' return value indicates that the shutdown has been initiated.
- ''' It does not indicate whether the shutdown will succeed.
- ''' It is possible that the system, the user, or another application will abort the shutdown.
- ''' If the function fails, the return value is 'False'.
- ''' </returns>
- Public Shared Function LogOff(Optional ByVal Force As Enums.ExitwindowsEx_Force =
- Enums.ExitwindowsEx_Force.Wait,
- Optional ByVal Reason As Enums.ShutdownReason =
- Enums.ShutdownReason.MajorOther) As Boolean
- GetLocalShutdownPrivileges(Nothing)
- GetRemoteShutdownPrivileges(Nothing)
- Return NativeMethods.ExitWindowsEx(Enums.ExitwindowsEx_Flags.LogOff Or Force, Reason)
- End Function
- ''' <summary>
- ''' Shutdowns the specified computer and begins powered down.
- ''' </summary>
- ''' <param name="Computer">
- ''' The name of the computer to poweroff.
- ''' If the value of this parameter is 'NULL', the local computer is shut down.
- ''' This parameter can be an addres, for example: '127.0.0.1'
- ''' </param>
- ''' <param name="Message">
- ''' The message to be displayed in the interactive poweroff dialog box.
- ''' </param>
- ''' <param name="Timeout">
- ''' The number of seconds to wait before shutting down the computer.
- ''' If the value of this parameter is zero, the computer is poweroff immediately.
- ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
- ''' If the value of this parameter is greater than zero, and the 'dwShutdownFlags' parameter
- ''' specifies the flag 'GRACE_OVERRIDE', the function fails and returns the error code 'ERROR_BAD_ARGUMENTS'.
- ''' </param>
- ''' <param name="Force">
- ''' Indicates whether to force the PowerOff.
- ''' </param>
- ''' <param name="Reason">
- ''' The reason for initiating the poweroff.
- ''' If this parameter is zero,
- ''' the default is an undefined poweroff that is logged as "No title for this reason could be found".
- ''' By default, it is also an 'unplanned' poweroff.
- ''' </param>
- ''' <param name="Planning">
- ''' Indicates whether it's a planned or unplanned PowerOff operation.
- ''' </param>
- ''' <returns>
- ''' <c>true</c> if the poweroff operation is initiated correctlly, <c>false</c> otherwise.
- ''' </returns>
- Public Shared Function PowerOff(Optional ByVal Computer As String = Nothing,
- Optional ByVal Timeout As Integer = 0,
- Optional ByVal Message As String = Nothing,
- Optional ByVal Force As Enums.InitiateShutdown_Force =
- Enums.InitiateShutdown_Force.Wait,
- Optional ByVal Reason As Enums.ShutdownReason =
- Enums.ShutdownReason.MajorOther,
- Optional ByVal Planning As Enums.ShutdownPlanning =
- Enums.ShutdownPlanning.Unplanned) As Boolean
- GetLocalShutdownPrivileges(Computer)
- GetRemoteShutdownPrivileges(Computer)
- Select Case Timeout
- Case Is = 0
- Return Not Convert.ToBoolean(
- NativeMethods.InitiateShutdown(Computer, Message, Timeout,
- Enums.InitiateShutdown_Flags.PowerOff Or
- Enums.InitiateShutdown_Flags.GraceOverride Or Force,
- Reason Or Planning))
- Case Else
- Return Not Convert.ToBoolean(
- NativeMethods.InitiateShutdown(Computer, Message, Timeout,
- Enums.InitiateShutdown_Flags.PowerOff Or Force,
- Reason Or Planning))
- End Select
- End Function
- ''' <summary>
- ''' Restarts the specified computer.
- ''' </summary>
- ''' <param name="Computer">
- ''' The name of the computer to restart.
- ''' If the value of this parameter is 'NULL', the local computer is shut down.
- ''' This parameter can be an addres, for example: '127.0.0.1'
- ''' </param>
- ''' <param name="Message">
- ''' The message to be displayed in the interactive restart dialog box.
- ''' </param>
- ''' <param name="Timeout">
- ''' The number of seconds to wait before restarting the computer.
- ''' If the value of this parameter is zero, the computer is restarted immediately.
- ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
- ''' If the value of this parameter is greater than zero, and the 'dwShutdownFlags' parameter
- ''' specifies the flag 'GRACE_OVERRIDE', the function fails and returns the error code 'ERROR_BAD_ARGUMENTS'.
- ''' </param>
- ''' <param name="Force">
- ''' Indicates whether to force the restart.
- ''' </param>
- ''' <param name="Reason">
- ''' The reason for initiating the restart.
- ''' If this parameter is zero,
- ''' the default is an undefined restart that is logged as "No title for this reason could be found".
- ''' By default, it is also an 'unplanned' restart.
- ''' </param>
- ''' <param name="Planning">
- ''' Indicates whether it's a planned or unplanned restart operation.
- ''' </param>
- ''' <returns>
- ''' <c>true</c> if the restart operation is initiated correctlly, <c>false</c> otherwise.
- ''' </returns>
- Public Shared Function Restart(Optional ByVal Computer As String = Nothing,
- Optional ByVal Timeout As Integer = 0,
- Optional ByVal Message As String = Nothing,
- Optional ByVal Force As Enums.InitiateShutdown_Force =
- Enums.InitiateShutdown_Force.Wait,
- Optional ByVal Reason As Enums.ShutdownReason =
- Enums.ShutdownReason.MajorOther,
- Optional ByVal Planning As Enums.ShutdownPlanning =
- Enums.ShutdownPlanning.Unplanned) As Boolean
- GetLocalShutdownPrivileges(Computer)
- GetRemoteShutdownPrivileges(Computer)
- Select Case Timeout
- Case Is = 0
- Return Not Convert.ToBoolean(
- NativeMethods.InitiateShutdown(Computer, Message, Timeout,
- Enums.InitiateShutdown_Flags.Restart Or
- Enums.InitiateShutdown_Flags.GraceOverride Or Force,
- Reason Or Planning))
- Case Else
- Return Not Convert.ToBoolean(
- NativeMethods.InitiateShutdown(Computer, Message, Timeout,
- Enums.InitiateShutdown_Flags.Restart Or Force,
- Reason Or Planning))
- End Select
- End Function
- ''' <summary>
- ''' Restarts the specified computer,
- ''' also restarts any applications that have been registered for restart
- ''' using the 'RegisterApplicationRestart' function.
- ''' </summary>
- ''' <param name="Computer">
- ''' The name of the computer to restart.
- ''' If the value of this parameter is 'NULL', the local computer is shut down.
- ''' This parameter can be an addres, for example: '127.0.0.1'
- ''' </param>
- ''' <param name="Message">
- ''' The message to be displayed in the interactive restart dialog box.
- ''' </param>
- ''' <param name="Timeout">
- ''' The number of seconds to wait before restarting the computer.
- ''' If the value of this parameter is zero, the computer is restarted immediately.
- ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
- ''' If the value of this parameter is greater than zero, and the 'dwShutdownFlags' parameter
- ''' specifies the flag 'GRACE_OVERRIDE', the function fails and returns the error code 'ERROR_BAD_ARGUMENTS'.
- ''' </param>
- ''' <param name="Force">
- ''' Indicates whether to force the restart.
- ''' </param>
- ''' <param name="Reason">
- ''' The reason for initiating the restart.
- ''' If this parameter is zero,
- ''' the default is an undefined restart that is logged as "No title for this reason could be found".
- ''' By default, it is also an 'unplanned' restart.
- ''' </param>
- ''' <param name="Planning">
- ''' Indicates whether it's a planned or unplanned restart operation.
- ''' </param>
- ''' <returns>
- ''' <c>true</c> if the restart operation is initiated correctlly, <c>false</c> otherwise.
- ''' </returns>
- Public Shared Function RestartApps(Optional ByVal Computer As String = Nothing,
- Optional ByVal Timeout As Integer = 0,
- Optional ByVal Message As String = Nothing,
- Optional ByVal Force As Enums.InitiateShutdown_Force =
- Enums.InitiateShutdown_Force.Wait,
- Optional ByVal Reason As Enums.ShutdownReason =
- Enums.ShutdownReason.MajorOther,
- Optional ByVal Planning As Enums.ShutdownPlanning =
- Enums.ShutdownPlanning.Unplanned) As Boolean
- GetLocalShutdownPrivileges(Computer)
- GetRemoteShutdownPrivileges(Computer)
- Select Case Timeout
- Case Is = 0
- Return Not Convert.ToBoolean(
- NativeMethods.InitiateShutdown(Computer, Message, Timeout,
- Enums.InitiateShutdown_Flags.RestartApps Or
- Enums.InitiateShutdown_Flags.GraceOverride Or Force,
- Reason Or Planning))
- Case Else
- Return Not Convert.ToBoolean(
- NativeMethods.InitiateShutdown(Computer, Message, Timeout,
- Enums.InitiateShutdown_Flags.RestartApps Or Force,
- Reason Or Planning))
- End Select
- End Function
- ''' <summary>
- ''' Shutdowns the specified computer.
- ''' </summary>
- ''' <param name="Computer">
- ''' The name of the computer to be shut down.
- ''' If the value of this parameter is 'NULL', the local computer is shut down.
- ''' This parameter can be an addres, for example: '127.0.0.1'
- ''' </param>
- ''' <param name="Message">
- ''' The message to be displayed in the interactive shutdown dialog box.
- ''' </param>
- ''' <param name="Timeout">
- ''' The number of seconds to wait before shutting down the computer.
- ''' If the value of this parameter is zero, the computer is shut down immediately.
- ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
- ''' If the value of this parameter is greater than zero, and the 'dwShutdownFlags' parameter
- ''' specifies the flag 'GRACE_OVERRIDE', the function fails and returns the error code 'ERROR_BAD_ARGUMENTS'.
- ''' </param>
- ''' <param name="Force">
- ''' Indicates whether to force the shutdown.
- ''' </param>
- ''' <param name="Reason">
- ''' The reason for initiating the shutdown.
- ''' If this parameter is zero,
- ''' the default is an undefined shutdown that is logged as "No title for this reason could be found".
- ''' By default, it is also an 'unplanned' shutdown.
- ''' </param>
- ''' <param name="Planning">
- ''' Indicates whether it's a planned or unplanned shutdoen operation.
- ''' </param>
- ''' <returns>
- ''' <c>true</c> if the shutdown operation is initiated correctlly, <c>false</c> otherwise.
- ''' </returns>
- Public Shared Function Shutdown(Optional ByVal Computer As String = Nothing,
- Optional ByVal Timeout As Integer = 0,
- Optional ByVal Message As String = Nothing,
- Optional ByVal Force As Enums.InitiateShutdown_Force =
- Enums.InitiateShutdown_Force.Wait,
- Optional ByVal Reason As Enums.ShutdownReason =
- Enums.ShutdownReason.MajorOther,
- Optional ByVal Planning As Enums.ShutdownPlanning =
- Enums.ShutdownPlanning.Unplanned) As Boolean
- GetLocalShutdownPrivileges(Computer)
- GetRemoteShutdownPrivileges(Computer)
- Select Case Timeout
- Case Is = 0
- Return Not Convert.ToBoolean(
- NativeMethods.InitiateShutdown(Computer, Message, Timeout,
- Enums.InitiateShutdown_Flags.Shutdown Or
- Enums.InitiateShutdown_Flags.GraceOverride Or Force,
- Reason Or Planning))
- Case Else
- Return Not Convert.ToBoolean(
- NativeMethods.InitiateShutdown(Computer, Message, Timeout,
- Enums.InitiateShutdown_Flags.Shutdown Or Force,
- Reason Or Planning))
- End Select
- End Function
- ''' <summary>
- ''' Use this function only for Windows 8/8.1
- ''' Shutdowns the specified computer and prepares the system for a faster startup.
- ''' </summary>
- ''' <param name="Computer">
- ''' The name of the computer to be shut down.
- ''' If the value of this parameter is 'NULL', the local computer is shut down.
- ''' This parameter can be an addres, for example: '127.0.0.1'
- ''' </param>
- ''' <param name="Message">
- ''' The message to be displayed in the interactive shutdown dialog box.
- ''' </param>
- ''' <param name="Timeout">
- ''' The number of seconds to wait before shutting down the computer.
- ''' If the value of this parameter is zero, the computer is shut down immediately.
- ''' This value is limited to 'MAX_SHUTDOWN_TIMEOUT'.
- ''' If the value of this parameter is greater than zero, and the 'dwShutdownFlags' parameter
- ''' specifies the flag 'GRACE_OVERRIDE', the function fails and returns the error code 'ERROR_BAD_ARGUMENTS'.
- ''' </param>
- ''' <param name="Force">
- ''' Indicates whether to force the shutdown.
- ''' </param>
- ''' <param name="Reason">
- ''' The reason for initiating the shutdown.
- ''' If this parameter is zero,
- ''' the default is an undefined shutdown that is logged as "No title for this reason could be found".
- ''' By default, it is also an 'unplanned' shutdown.
- ''' </param>
- ''' <param name="Planning">
- ''' Indicates whether it's a planned or unplanned shutdoen operation.
- ''' </param>
- ''' <returns>
- ''' <c>true</c> if the shutdown operation is initiated correctlly, <c>false</c> otherwise.
- ''' </returns>
- Public Shared Function HybridShutdown(Optional ByVal Computer As String = Nothing,
- Optional ByVal Timeout As Integer = 0,
- Optional ByVal Message As String = Nothing,
- Optional ByVal Force As Enums.InitiateShutdown_Force =
- Enums.InitiateShutdown_Force.Wait,
- Optional ByVal Reason As Enums.ShutdownReason =
- Enums.ShutdownReason.MajorOther,
- Optional ByVal Planning As Enums.ShutdownPlanning =
- Enums.ShutdownPlanning.Unplanned) As Boolean
- GetLocalShutdownPrivileges(Computer)
- GetRemoteShutdownPrivileges(Computer)
- Select Case Timeout
- Case Is = 0
- Return Not Convert.ToBoolean(
- NativeMethods.InitiateShutdown(Computer, Message, Timeout,
- Enums.InitiateShutdown_Flags.Shutdown Or
- Enums.InitiateShutdown_Flags.HybridShutdown Or
- Enums.InitiateShutdown_Flags.GraceOverride Or Force,
- Reason Or Planning))
- Case Else
- Return Not Convert.ToBoolean(
- NativeMethods.InitiateShutdown(Computer, Message, Timeout,
- Enums.InitiateShutdown_Flags.Shutdown Or
- Enums.InitiateShutdown_Flags.HybridShutdown Or Force,
- Reason Or Planning))
- End Select
- End Function
- #End Region
- End Class
- #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement