Får ej API't ExitWindowsEx att fungera. Jag kör windows 2000. Ett tips hämta Api guiden från "http://www.allapi.net/agnet/apiguide.php" och titta på exemplen. Krävs SeShutdownPrivilege privilegier hos ProcessToken.ExitWindowsEx
Modul:
Public Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
Public Const EWX_FORCE = 4 'Force any applications to quit instead of prompting the user to close them.
Public Const EWX_LOGOFF = 0 'Log off the network.
Public Const EWX_POWEROFF = 8 'Shut down the system and, if possible, turn the computer off.
Public Const EWX_REBOOT = 2 'Perform a full reboot of the system.
Public Const EWX_SHUTDOWN = 1 'Shut down the system
Form1:
Private Sub Command2_Click()
Dim retval As Long ' return value
retval = ExitWindowsEx(EWX_REBOOT, 0)
If retval = 0 Then Debug.Print "Reboot attempt failed."
End Sub
Någon som har en ide?
MVH
indurainSv: ExitWindowsEx
Enligt den enablar man shutdown i WinNt innan kommandot körs
\Leif PSv: ExitWindowsEx
Här är en kapslad funktion:
'Module: Module1
Option Explicit
Private Const EWX_LOGOFF = 0 'Log off the network.
Private Const EWX_SHUTDOWN = 1 'Shut down the system
Private Const EWX_REBOOT = 2 'Perform a full reboot of the system.
Private Const EWX_FORCE = 4 'Force any applications to quit instead of prompting the user to close them.
Private Const EWX_POWEROFF = 8 'Shut down the system and, if possible, turn the computer off.
Private Const TOKEN_QUERY = &H8
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
LUID As LARGE_INTEGER
Attributes As Long
End Type
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LARGE_INTEGER) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Public Enum ExitWindowsParameter
ewLogOff = EWX_LOGOFF
ewShutDown = EWX_SHUTDOWN
ewReBoot = EWX_REBOOT
ewForce = EWX_FORCE
ewPowerOff = EWX_POWEROFF
End Enum
Public Function ExitWindows(Optional Parameter As ExitWindowsParameter = ewShutDown) As Boolean
Dim hProcess As Long
Dim hToken As Long
Dim lReturn As Long
Dim tmpLuid As LARGE_INTEGER
Dim NewState As TOKEN_PRIVILEGES
Dim PreviousState As TOKEN_PRIVILEGES
Dim ReturnLength As Long
hProcess = GetCurrentProcess()
If hProcess Then
lReturn = OpenProcessToken(hProcess, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hToken)
If lReturn Then
LookupPrivilegeValue vbNullString, "SeShutdownPrivilege", tmpLuid
With NewState
.PrivilegeCount = 1
.LUID = tmpLuid
.Attributes = SE_PRIVILEGE_ENABLED
End With
lReturn = AdjustTokenPrivileges(hToken, False, NewState, Len(NewState), PreviousState, ReturnLength)
lReturn = ExitWindowsEx(Parameter, 0&)
ExitWindows = (lReturn <> 0)
End If
End If
End Function
Används på följande sätt:
'Form: Form1
Option Explicit
Private Sub Command1_Click()
ExitWindows ewReBoot
End Sub