Rebooting the System

You read the old sample of shutting down Windows ??
Sure it works but I tried it with Vista and it doesn't !!
Bref, we don't have permission
So I made a search again, and let's fight to get this permission !

The Windows Class
Use the following to Shut, Reboot and logoff
Windows.ExitViaShutdown()
Windows.ExitViaReboot( )
Windows.ExitViaLogoff( )

Public Class windows
' ----- Windows constants used in shutdown permissions.
Const SE_PRIVILEGE_ENABLED As Integer = &H2
Const TOKEN_QUERY As Integer = &H8
Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"

' ----- Shutdown method flags.
Private Enum ShutdownMethods As Integer
Logoff = 0
Shutdown = 1
Reboot = 6
End Enum

_
Private Structure TokenPrivileges
Public PrivilegeCount As Integer
Public Luid As Long
Public Attributes As Integer
End Structure

' ----- External features needed to exit Windows.
Private Declare Ansi Function AdjustTokenPrivileges _
Lib "advapi32.dll" _
(ByVal tokenHandle As IntPtr, _
ByVal disableAllPrivileges As Boolean, _
ByRef newState As TokenPrivileges, _
ByVal bufferLength As Integer, _
ByVal previousState As IntPtr, _
ByVal returnLength As IntPtr) As Boolean

Private Declare Ansi Function ExitWindowsEx _
Lib "user32.dll" _
(ByVal flags As Integer, _
ByVal reason As Integer) As Boolean

Private Declare Ansi Function GetCurrentProcess _
Lib "kernel32.dll" ( ) As IntPtr

Private Declare Ansi Sub LockWorkStation _
Lib "user32.dll" ( )

Private Declare Ansi Function LookupPrivilegeValueA _
Lib "advapi32.dll" _
(ByVal
systemName As String, _
ByVal privilegeName As String, _
ByRef lookupID As Long) As Boolean

Private Declare Ansi Function OpenProcessToken _
Lib "advapi32.dll" _
(ByVal processHandle As IntPtr, _
ByVal desiredAccess As Integer, _
ByRef tokenHandle As IntPtr) As Boolean

Private Shared Sub PerformExit( _
ByVal usingMethod As Integer)
' ----- Log off, reboot, or shut down the
system.
Dim shutdownPrivileges As TokenPrivileges
Dim processHandle As IntPtr
Dim tokenHandle As IntPtr = IntPtr.Zero

' ----- Give ourselves the privilege of shutting
' down the system. First, obtain the token.
processHandle = GetCurrentProcess( )
OpenProcessToken(processHandle, _
TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, tokenHandle)

' ----- Adjust the token to enable shutdown permissions.
shutdownPrivileges.PrivilegeCount = 1
shutdownPrivileges.Luid = 0
shutdownPrivileges.Attributes = SE_PRIVILEGE_ENABLED
LookupPrivilegeValueA(Nothing, SE_SHUTDOWN_NAME, _
shutdownPrivileges.Luid)
AdjustTokenPrivileges(tokenHandle, False, _
shutdownPrivileges, 0, IntPtr.Zero, IntPtr.Zero)

' ----- Now shut down the system.
ExitWindowsEx(usingMethod, 0)
End Sub

Public Shared Sub ExitViaLockWorkstation( )
' ----- Lock the workstation.
LockWorkStation( )
End Sub

Public Shared Sub ExitViaLogoff( )
' ----- Log off the current user.
PerformExit(ShutdownMethods.Logoff)
End Sub

Public Shared Sub ExitViaReboot( )
' ----- Reboot the system.
PerformExit(ShutdownMethods.Reboot)
End Sub

Public Shared Sub ExitViaShutdown( )
' ----- Shut down the system.
PerformExit(ShutdownMethods.Shutdown)
End Sub
End Class

0 comments:

Post a Comment