In short, you have to call several windows APIs.
CODE
[COLOR=green]' Constants[/COLOR]
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"
CODE
[COLOR=green]' Exit Windows Constants[/COLOR]
Const EWX_LOGOFF As Integer = &H0
Const EWX_SHUTDOWN As Integer = &H1
Const EWX_REBOOT As Integer = &H2
Const EWX_FORCE As Integer = &H4
Const EWX_POWEROFF As Integer = &H8
Const EWX_FORCEIFHUNG As Integer = &H10
CODE
[COLOR=green]'Structure
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Friend Structure Luid
Public Count As Integer
Public Luid As Long
Public Attr As Integer
End Structure 'TokPriv1Luid
CODE
[COLOR=green]' Get Current Processes.
<DllImport("kernel32.dll", ExactSpelling:=True)> _
Function GetCurrentProcess() As IntPtr
End Function
CODE
[CODE][COLOR=green]' Open Process Token.
<DllImport("advapi32.dll", SetLastError:=True)> _
Function OpenProcessToken(ByVal h As IntPtr, ByVal acc As Integer, ByRef phtok As IntPtr) As Boolean
End Function
CODE
[COLOR=green]' Look up Priviledge Value.
<DllImport("advapi32.dll", SetLastError:=True)> _
Friend Function LookupPrivilegeValue(ByVal host As String, ByVal name As String, ByRef pluid As Long) As Boolean
End Function
CODE
[COLOR=green]' Adjust Token Priviledges.[/COLOR]
<DllImport("advapi32.dll", ExactSpelling:=True, SetLastError:=True)> _
Friend Function AdjustTokenPrivileges(ByVal htok As IntPtr, ByVal disall As Boolean, ByRef newst As Luid, ByVal len As Integer, ByVal prev As IntPtr, ByVal relen As IntPtr) As Boolean
End Function
CODE
[COLOR=green]' Exit Windows[/COLOR]
<DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)> _
Friend Function ExitWindowsEx(ByVal flg As Integer, ByVal rea As Integer) As Boolean
End Function
CODE
[COLOR=green]' Exit Windows Sub[/COLOR]
Private Sub DoExitWindows(ByVal flg As Integer)
Dim tp As Luid
Dim hproc As IntPtr = GetCurrentProcess()
Dim htok As IntPtr = IntPtr.Zero
CODE
[COLOR=green] 'Get a token for this process. [/COLOR]
OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htok)
tp.Count = 1
tp.Luid = 0
tp.Attr = SE_PRIVILEGE_ENABLED
CODE
[COLOR=green] 'Get the LUID for the shutdown privilege.[/COLOR]
LookupPrivilegeValue(Nothing, SE_SHUTDOWN_NAME, tp.Luid)
CODE
[COLOR=green] 'Get the shutdown privilege for this process.[/COLOR]
AdjustTokenPrivileges(htok, False, tp, 0, IntPtr.Zero, IntPtr.Zero)
CODE
[COLOR=green] 'Exit Windows[/COLOR]
ExitWindowsEx(flg, 0)
End Sub
CODE
[COLOR=green]' Shutdown[/COLOR]
DoExitWindows(EWX_SHUTDOWN)
[COLOR=green]' Restart[/COLOR]
DoExitWindows(EWX_REBOOT Or EWX_FORCE)
[COLOR=green]' Log off[/COLOR]
DoExitWindows(EWX_LOGOFF)
[/CODE]
A site that really describes this process is
http://blogs.msdn.com/brad_mccabe/archive/.../02/383542.aspx
Comment/Reply (w/o sign-up)