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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
| Public Class Form1
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const READ_CONTROL = &H20000
Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Private Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Private Const STANDARD_RIGHTS_EXECUTE = (READ_CONTROL)
Public Enum TokenRights
TOKEN_ASSIGN_PRIMARY = &H1
TOKEN_DUPLICATE = &H2
TOKEN_IMPERSONATE = &H4
TOKEN_QUERY = &H8
TOKEN_QUERY_SOURCE = &H10
TOKEN_ADJUST_PRIVILEGES = &H20
TOKEN_ADJUST_GROUPS = &H40
TOKEN_ADJUST_DEFAULT = &H80
TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or TOKEN_ASSIGN_PRIMARY Or TOKEN_DUPLICATE Or TOKEN_IMPERSONATE Or TOKEN_QUERY Or TOKEN_QUERY_SOURCE Or TOKEN_ADJUST_PRIVILEGES Or TOKEN_ADJUST_GROUPS Or TOKEN_ADJUST_DEFAULT)
TOKEN_READ = (STANDARD_RIGHTS_READ Or TOKEN_QUERY)
TOKEN_WRITE = (STANDARD_RIGHTS_WRITE Or TOKEN_ADJUST_PRIVILEGES Or TOKEN_ADJUST_GROUPS Or TOKEN_ADJUST_DEFAULT)
TOKEN_EXECUTE = (STANDARD_RIGHTS_EXECUTE)
End Enum
Public Enum PrivilegeAttributes
SE_PRIVILEGE_ENABLED_BY_DEFAULT = &H1
SE_PRIVILEGE_ENABLED = &H2
SE_PRIVILEGE_USED_FOR_ACCESS = &H80000000
End Enum
Public Enum ExitOptions
EWX_LOGOFF = 0
EWX_SHUTDOWN = 1
EWX_REBOOT = 2
EWX_FORCE = 4
End Enum
Public Enum TokenAccess
TokenUser = 1
TokenGroups = 2
TokenPrivileges = 3
TokenOwner = 4
TokenPrimaryGroup = 5
TokenDefaultDacl = 6
TokenType = 8
TokenImpersonationLevel = 9
TokenStatistics = 10
End Enum
Private Structure LUID
Public lowPart As Int32
Public HighPart As Int32
End Structure
Private Structure LUID_AND_ATTRIBUTES
Public pLuid As LUID
Public Attributes As PrivilegeAttributes
End Structure
Private Structure PTOKEN_PRIVILEGES
Public PrivilegeCount As Int32
Public Privileges As LUID_AND_ATTRIBUTES
End Structure
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As TokenRights, ByRef TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValueA Lib "advapi32" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, ByRef NewState As PTOKEN_PRIVILEGES, ByVal BufferLength As Long, ByRef PreviousState As Long, ByRef ReturnLenght As Long) As Long
Private Declare Function AdjustTokenPrivilegesOld Lib "advapi32" Alias "AdjustTokenPrivileges" (ByVal TokenHandle As Long, ByVal disableAllPrivileges As Long, ByRef NewState As PTOKEN_PRIVILEGES, ByVal BufferLength As Long, ByRef PreviousState As PTOKEN_PRIVILEGES, ByRef ReturnLenght As Long) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As ExitOptions, ByVal dwReserved As Long) As Long
Public Function ShutDown(ByVal Operation As ExitOptions) As Long
Dim lngProcess As Long
Dim lngReturn As Long
Dim lngToken As Long
Dim udtLUID As LUID
Dim lngTokenPrivileges As TokenRights
Dim udtTokenPrivNew As PTOKEN_PRIVILEGES
lngProcess = GetCurrentProcess()
lngTokenPrivileges = TokenRights.TOKEN_ADJUST_PRIVILEGES
lngReturn = OpenProcessToken(lngProcess, lngTokenPrivileges, lngToken)
lngReturn = LookupPrivilegeValueA(vbNullString, "SE_SHUTDOWN_NAME", udtLUID)
udtTokenPrivNew.PrivilegeCount = 1
udtTokenPrivNew.Privileges.Attributes = PrivilegeAttributes.SE_PRIVILEGE_ENABLED
udtTokenPrivNew.Privileges.pLuid = udtLUID
lngReturn = AdjustTokenPrivileges(lngToken, 0, udtTokenPrivNew, 0&, 0, 0&)
ShutDown = ExitWindowsEx(Operation, 0)
End Function
Dim i As Integer = 2 ' сюда пишим нужную цифру
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Select Case i
Case 0 '"Автоматическое выключение компьютера"
Me.ShutDown(ExitOptions.EWX_SHUTDOWN)
Case 1 ' "Автоматическая перезагрузка компьютера"
Me.ShutDown(ExitOptions.EWX_REBOOT)
Case 2 '"Автоматический выход из учётной записи"
Me.ShutDown(ExitOptions.EWX_LOGOFF)
Case 3 '"Быстрое выключение"
Me.ShutDown(ExitOptions.EWX_FORCE)
End Select
End Sub
End Class |