Jump to content

Tray Menu Interrupts _WinAPI_ShutdownBlockReasonCreate()


amin84
 Share

Recommended Posts

Hello,

I'm experiencing strange behavior with _WinAPI_ShutdownBlockReasonCreate().

I have this simple script that you can _WinAPI_ShutdownBlockReasonCreate() and _WinAPI_ShutdownBlockReasonDestroy() using Ctrl + Shift + T. When _WinAPI_ShutdownBlockReasonCreate() is called, it will stop system from restarting.

Here is the problem.

  • Run this code and it will be disabled by default (computer will log off or restart).
  • Press Ctrl + Shift + T to enable it which it will do _WinAPI_ShutdownBlockReasonCreate().
  • Now if you try to log off, it won't be able to and it will show that this program needs to close.
  • Press cancel to go back to desktop.
  • If you try to log off again, _WinAPI_ShutdownBlockReasonCreate() still exists which will stop you as expected.
  • Click on the tray icon to open the menu and click anywhere on the desktop to cancel the menu.
  • Now try to log off and it will log off which it should not!

_WinAPI_ShutdownBlockReasonCreate() doesn't exists anymore after opening the tray menu! Anybody knows what happens to _WinAPI_ShutdownBlockReasonCreate() when tray menu opens?

Here is the code:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Constants.au3>
#include <StaticConstants.au3>
#include <Misc.au3>
#include <WinAPISys.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>

Global $enabled = False

Opt('TrayMenuMode',3)
Opt('TrayAutoPause',0)
Opt("GUICloseOnESC", 0)

HotKeySet('^+t','_guard')

Global $g_hForm = GUICreate('DUMMY GUI', 200, 200)
GUIRegisterMsg($WM_QUERYENDSESSION, 'WM_QUERYENDSESSION')
_WinAPI_SetProcessShutdownParameters(0x03FF)
;menu
$aboutTray = TrayCreateItem('DEMO')
TrayItemSetState(-1,$TRAY_DISABLE)
TrayCreateItem('')
$enableTray = TrayCreateItem('Enabled'&@TAB&'Ctrl + Shift + T')
TrayCreateItem('')
$settingsTray = TrayCreateItem('Settings...')
TrayCreateItem('')
$exitTray = TrayCreateItem('Exit')
TraySetState()
;settings
$gui = GUICreate("DEMO Settings", 386+7, 114+93, -1, -1, $WS_SYSMENU)

TrayTip("", "DISABLED", 0)

Func _guard()
    If Not $enabled Then ;enable it
        $enabled = True
        _WinAPI_ShutdownBlockReasonCreate($g_hForm, 'This application is blocking system shutdown because the saving critical data is in progress.')
        TrayItemSetState($enableTray,$TRAY_CHECKED)
        TrayTip("", "ENABLED", 0)
    Else
        $enabled = False
        _WinAPI_ShutdownBlockReasonDestroy($g_hForm)
        TrayItemSetState($enableTray,$TRAY_UNCHECKED)
        TrayTip("", "DISABLED", 0)
    EndIf
EndFunc

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            GUISetState(@SW_HIDE, $gui)
    EndSwitch
    $tMsg = TrayGetMsg()
    Switch $tMsg
        Case $enableTray
            _guard()
        Case $exitTray
            Exit
    EndSwitch
WEnd

Exit

Func WM_QUERYENDSESSION($hWnd, $Msg, $wParam, $lParam)
    Switch $hWnd
        Case $g_hForm
            If _WinAPI_ShutdownBlockReasonQuery($g_hForm) Then
                Return 0
            EndIf
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

Any help is much appreciated.

Thanks.

Edited by leomoon
Link to comment
Share on other sites

It seems that AutoIT does not allow toggle between _WinAPI_ShutdownBlockReasonCreate and _WinAPI_ShutdownBlockReasonDestroy using system tray directly.

Something happens to _WinAPI_ShutdownBlockReasonCreate as soon as the system tray is clicked and _WinAPI_ShutdownBlockReasonCreate no longer works!

Is there anyway to toggle it indirectly using system tray?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...