Page 1 sur 1

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

Posté : jeu. 19 août 2021 12:04
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)

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

Posté : jeu. 19 août 2021 16:22
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).

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

Posté : jeu. 19 août 2021 18:06
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

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

Posté : jeu. 19 août 2021 20:34
par mikell
Peut-être parce que dans Autoit $WM_POWERBROADCAST est déjà déclaré comme constante dans l'include WindowsConstants.au3 ?

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

Posté : jeu. 19 août 2021 21:02
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.

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

Posté : ven. 20 août 2021 10:19
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