[R] Script pour éteindre une application en cours lorsque le processus : « shutdown » est en action.

Aide et conseils concernant AutoIt et ses outils.
Règles du forum
.
Répondre
Boulanza
Niveau 9
Niveau 9
Messages : 697
Enregistré le : mer. 31 juil. 2013 15:49
Status : Hors ligne

[R] Script pour éteindre une application en cours lorsque le processus : « shutdown » est en action.

#1

Message par Boulanza »

Bonjour tout le monde,
Je cherche un script qui va me permettre d’arrêter le processus d’ une applications qui tourne en boucle lorsque « shutdown » est en action (-c: arrêter le "shutdown" en cours). Notamment dans les cas suivants :
-l: fermeture la session en cours.
-u: éteindre l'ordinateur
-r: redémarre l'ordinateur.
-h: mise en veille prolongée.
-s: mise en veille simple.
-f: force l'action.
- t secs: délai en secondes.
N.B.: Lorsqu’en clique sur Démarrer\Marche\Arrêt\Mettre en veille ==> les processus en cours d’exécution ne se ferment pas
Merci, pour votre aide.
Cordialement.
If ProcessExists("shutdown.exe") Then ProcessClose("shutdown.exe")
Sleep(1000)
ProcessClose("MonApplication.exe")
Sleep(5000)
Shutdown(4)
Modifié en dernier par Boulanza le mar. 20 sept. 2022 18:35, modifié 1 fois.
Avatar du membre
TommyDDR
Modérateur
Modérateur
Messages : 2086
Enregistré le : mar. 22 juil. 2008 21:55
Localisation : Nantes
Status : Hors ligne

Re: [..] Script pour éteindre une application en cours lorsque le processus : « shutdown » est en action.

#2

Message par TommyDDR »

Essayez de regarder le post suivant : https://www.autoitscript.com/forum/topi ... ent=773510
Cela consiste plutôt en la détection de l’évènement de mise en veille et d'agir au sein de votre application directement (plus propre que de le fermer de l'extérieur).
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
Boulanza
Niveau 9
Niveau 9
Messages : 697
Enregistré le : mer. 31 juil. 2013 15:49
Status : Hors ligne

Re: [..] Script pour éteindre une application en cours lorsque le processus : « shutdown » est en action.

#3

Message par Boulanza »

Bonjour TommyDDR,
Merci infiniment pour votre aide. Malheureusement le code ne fonctionne que pour : Windows Server 2003, Windows XP, Windows 2000 , Windows 2008 et Windows Vista, pas pour Windows 10.
Voici le message d'erreur : error: $WM_POWERBROADCAST previously declared as a 'Const'.
Cordialement.
Global Const $WM_POWERBROADCAST = 0x218
Global Const $PBT_APMPOWERSTATUSCHANGE = 0xA ; Power status has changed.
Global Const $PBT_APMRESUMEAUTOMATIC = 0x12 ; Operation is resuming automatically from a low-power state.
  This message is sent every time the system resumes.
Global Const $PBT_APMRESUMESUSPEND = 0x7 ;Operation is resuming from a low-power state. This message is sent after
 ;PBT_APMRESUMEAUTOMATIC if the resume is triggered by user input,
 ; such as pressing a key.
Global Const $PBT_APMSUSPEND = 0x4 ; System is suspending operation.
Global Const $PBT_POWERSETTINGCHANGE = 0x8013 ; A power setting change event has been received.
; Windows Server 2003, Windows XP, and Windows 2000:  The following event identifiers are also supported.
Global Const $PBT_APMBATTERYLOW = 0x9 ; Battery power is low. In Windows Server 2008 and Windows Vista, use PBT_APMPOWERSTATUSCHANGE instead.
Global Const $PBT_APMOEMEVENT = 0xB ; OEM-defined event occurred. In Windows Server 2008 and Windows Vista, this event is not available because these operating systems support only ACPI; APM BIOS events are not supported.
Global Const $PBT_APMQUERYSUSPEND = 0x0 ; Request for permission to suspend. In Windows Server 2008 and Windows Vista, use the SetThreadExecutionState function instead.
Global Const $PBT_APMQUERYSUSPENDFAILED = 0x2 ; Suspension request denied. In Windows Server 2008 and Windows Vista, use SetThreadExecutionState instead.
Global Const $PBT_APMRESUMECRITICAL = 0x6 ; Operation resuming after critical suspension. In Windows Server 2008 and Windows Vista, use PBT_APMRESUMEAUTOMATIC instead.
Global Const $BROADCAST_QUERY_DENY = 0x424D5144

GUICreate("Event Receiver")
GUIRegisterMsg($WM_POWERBROADCAST,"MY_WM_POWERBROADCAST")
While 1
    Sleep(100)
