Public Class WindowsController
    <DllImport("advapi32", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
    Private Shared Function AdjustTokenPrivileges(ByVal TokenHandle As IntPtr, ByVal DisableAllPrivileges As Integer, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Integer, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Integer) As Integer
    End Function
    Protected Shared Function CheckEntryPoint(ByVal library As String, ByVal method As String) As Boolean
        Dim hModule As IntPtr = WindowsController.LoadLibrary((library))
        If Not hModule.Equals(IntPtr.Zero) Then
            If Not WindowsController.GetProcAddress(hModule, (method)).Equals(IntPtr.Zero) Then
                WindowsController.FreeLibrary(hModule)
                Return True
            End If
            WindowsController.FreeLibrary(hModule)
        End If
        Return False
    End Function
    Protected Shared Sub EnableToken(ByVal privilege As String)
        If ((Environment.OSVersion.Platform = PlatformID.Win32NT) AndAlso WindowsController.CheckEntryPoint("advapi32.dll", "AdjustTokenPrivileges")) Then
            Dim token_privileges As TOKEN_PRIVILEGES
            Dim luid As LUID
            Dim ptr As IntPtr
            Dim token_privileges2 As TOKEN_PRIVILEGES
            If (WindowsController.OpenProcessToken(Process.GetCurrentProcess.Handle, 40, (ptr)) = 0) Then
                Throw New PrivilegeException(WindowsController.FormatError(Marshal.GetLastWin32Error))
            End If
            Dim lpSystemName As String = ""
            If (WindowsController.LookupPrivilegeValue((lpSystemName), (privilege), (luid)) = 0) Then
                Throw New PrivilegeException(WindowsController.FormatError(Marshal.GetLastWin32Error))
            End If
            token_privileges2.PrivilegeCount = 1
            token_privileges2.Privileges.Attributes = 2
            token_privileges2.Privileges.pLuid = luid
            Dim returnLength As Integer = (4 + (12 * token_privileges.PrivilegeCount))
            If (WindowsController.AdjustTokenPrivileges(ptr, 0, (token_privileges2), (4 + (12 * token_privileges2.PrivilegeCount)), (token_privileges), (returnLength)) = 0) Then
                Throw New PrivilegeException(WindowsController.FormatError(Marshal.GetLastWin32Error))
            End If
        End If
    End Sub
    Public Shared Sub ExitWindows(ByVal how As RestartOptions, ByVal force As Boolean)
        Select Case how
            Case RestartOptions.Hibernate
                WindowsController.SuspendSystem(True, force)
                Exit Select
            Case RestartOptions.Suspend
                WindowsController.SuspendSystem(False, force)
                Exit Select
            Case Else
                WindowsController.ExitWindows(CInt(how), force)
                Exit Select
        End Select
    End Sub
    Protected Shared Sub ExitWindows(ByVal how As Integer, ByVal force As Boolean)
        WindowsController.EnableToken("SeShutdownPrivilege")
        If force Then
            how = (how Or 4)
        End If
        If (WindowsController.ExitWindowsEx(how, 0) = 0) Then
            Throw New PrivilegeException(WindowsController.FormatError(Marshal.GetLastWin32Error))
        End If
    End Sub
    <DllImport("user32", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
    Private Shared Function ExitWindowsEx(ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer
    End Function
    Protected Shared Function FormatError(ByVal number As Integer) As String
        Dim str As String
        Try 
            Dim lpBuffer As New StringBuilder(&HFF)
            WindowsController.FormatMessage(&H1000, IntPtr.Zero, number, 0, lpBuffer, lpBuffer.Capacity, 0)
            str = lpBuffer.ToString
        Catch exception1 As Exception
            ProjectData.SetProjectError(exception1)
            Dim exception As Exception = exception1
            str = ("Unspecified error [" & number.ToString & "]")
            ProjectData.ClearProjectError
        End Try
        Return str
    End Function
    <DllImport("kernel32", EntryPoint:="FormatMessageA", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
    Private Shared Function FormatMessage(ByVal dwFlags As Integer, ByVal lpSource As IntPtr, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByVal lpBuffer As StringBuilder, ByVal nSize As Integer, ByVal Arguments As Integer) As Integer
    End Function
    <DllImport("kernel32", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
    Private Shared Function FreeLibrary(ByVal hLibModule As IntPtr) As Integer
    End Function
    <DllImport("kernel32", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
    Private Shared Function GetProcAddress(ByVal hModule As IntPtr, <MarshalAs(UnmanagedType.VBByRefStr)> ByRef lpProcName As String) As IntPtr
    End Function
    <DllImport("kernel32", EntryPoint:="LoadLibraryA", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
    Private Shared Function LoadLibrary(<MarshalAs(UnmanagedType.VBByRefStr)> ByRef lpLibFileName As String) As IntPtr
    End Function
    <DllImport("advapi32", EntryPoint:="LookupPrivilegeValueA", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
    Private Shared Function LookupPrivilegeValue(<MarshalAs(UnmanagedType.VBByRefStr)> ByRef lpSystemName As String, <MarshalAs(UnmanagedType.VBByRefStr)> ByRef lpName As String, ByRef lpLuid As LUID) As Integer
    End Function
    <DllImport("advapi32", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
    Private Shared Function OpenProcessToken(ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Integer, ByRef TokenHandle As IntPtr) As Integer
    End Function
    <DllImport("powrprof", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
    Private Shared Function SetSuspendState(ByVal Hibernate As Integer, ByVal ForceCritical As Integer, ByVal DisableWakeEvent As Integer) As Integer
    End Function
    Protected Shared Sub SuspendSystem(ByVal hibernate As Boolean, ByVal force As Boolean)
        If Not WindowsController.CheckEntryPoint("powrprof.dll", "SetSuspendState") Then
            Throw New PlatformNotSupportedException("The SetSuspendState method is not supported on this system!")
        End If
        WindowsController.SetSuspendState(Conversions.ToInteger(Interaction.IIf(hibernate, 1, 0)), Conversions.ToInteger(Interaction.IIf(force, 1, 0)), 0)
    End Sub
    Private Const EWX_FORCE As Integer = 4
    Private Const FORMAT_MESSAGE_FROM_SYSTEM As Integer = &H1000
    Private Const SE_PRIVILEGE_ENABLED As Integer = 2
    Private Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
    Private Const TOKEN_QUERY As Integer = 8
End Class