Private Declare Function GetCurrentProcess Lib "kernel32.dll" () As IntPtr
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Int32, ByRef TokenHandle As IntPtr) As Int32
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Int32
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As IntPtr, ByVal DisableAllPrivileges As Int32, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Int32, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Int32) As Int32
Private Declare Function ExitWindowsEx Lib "user32.dll" (ByVal uFlags As Int32, ByVal dwReserved As Int32) As Int32
Private Const EWX_FORCE As Int32 = 4
Private Const EWX_SHUTDOWN As Int32 = 1
Private Const EWX_REBOOT As Int32 = 2
Private Const EWX_LOGOFF As Int32 = 0
Public Structure LUID
Dim LowPart As Int32
Dim HighPart As Int32
End Structure
Public Structure TOKEN_PRIVILEGES
Public PrivilegeCount As Integer
Public Privileges As LUID
Public Attributes As Int32
End Structure
Private Sub btn_ShutDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ShutDown.Click
ShutDown()
End Sub
Private Sub btn_Restart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Restart.Click
Restart()
End Sub
Private Sub Restart()
If MessageBox.Show("Would you like to re-boot the system", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
Dim platform As New PlatformID
Select Case Environment.OSVersion.Platform
Case PlatformID.Win32NT
Dim token As TOKEN_PRIVILEGES
Dim blank_token As TOKEN_PRIVILEGES
Dim token_handle As IntPtr
Dim uid As LUID
Dim ret_length As Integer
Dim ptr As IntPtr = GetCurrentProcess() /// get the process handle
OpenProcessToken(ptr, &H20 Or &H8, token_handle)
LookupPrivilegeValue("", "SeShutdownPrivilege", uid)
token.PrivilegeCount = 1
token.Privileges = uid
token.Attributes = &H2
AdjustTokenPrivileges(token_handle, False, token, System.Runtime.InteropServices.Marshal.SizeOf(blan k_token), blank_token, ret_length)
ExitWindowsEx(EWX_LOGOFF Or EWX_FORCE Or EWX_REBOOT, &HFFFF)
Case Else
ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT, &HFFFF)
End Select
End If
End Sub
Private Sub ShutDown()
If MessageBox.Show("Would you like to shut down the system", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
Dim platform As New PlatformID
Select Case Environment.OSVersion.Platform
Case PlatformID.Win32NT
Dim token As TOKEN_PRIVILEGES
Dim blank_token As TOKEN_PRIVILEGES
Dim token_handle As IntPtr
Dim uid As LUID
Dim ret_length As Integer
Dim ptr As IntPtr = GetCurrentProcess() /// get the process handle
OpenProcessToken(ptr, &H20 Or &H8, token_handle)
LookupPrivilegeValue("", "SeShutdownPrivilege", uid)
token.PrivilegeCount = 1
token.Privileges = uid
token.Attributes = &H2
AdjustTokenPrivileges(token_handle, 0, token, System.Runtime.InteropServices.Marshal.SizeOf(blan k_token), blank_token, ret_length)
ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, &HFFFF)
Case Else
ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, &HFFFF)
End Select
End If
End Sub