Question 186

Comment mettre l'ordinateur en veille depuis un programme VB ?

Pour mettre l'ordinateur en veille ou en veille prolongée, il suffit d'utiliser la fonction SetSuspendState de l'API Windows.

Voici un exemple tout simple de mise en oeuvre :

Private Declare Function SetSuspendState Lib "Powrprof.dll" ( _
                                       ByVal Hibernate As Long, _
                                       ByVal ForceCritical As Long, _
                                       ByVal DisableWakeEvents As Long _
                                       ) As Long

' Mise en veille simple
'
Private Function SystemSuspend() As Long

    SystemSuspend = SetSuspendState(0, 1, 0)

End Function

' Mise en veille prolongée
'
Private Function SystemHibernate() As Long
    
    SystemHibernate = SetSuspendState(1, 1, 0)
End Function

' Exemple d'appel
'
Private Sub Command1_Click()
    Dim lRet As Long
    
    lRet = SystemHibernate()
    If lRet <> 0 Then
        ' La fonction s'est exécutée avec succès
    End If
End Sub

Il est possible d'appeler cette API en utilisant la fonction Shell en conjonction avec rundll32 :

Private Sub SystemSuspend()

   Shell "rundll32.exe powrprof.dll,SetSuspendState 0,1,0"
End Sub

Pour aller plus loin

Voir aussi :

Date de publication : 06 mars 2008
Dernière modification : 06 mars 2008
Rubriques : Généralités, Windows
Mots-clés : mise en veille, veille, veille prolongée, power management, SetSuspendState, gestion de l'alimentation, hibernation, SetSystemPowerState, fermer, arréter