Newsgroup: it.comp.lang.visual-basic
Titolo: Shutdown win2000

 Come faccio a chiuderlo?
In WinNT e Win2000 devi prima ottenere i diritti di spegnimento
Ti incollo un esempio (fa solo lo shutdown, ma con poche modifiche
può fare tutto il resto).
In ufficio ho creato, partendo da questo esempio, un modulo
completo per la gestione dello shutdown (in tutti i Win), ma
purtroppo non l'ho mai portato a casa.
Se puoi aspettare lunedì, lo posterò qui.
===========================================
Option Explicit
    Private Type LUID
        UsedPart As Long
        IgnoredForNowHigh32BitPart As Long
    End Type
    Private Type TOKEN_PRIVILEGES
        PrivilegeCount As Long
        TheLuid As LUID
        Attributes As Long
    End Type
    Private Const EWX_SHUTDOWN As Long = 1
    Private Const EWX_FORCE As Long = 4
    Private Const EWX_REBOOT = 2
    Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As
Long, ByVal dwReserved As Long) As Long
    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" Alias
"LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As
String, lpLuid As LUID) As Long
    Private 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
Public Sub ShutDownWindows()
    Const TOKEN_ADJUST_PRIVILEGES = &H20
    Const TOKEN_QUERY = &H8
    Const SE_PRIVILEGE_ENABLED = &H2
    Dim hdlProcessHandle As Long
    Dim hdlTokenHandle As Long
    Dim tmpLuid As LUID
    Dim tkp As TOKEN_PRIVILEGES
    Dim tkpNewButIgnored As TOKEN_PRIVILEGES
    Dim lBufferNeeded As Long
    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
    ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE), &HFFFF
End Sub

Ho usato l'api ExitWindowsEx di user32 ma al massimo riesco a disconnettere
l'utente.
Come faccio a chiuderlo?
CiaoZ

Che parametri gli dai ?
Ciao
Sergio


> Che parametri gli dai ?
E' tutto qui:
Option Explicit
Dim OraSpegn As Date, OraCorr As Date
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long,
ByVal dwReserved As Long) As Long
Private Const EWX_LOGOFF = 0
Private Const EWX_SHUTDOWN = 1
Private Const EWX_REBOOT = 2
Private Const EWX_FORCE = 4
Private Sub chkDisattiva_Click()
    If chkDisattiva.Value = vbChecked Then
        TimerConta.Enabled = False
        lblSpegni(6).Enabled = False
    Else
        TimerConta.Enabled = True
        lblSpegni(6).Enabled = True
    End If
End Sub
Private Sub cmdEsegui_Click()
    If comTipo.Text = "" Then MsgBox "Seleziona prima un tipo di
spegnimento", vbExclamation: Exit Sub
    If Not IsDate(mskOreSpegn.Text) Then MsgBox "Questa non è una data!",
vbExclamation: Exit Sub
    OraSpegn = mskOreSpegn.Text
    lblSpegni(5) = OraSpegn
    chkDisattiva.Value = vbUnchecked
End Sub
Private Sub Spegni()
    Dim r As Long, Tipo As Integer
    Select Case comTipo.Text
        Case "SHUTDOWN": Tipo = EWX_LOGOFF
        Case "LOGOFF": Tipo = EWX_SHUTDOWN
        Case "REBOOT": Tipo = EWX_REBOOT
        Case "FORCE": Tipo = EWX_FORCE
        Case Else:
            MsgBox "Tipo Errato! Impossibile Spegnere!", vbCritical: End
    End Select
    r = ExitWindowsEx(Tipo, 0&)
    End
End Sub
Private Sub Form_Load()
    lblSpegni(2) = Now()
End Sub
Private Sub TimerConta_Timer()
    If OraSpegn = Null Then Exit Sub
    lblSpegni(6) = DateDiff("s", OraSpegn, Now())
    lblSpegni(2) = Now()
    If Now() > OraSpegn Then Spegni
End Sub
CiaoZ