Newsgroup: it.comp.lang.visual-basic
Titolo: Arresta - Riavvia il sistema da VB

Sul sito comune ho trovato il file control.zip che contiene un vbp il
quale usa WinControl.ocx per arrestare, riavviare o riavviare come
altro utente, la sessione di Windows.
Ho provato le proprieta' Reboot, Logoff e ShutDown ma sono solo
riuscito a spegnere il PC, di riavviarlo non se ne parla. A parte
l'OCX in questione, come si puo' fare per riavviare o arrestare
Windows da VB.
Gli option che appaiono nella finestra 'fine sessione di lavoro' dopo
aver cliccato su chiudi sessione dove vanno a parare?
(VB5, W98)
Roby

 Windows da VB.
in tuo onore ho lanciato un mio exe che non toccavo da due anni, e ha appena
*ributtato* il pc (w98, andava anche in w95, non so nt).
palarm, con sorgenti, sul mio sito.
non usa ocx.
non c'e' il "riavvia solo windows", ma se controlli se e' premuto lo shift
dovresti riuscirci.
ciao, pal
--
andrea dot palazzi at tin dot it
free vb/sw: 
http://www.comune.modena.it/cittadini/apal/html/

 Windows da VB.
credo che con questo modulo tu faccia ciò che vuoi, a proposito di reboot :)
Ciao ciao
Faber
'Constants
Private Const EWX_LOGOFF = 0&
Private Const EWX_SHUTDOWN = 1&
Private Const EWX_REBOOT = 2&
Private Const EWX_FORCE = 4&
Private Const EWX_POWEROFF = 8&
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_QUERY = &H8
Private Const SE_PRIVILEGE_ENABLED = &H2
'Type Declarations
Private Type LUID
    UsedPart                    As Long
    IgnoredForNowHigh32BitPart  As Long
End Type
Private Type SECURITY_ATTRIBUTES
    nLength                     As Long
    lpSecurityDescriptor        As Long
    bInheritHandle              As Long
End Type
Private Type TOKEN_PRIVILEGES
    PrivilegeCount              As Long
    TheLuid                     As LUID
    Attributes                  As Long
End Type
Private Type OSVERSIONINFO
    dwOSVersionInfoSize         As Long
    dwMajorVersion              As Long
    dwMinorVersion              As Long
    dwBuildNumber               As Long
    dwPlatformId                As Long
    szCSDVersion                As String * 128
End Type
'Function Declarations
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
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 GetVersionEx Lib "kernel32" Alias "GetVersionExA"
(lpVersionInformation As OSVERSIONINFO) 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 OpenProcessToken Lib "advapi32" (ByVal
ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As
Long
'Enum declarations
Public Enum eShutDown
    shtRestart = -1 'Win9x only, restarts Windows without rebooting
    shtLogOff = EWX_LOGOFF
    shtForceLogOff = EWX_LOGOFF Or EWX_FORCE
    shtShutDown = EWX_SHUTDOWN
    shtForceShutDown = EWX_SHUTDOWN Or EWX_FORCE
    shtReboot = EWX_REBOOT
    shtForceReboot = EWX_REBOOT Or EWX_FORCE
    shtPowerOff = EWX_POWEROFF
    shtForcePowerOff = EWX_POWEROFF Or EWX_FORCE
End Enum
Private Enum eWindowsVersion
    wvWin32s = 0 'on Windows 3.x
    wvWindows95 = 1
    wvWindows98 = 2
    wvWindows98SE = 3
    wvWindowsNT351 = 4
    wvWindowsNT4 = 5
    wvWindows2000 = 6
End Enum
Public Sub ShutdownSystem(ByVal eType As eShutDown)
    Dim eWinVersion             As eWindowsVersion
    eWinVersion = GetWindowsVersion()
    If eType = shtRestart Then
        If eWinVersion = wvWindows95 Or _
           eWinVersion = wvWindows98 Or _
           eWinVersion = wvWindows98SE Then
            Call Shell("rundll user.exe,ExitWindowsExec")
            Exit Sub
        Else
            eType = shtReboot
        End If
    End If
    If Not (eType = shtLogOff Or eType = shtForceLogOff) Then
        If eWinVersion = wvWindowsNT351 Or _
           eWinVersion = wvWindowsNT4 Or _
           eWinVersion = wvWindows2000 Then
            Call AdjustToken(False) 'Switch to True for error messages
        End If
    End If
    Call ExitWindowsEx(eType, &HFFFF)
End Sub
'Private procedures
Private Sub AdjustToken(Optional bolShowError As Boolean)
'   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
'   * This procedure sets the proper privileges to allow a log off or a *
'   * shut down to occur under Windows NT.                              *
'   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    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
    Dim lLastError              As Long
    'Use the GetCurrentProcess function to set the hdlProcessHandle
    'variable.
    hdlProcessHandle = GetCurrentProcess()
    lLastError = Err.LastDllError
    If lLastError And bolShowError Then
        MsgBox "GetCurrentProcess error = " & Str(lLastError)
    End If
    Call OpenProcessToken(hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or
TOKEN_QUERY), hdlTokenHandle)
    lLastError = Err.LastDllError
    If lLastError And bolShowError Then
        MsgBox "OpenProcessToken error = " & Str(lLastError)
    End If
    'Get the LUID for shutdown privilege
    Call LookupPrivilegeValue("", "SeShutdownPrivilege", tmpLuid)
    lLastError = Err.LastDllError
    If lLastError And bolShowError Then
        MsgBox "LookupPrivilegeValue error = " & Str(lLastError)
    End If
    tkp.PrivilegeCount = 1    ' One privilege to set
    tkp.TheLuid = tmpLuid
    tkp.Attributes = SE_PRIVILEGE_ENABLED
    'Enable the shutdown privilege in the access token of this process
    Call AdjustTokenPrivileges(hdlTokenHandle, False, tkp,
Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded)
    lLastError = Err.LastDllError
    If lLastError And bolShowError Then
        MsgBox "AdjustTokenPrivileges error = " & Str(lLastError)
    End If
End Sub
Private Function GetWindowsVersion() As eWindowsVersion
    Dim OSInfo                  As OSVERSIONINFO
    Dim iRetValue               As Integer
    OSInfo.dwOSVersionInfoSize = 148
    iRetValue = GetVersionEx(OSInfo)
    With OSInfo
        Select Case .dwPlatformId
            Case 1
                If .dwMinorVersion = 0 Then
                    GetWindowsVersion = wvWindows95
                ElseIf .dwMinorVersion = 10 Then
                    If .dwBuildNumber = 2222 Then
                        GetWindowsVersion = wvWindows98SE
                    Else '.dwBuildNumber = 1998
                        GetWindowsVersion = wvWindows98
                    End If
                End If
            Case 2
                If .dwMajorVersion = 3 Then
                    GetWindowsVersion = wvWindowsNT351
                ElseIf .dwMajorVersion = 4 Then
                    GetWindowsVersion = wvWindowsNT4
                ElseIf .dwMajorVersion = 5 Then
                    GetWindowsVersion = wvWindows2000
                End If
            Case Else
                GetWindowsVersion = wvWin32s
        End Select
    End With
End Function