Newsgroup: it.comp.lang.visual-basic
Titolo: ShutDown di Windows NT 4.0

Ciao a tutti,
ho bisogno di comandare lo ShutDown (in particolare il Reboot forzato) di
Windows NT e per far ciň ho utilizzato l'API ExitWindowsEx di User32.Dll.
Se l'applicazione gira sotto Windows '95 non ho alcun problema ma sotto
ambiente Windows NT 4.0 WS mi č permesso soltanto il LogOff ed il LogOff
forzato.
La funzione che uso č:
Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal
dwReserved As Long) As Long
e utilizzo uFlags = EWX_REBOOT + EWX_FORCE (reboot forzato).
NB: sotto NT ho usato tutte le combinazioni dei valori di uFlags e gli unici
che danno un esito positivo sono EWX_LOGOFF e EWX_LOGOFF + EWX_FORCE.
Spero che qualcuno possa darmi una mano :-))
Maurizio L.

 wrote:
Posso solo dirti che prima di chiamare la ExitWindowsEx in NT devi
autoassegnare al processo i privilegi per farlo (non so come si fa!).



Prova il codice che riporto sotto.
(semplicemente, in NT devi settare i privilegi prima di richiedere lo
shut-down)
--
Roal Zanazzi
z...@iol.it
with this code you can reboot, shutdown and/or logoff your Windows NT 4.0,
if you want to use with 95 just add a function to test
the OS and in W95 don't call the AdjustToken().
'=========================DECLARE.BAS===================================
Option Explicit
Public Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_REBOOT = 2
Public Const EWX_FORCE = 4
Public Const TOKEN_QUERY = &H8
Public Const TOKEN_ADJUST_PRIVILEGES = &H20
Public Const SE_PRIVILEGE_ENABLED = &H2
Public Type LUID
    UsedPart As Long
    IgnoredForNowHigh32BitPart As Long
End Type
Public Type TOKEN_PRIVILEGES
    PrivilegeCount As Long
    TheLuid As LUID
    Attributes As Long
End Type
Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal
dwReserved As Long) As Long
Declare Function LookupPrivilegeValue Lib "advapi32" Alias _
    "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As
String, _
    lpLuid As LUID) As Long
Declare Function GetCurrentProcess Lib "kernel32" () As Long
Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As
Long, _
    ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Declare Function AdjustTokenPrivileges Lib "advapi32" (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
'====================REBOOT=================================================
=
'
' Codice di esempio per il reboot, logoff e riavvio di un sistema Windows NT
'
Option Explicit
Private Sub Logoff_Click()
    AdjustToken
    Call ExitWindowsEx(EWX_LOGOFF Or EWX_FORCE, 0)
End Sub
Private Sub Reboot_Click()
    AdjustToken
    Call ExitWindowsEx(EWX_REBOOT Or EWX_FORCE, 0)
End Sub
Private Sub Shutdown_Click()
    AdjustToken
    Call ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, 0)
End Sub
'
'  Taken from MSDN
'
Private Sub AdjustToken()
    Dim hdlProcessHandle As Long
    Dim hdlTokenHandle As Long
    Dim lBufferNeeded As Long
    Dim tmpLuid As LUID
    Dim tkp As TOKEN_PRIVILEGES, tkpNewButIgnored As TOKEN_PRIVILEGES
    hdlProcessHandle = GetCurrentProcess()
    OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or
TOKEN_QUERY), hdlTokenHandle
    LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
    tkp.PrivilegeCount = 1
    tkp.TheLuid = tmpLuid
    tkp.Attributes = SE_PRIVILEGE_ENABLED
    AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored),
tkpNewButIgnored, lBufferNeeded
End Sub