Ramzes » 20.04.2003 (Вс) 11:33
Да это все хорошо работает но если тебе надо чтоб код перезагружался без всяких там окон то держы...
Для формы[/code]
Private Sub mnuCompOff_Click()
Call ShutDown(EWX_SHUTDOWN)
End Sub
Private Sub mnuCompReboot_Click()
Call ShutDown(EWX_REBOOT)
End Sub
'После этого надо создать модуль с таким кодом:
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
Type LUID
lowPart As Long
HighPart As Long
End Type
Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As PrivilegeAttributes
End Type
Type PTOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(0) As LUID_AND_ATTRIBUTES
End Type
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(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 = TOKEN_ADJUST_PRIVILEGES
lngReturn = OpenProcessToken(lngProcess, lngTokenPrivileges, lngToken)
lngReturn = LookupPrivilegeValueA(vbNullString, "SE_SHUTDOWN_NAME", udtLUID)
udtTokenPrivNew.PrivilegeCount = 1
udtTokenPrivNew.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
udtTokenPrivNew.Privileges(0).pLuid = udtLUID
lngReturn = AdjustTokenPrivileges(lngToken, 0, udtTokenPrivNew, 0&, 0, 0&)
ShutDown = ExitWindowsEx(Operation, 0)
End Function
'Я проверял должно работать