WEnd
Func MY_WM_POWERBROADCAST($hWnd, $uMsg, $wParam, $lParam)
    ; Beschreibgungen auf http://msdn.microsoft.com/en-us/library/aa373247(VS.85).aspx
    Switch $wParam
        Case $PBT_APMPOWERSTATUSCHANGE
           #cs
            typedef struct {
              GUID  PowerSetting;
              DWORD DataLength;
              UCHAR Data[1];
            } POWERBROADCAST_SETTING, *PPOWERBROADCAST_SETTING;
            #ce
           $GUID = DllStructCreate("dword;word;word;byte")

            ;Local $tNMBHOTITEM = DllStructCreate($GUID&";DWORD;UCHAR",$lParam)
            Local $tNMBHOTITEM = DllStructCreate(DllStructGetPtr($GUID)&";DWORD;CHAR",$lParam)
            Local $nNotifyCode = DllStructGetData($tNMBHOTITEM, 1)
            Local $nID = DllStructGetData($tNMBHOTITEM, 2)
            Local $hCtrl = DllStructGetData($tNMBHOTITEM, 3)
            Local $dwFlags = DllStructGetData($tNMBHOTITEM, 4)
            Tooltip($nNotifyCode&" "&$nID&" "&$hCtrl);&" "&$dwFlags
        Case $PBT_APMQUERYSUSPEND
            Return $BROADCAST_QUERY_DENY

    EndSwitch
EndFunc
Avatar du membre
mikell
Spammer !
Spammer !
Messages : 6292
Enregistré le : dim. 29 mai 2011 17:32
Localisation : Deep Cévennes
Status : Hors ligne

Re: [..] Script pour éteindre une application en cours lorsque le processus : « shutdown » est en action.

#4

Message par mikell »

Peut-être parce que dans Autoit $WM_POWERBROADCAST est déjà déclaré comme constante dans l'include WindowsConstants.au3 ?
" L'échec est le fondement de la réussite. " (Lao-Tseu )
" Plus ça rate, plus on a de chances que ça marche " (les Shadoks )
Boulanza
Niveau 9
Niveau 9
Messages : 697
Enregistré le : mer. 31 juil. 2013 15:49
Status : Hors ligne

Re: [..] Script pour éteindre une application en cours lorsque le processus : « shutdown » est en action.

#5

Message par Boulanza »

Bonjour mikell,
mikell a écrit : jeu. 19 août 2021 20:34 Peut-être parce que dans Autoit $WM_POWERBROADCAST est déjà déclaré comme constante dans l'include WindowsConstants.au3 ?
Je ne pense pas que c'est la cause du message d'erreur !
y a-t-il une autre méthode/suggestion pour éviter cela ? Merci.
Cordialement.
Avatar du membre
TommyDDR
Modérateur
Modérateur
Messages : 2086
Enregistré le : mar. 22 juil. 2008 21:55
Localisation : Nantes
Status : Hors ligne

Re: [..] Script pour éteindre une application en cours lorsque le processus : « shutdown » est en action.

#6

Message par TommyDDR »

Pourtant quand je copie tel quel votre script, il n'y a pas d'erreur.
Et comme l'a dit mikell, ce message vous dit explicitement que la variable est déjà déclarée ailleurs.

PS : en virant les variables inutiles et en ajoutant l'include :
#include <WindowsConstants.au3>

Global Const $PBT_APMPOWERSTATUSCHANGE = 0xA ; Power status has changed.
Global Const $PBT_APMQUERYSUSPEND = 0x0 ; Request for permission to suspend. In Windows Server 2008 and Windows Vista, use the SetThreadExecutionState function instead.
Global Const $BROADCAST_QUERY_DENY = 0x424D5144

GUICreate("Event Receiver")
GUIRegisterMsg($WM_POWERBROADCAST,"MY_WM_POWERBROADCAST")
While 1
    Sleep(100)
WEnd
Func MY_WM_POWERBROADCAST($hWnd, $uMsg, $wParam, $lParam)
    Switch $wParam
        Case $PBT_APMPOWERSTATUSCHANGE
           $GUID = DllStructCreate("dword;word;word;byte")

            ;Local $tNMBHOTITEM = DllStructCreate($GUID&";DWORD;UCHAR",$lParam)
            Local $tNMBHOTITEM = DllStructCreate(DllStructGetPtr($GUID)&";DWORD;CHAR",$lParam)
            Local $nNotifyCode = DllStructGetData($tNMBHOTITEM, 1)
            Local $nID = DllStructGetData($tNMBHOTITEM, 2)
            Local $hCtrl = DllStructGetData($tNMBHOTITEM, 3)
            Local $dwFlags = DllStructGetData($tNMBHOTITEM, 4)
            Tooltip($nNotifyCode&" "&$nID&" "&$hCtrl);&" "&$dwFlags
        Case $PBT_APMQUERYSUSPEND
            Return $BROADCAST_QUERY_DENY

    EndSwitch
EndFunc
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
Répondre