1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
Imports System.Runtime.InteropServices
Module connexion1
Private Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
Private Const TOKEN_QUERY As Integer = &H8
Private Const SE_PRIVILEGE_ENABLED As Integer = &H2
Private Const EWX_LOGOFF As Integer = &H0
Private Const EWX_SHUTDOWN As Integer = &H1
Private Const EWX_REBOOT As Integer = &H2
Private Const EWX_FORCE As Integer = &H4
Private Const EWX_POWEROFF As Integer = &H8
Private Const EWX_FORCEIFHUNG As Integer = &H10 '2000/XP only
Private Structure LUID
Dim dwLowPart As Integer
Dim dwHighPart As Integer
End Structure
Private Structure LUID_AND_ATTRIBUTES
Dim udtLUID As LUID
Dim dwAttributes As Integer
End Structure
Private Structure TOKEN_PRIVILEGES
Dim PrivilegeCount As Integer
Dim laa As LUID_AND_ATTRIBUTES
End Structure
Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Integer, ByVal dwReserved As Integer) As Integer
Private Declare Function GetCurrentProcess Lib "kernel32" () As Integer
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Integer, ByVal DesiredAccess As Integer, ByVal TokenHandle As Integer) As Integer
Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByVal lpLuid As LUID) As Integer
Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Integer, ByVal DisableAllPrivileges As Integer, ByVal NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Integer, ByVal PreviousState As Integer, ByVal ReturnLength As Integer) As Integer
Private Function EnableShutdownPrivledges() As Boolean
Dim hProcessHandle As Integer
Dim hTokenHandle As Integer
Dim lpv_la As LUID
Dim token As TOKEN_PRIVILEGES
hProcessHandle = GetCurrentProcess()
If hProcessHandle <> 0 Then
If OpenProcessToken(hProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hTokenHandle) <> 0 Then
If LookupPrivilegeValue(vbNullString, "SeShutdownPrivilege", lpv_la) <> 0 Then
With token
.PrivilegeCount = 1
.laa.udtLUID = lpv_la
.laa.dwAttributes = SE_PRIVILEGE_ENABLED
End With
If AdjustTokenPrivileges(hTokenHandle, False, token, 0&, 0&, 0&) <> 0 Then
EnableShutdownPrivledges = True
End If
End If
End If
End If
End Function
Public Sub ReBoot()
EnableShutdownPrivledges()
ExitWindowsEx(EWX_REBOOT Or EWX_FORCE, &HFFFF)
End Sub
Public Sub ShutDown()
EnableShutdownPrivledges()
ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, &HFFFF)
End Sub
Public Sub LogOff()
EnableShutdownPrivledges()
ExitWindowsEx(EWX_LOGOFF Or EWX_FORCE, &HFFFF)
End Sub
End Module |
Partager