Newsgroup: it.comp.lang.visual-basic
Titolo: spegnere il computer

per favore , qualcuno potrebbe spiegarmi come č possibile associare il
command_click allo spegnimento del computer.
Spero che ci sia qualcuno di cosě gentile da rispondermi,
Ringrazio anticipatamente
Enrico....

 command_click allo spegnimento del computer.
metti il codice seguente in un modulo, poi nel Command1_Click chiami la
funzione ShutdownSystem con il parametro opportuno (scusa ma non ho voglia
di riavviare il PC ora provandolo  :))
Ciao
Faber
'***********************************************************
'You have the royalty free right to use the code in this
'module in your own applications.
'You may not distribute or sell all or parts of this
'sourcecode in any way.
'You may not distribute or sell any application which main
'features are the functions of this module.
'You may point to our internet area in order to let other
'developers use this module.
'***********************************************************
'modEndWindowsSession (EndWindowsSession.bas) - Version 2
'
'© 1998-1999 Whitworth Software Solutions
'            Am Hessebusch 14
'            D-31812 Bad Pyrmont
'            Germany
'
'            Tel. : 
+49 5281 960401
'            Fax  : 
+49 5281 960498
'
'            Inet : 
http://www.w-s-s.de
'            eMail: 
Ser...@w-s-s.de
'
'***********************************************************
'Developed by   : R. Whitworth
'Date           : 10/21/98
'***********************************************************
'Changes:                           by:             Date:
'-----------------------------------------------------------
'AdjustToken() errorhandling        R. Whitworth    06/05/99
'***********************************************************
'Exported functions:
'
'   ShutdownSystem(eType As eShutDown)
'
'***********************************************************
Option Explicit
'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

 command_click allo spegnimento del computer.


Se fai una ricerca troverai un altro msg simile con la risposta, ad ogni
modo devi chiamare con Shell il seguente CMD esterno:
C:\WINDOWS\RUNDLL32.EXE user.exe,exitwindows
Enrico <bru...@tiscaliNET.it> wrote in message 391ea...@news.dada.it...
