Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    139

    [VB6] Spegnimento automatico del pc

    Ho un form in cui è presente un checkbox(auto_poweroff_pc) e un label con l'orario prestabilito per la chiusura(timeselected)
    devo creare una funzione che (al verificarsi di timeselected.Caption = Time e al verificarsi di auto_poweroff_pc.Value = 1) mi spenga automaticamente il pc (indipendentemente dal sistema operativo).

    Ho provato a usare questo codice ma al momento dello spegnimento mi ricarica il programma e mi da un errore

    Private Sub Timer1_Timer()
    If timeselected.Caption = Time Then
    Unload frmMain
    Unload Me
    Unload frmTools
    Unload frmInfo
    Unload frmUseronline
    If auto_poweroff_pc.Value = 1 Then
    Shell ("Shutdown -S -F -T 5")
    Else
    End
    End If
    End If
    come posso fare???

  2. #2
    Utente di HTML.it L'avatar di LMondi
    Registrato dal
    Sep 2004
    Messaggi
    1,291
    Per spegnere il PC, ti riporto il seguente codice con alcune spiegazioni e considerazioni. Il codice non è farina del mio sacco e non l'ho testato; provalo e raccordalo con il time ....
    codice:
    Const EWX_LOGOFF = 0    'Termina tutti i processi e poi fa il log off.
    Const EWX_SHUTDOWN = 1  'Spegne il sistema.
    Const EWX_REBOOT = 2    'fa il reboot del sistema.
    
    Const EWX_FORCE = 4     ' Forza a essere terminati tutti i processi che non rispondono.
    Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
    '-----------------------------------------------------------------
    Private Sub Form_Load()
        msg = MsgBox("Il programma sta per riavviare il sistema !!! Premere OK per continuare , Annulla per non Riavviare!!!", vbCritical + vbOKCancel + 256, App.Title)
        If msg = vbCancel Then End
        ret& = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
    End Sub
    '-----------------------------------------------------------------
    
    'Funzione: ExitWindowsEx
    'PIATTAFORMA: Win 95/98, Win NT
    
    'La Funzione ExitWindowsEx ARRESTA O RIAVVIA IL COMPUTER DELL'UTENTE.
    'Se avviene un errore, la funzione restituisce 0 altrimenti 1.
    
    'uFlags
    'Uno o più dei seguenti flags per definire come avviene il riavvio o l'arresto
    'del sistema:
    
    'EWX_FORCE = 4
    'Forza la chiusura di una applicazione, ...senza chiedere il permesso all'utente,
    '(non mostra la dialog box dove confermarne la chiusura).
    'Quando questo flag è settato, Windows non trasmette il messaggio
    'WM_QUERYENDSESSION e WM_ENDSESSION all'applicazione in corso, e ciò potrebbe causare
    'la perdita di dati.
    'Pertanto questo flag, andrebbe utilizzato solo in condizioni di emergenza
    
    'EWX_LOGOFF = 0
    'Taglia fuori l'utente e gli chiude tutti i processi in corso.
    
    'EWX_POWEROFF = 8
    'Chiude il sistema , è se questo è supportato dalle caratteristiche POWER-OFF
    'spegne il computer.
    
    'EWX_REBOOT = 2
    'Riavvia il sistema.
    
    'EWX_SHUTDOWN = 1
    'Chiude il sistema nel momento in cui è sicuro che i processi in corso siano
    'terminati.
    
    'dwReserved
    'Riservato per versioni future di Windows. Settarlo sempre a zero.
    
    '-----------------------------------------------------------------
    'OSSERVAZIONI
    
    'La funzione ExitWindowsEx ritorna nel momento in cui ha inizio lo shutdown
    'La chiusura ed il logoff, quindi procedono asincronicamente.
    'Durante queste operazioni, le applicazioni che si stanno chiudendo
    'hanno un tempo per rispondere alla richiesta di shutdown.
    'Se questo tempo scade, Windows mostra una dialog box che abilita l'utente
    'a forzare la chiusura dell'applicazione, oppure ad annullare l'evento.
    LM

  3. #3
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    139
    Private Sub Form_Load()
    msg = MsgBox("Il programma sta per riavviare il sistema !!! Premere OK per continuare , Annulla per non Riavviare!!!", vbCritical + vbOKCancel + 256, App.Title)
    If msg = vbCancel Then End
    ret& = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
    End Sub

    Non mi funziona:
    Se nel msgbox premo cancel mi arresta l'applicazione
    se premo Ok mi ritorna all'applicazione e non succede nulla

    Riporto il mio codice:


    Private Sub Timer1_Timer()
    If timeselected.Caption = Time Then
    If auto_poweroff_pc.Value = 1 Then
    msg = MsgBox("Il programma sta per arrestare il sistema !!! Premere OK per continuare , Annulla per non Riavviare!!!", vbCritical + vbOKCancel + 256, App.Title)
    If msg = vbCancel Then End
    If msg = vbOK Then ret& = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
    Else
    Unload frmMain
    Unload Me
    Unload frmTools
    Unload frmInfo
    Unload frmUseronline
    End
    End If
    End If
    End Sub

  4. #4
    Utente di HTML.it L'avatar di LMondi
    Registrato dal
    Sep 2004
    Messaggi
    1,291
    Fai una ricerca su API Guide della funzione ExitWindowsEx - vi sono altri esempi.
    LM

  5. #5
    Utente di HTML.it
    Registrato dal
    Feb 2007
    Messaggi
    42
    Devi impostare i privilegi di shutdown con:
    codice:
    Private Sub AdjustToken()
        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    ' One privilege to set
        tkp.TheLuid = tmpLuid
        tkp.Attributes = SE_PRIVILEGE_ENABLED
        
        AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
    
    End Sub
    Le chiamate dunque sono:
    codice:
    AdjustToken
    ExitWindowsEx EWX_FORCE Or EWX_POWEROFF, 0&
    Ricordati di inserire queste dichiarazioni:


    codice:
    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 As Long = 2
    Private Const EWX_POWEROFF As Long = 8
    
    
    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

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.