Announcement

Collapse
No announcement yet.

Trying to figure out why I am not getting WM_POWERBROADCAST messages.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trying to figure out why I am not getting WM_POWERBROADCAST messages.

    I would have thought this would have worked in the dialog callback function:
    Code:
          CASE %WM_POWERBROADCAST
             WINBEEP(250,500)
             MSGBOX "%WM_POWERBROADCAST"

  • #2
    Have you called RegisterPowerSettingNotification

    Comment


    • #3
      Dave,

      If you ran a spy app on your WndProc you will see a very large number of messages going past. If you don't trap them they will just go to the default handler. You really only have to worry about the ones you trap and process.

      Depending on the method of showing a window or dialog, not all messages are available.
      hutch at movsd dot com
      The MASM Forum

      www.masm32.com

      Comment


      • #4
        Thanks guys! I think Stuart put his finger on it. I didn't see any mention of Register... when reading up on WM_POWERBROADCAST.

        Comment


        • #5
          You don't really need any registering to receive basic WM_POWERBROADCAST messages,
          just look for it in the main callback function, you will get Windows state like suspend and resume...

          If you need more specific message on particular hardware state then
          use RegisterPowerSettingNotification with the appropriate GUID.
          $GUID_MONITOR_POWER_ON for the monitor, $GUID_LIDSWITCH_STATE_CHANGE for a portable lid switch,
          $GUID_BATTERY_PERCENTAGE_REMAINING for battery percentage, ect.

          Comment


          • #6
            It's not really clear to me which $GUID I should use to get the PBT_APMSUSPEND, PBT_APMSUSPEND, PBT_APMRESUMEAUTOMATIC and PBT_APMRESUMESUSPEND events...
            Regards,
            Peter

            Comment


            • #7
              Hey Peter,
              You don't need any registration for those messages...

              Code:
              'An application may use SetSuspendState to transition the system from
              'the working state to the standby (sleep).
              'This function is similar to the SetSystemPowerState function.
              
              'If the system wakes due to user activity, the system will first broadcast
              'the PBT_APMRESUMEAUTOMATIC event followed by a PBT_APMRESUMESUSPEND event.
              'In addition, the system will turn on the display. Your application should reopen files
              'that it closed when the system entered the sleep state, and it should prepare for user input.
              
              '%PBT_APMQUERYSUSPEND         = &H0000
              '%PBT_APMQUERYSTANDBY         = &H0001
              '%PBT_APMQUERYSUSPENDFAILED   = &H0002
              '%PBT_APMQUERYSTANDBYFAILED   = &H0003
              '%PBT_APMSUSPEND              = &H0004
              '%PBT_APMSTANDBY              = &H0005
              '%PBT_APMRESUMECRITICAL       = &H0006
              '%PBT_APMRESUMESUSPEND        = &H0007
              '%PBT_APMRESUMESTANDBY        = &H0008
              '%PBTF_APMRESUMEFROMFAILURE   = &H00000001
              '%PBT_APMBATTERYLOW           = &H0009
              '%PBT_APMPOWERSTATUSCHANGE    = &H000A
              '%PBT_APMOEMEVENT             = &H000B
              '%PBT_APMRESUMEAUTOMATIC      = &H0012
              '_____________________________________________________________________________
              
              #COMPILE EXE '#Win#
              #INCLUDE "Win32Api.inc"
              #INCLUDE "CommCtrl.inc"
              
              #RESOURCE MANIFEST, 1, "XPTheme.xml" ' or...
              '#RESOURCE "D:\Basic\Bas\~~PbrDefault00.pbr" 'Set a valid resource
              
              DECLARE FUNCTION IsPwrSuspendAllowed LIB "PowrProf.DLL" ALIAS "IsPwrSuspendAllowed" () AS LONG
              
              DECLARE FUNCTION SetSuspendState LIB "PowrProf.DLL" ALIAS "SetSuspendState" _
              (BYVAL Hibernate AS LONG, BYVAL ForceCritical AS LONG, BYVAL DisableWakeEvent AS LONG) AS LONG
              
              GLOBAL hDlg AS DWORD
              
              %ButtonSuspend         = 101
              %ButtonExit            = 102
              %Listbox               = 201
              %TASKBAR_MIN_ALL       = 419
              '_____________________________________________________________________________
              
              FUNCTION LA(sItem AS STRING) AS LONG
               LOCAL hList    AS DWORD
               LOCAL LastItem AS LONG
              
               hList = GetDlgItem(hDlg, %ListBox)
               LastItem = SendMessage(hList, %LB_ADDSTRING, 0, BYVAL STRPTR(sItem))
               SendMessage(hList, %LB_SETCURSEL, LastItem, 0) 'Select an item and scroll it to view
               SendMessage(hList, %LB_SETCURSEL, -1, 0)       'Unselect an item
              
              END FUNCTION
              '_____________________________________________________________________________
              
              CALLBACK FUNCTION DlgProc
               LOCAL Retval AS LONG
              
               SELECT CASE CBMSG
              
                 CASE %WM_INITDIALOG
                   LA "Press suspend button to suspend..."
              
                 CASE %WM_COMMAND
                   SELECT CASE LOWRD(CBWPARAM)
              
                     CASE %ButtonExit
                       DIALOG END hDlg
              
                     CASE %ButtonSuspend
                       IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                         LISTBOX RESET hDlg, %Listbox
                         LA "Suspend button clicked at " & TIME$
                         IF IsPwrSuspendAllowed THEN 'See also IsPwrHibernateAllowed for hibernation.
              
                           ShowWindow(hDlg, %SW_MINIMIZE) 'Use this so SW_RESTORE will work
              
                           SendMessage(FindWindow("Shell_TrayWnd", ""), %WM_COMMAND, %TASKBAR_MIN_ALL, 0)
              
                           'SetSuspendState: Suspends the system by shutting power down. Depending on the Hibernate
                           'parameter, the system either enters a suspend (sleep) state or hibernation (S4).
                           'Hibernate        [in] If TRUE, system hibernates. If FALSE, system is suspended.
                           'ForceCritical    [in] If TRUE, system suspends immediately. If FALSE, system broadcasts
                           '                      PBT_APMQUERYSUSPEND to each program to request permission.
                           Retval = SetSuspendState(%FALSE, %FALSE, %FALSE)
                           '*********************************************
                           '***                                       ***
                           '***    S U S P E N D  take place here     ***
                           '***                                       ***
                           '*********************************************
                           IF Retval THEN
                             LA STRING$(50, "*")
                             LA "Resuming from suspend at " & TIME$
                           ELSE
                             LA "SetSuspendState error at " & TIME$
                           END IF
                           ShowWindow(hDlg, %SW_RESTORE)
                         ELSE
                           LA "IsPwrSuspendAllowed returned FALSE."
                           LA "Please setup computer for suspend."
                         END IF
                       END IF
                   END SELECT
              
                 CASE %WM_QUERYENDSESSION
              
                 CASE %WM_POWERBROADCAST
                   SELECT CASE CBWPARAM
              
                     CASE %PBT_APMQUERYSUSPEND
                       IF (CBLPARAM AND 1) THEN 'Hibernation do not have the force flag
                         LA "WM_POWERBROADCAST-PBT_APMQUERYSUSPEND received at " & TIME$
                         'FUNCTION = %TRUE 'Return TRUE to grant the request to suspend.
                         'FUNCTION = %BROADCAST_QUERY_DENY 'To deny the request.
                         'EXIT FUNCTION
                       END IF
              
                     CASE %PBT_APMSUSPEND '1 - Received before suspend
                       'Smooth transitions to and from sleep are integral to the Windows Vista experience.
                       'The PBT_APMSUSPEND suspend notice is broadcast about two seconds prior to
                       'the sleep transition. The notice interval is shorter in Windows Vista than
                       'in earlier versions of Windows. Also, the standard configuration of Windows Vista
                       'prohibits applications from vetoing a sleep transition, although this behavior
                       'can be overridden through "Group Policy".
                       LA "WM_POWERBROADCAST-PBT_APMSUSPEND received at " & TIME$ 'Not received under Vista.
              
                     CASE %PBT_APMRESUMEAUTOMATIC '2 - Received after suspend
                       LA& "WM_POWERBROADCAST-PBT_APMRESUMEAUTOMATIC received at " & TIME$
                       IF (CBLPARAM AND %PBTF_APMRESUMEFROMFAILURE) THEN
                         LA "WM_POWERBROADCAST--PBTF_APMRESUMEFROMFAILURE received at " & TIME$
                       END IF
              
                     CASE %PBT_APMRESUMESUSPEND '3 - Received after first mouse move on dialog
                       LA "WM_POWERBROADCAST-PBT_APMRESUMESUSPEND received at " & TIME$
                       IF (CBLPARAM AND %PBTF_APMRESUMEFROMFAILURE) THEN
                         LA "WM_POWERBROADCAST--PBTF_APMRESUMEFROMFAILURE received at " & TIME$
                       END IF
              
                     CASE %PBT_APMRESUMECRITICAL
                       LA "WM_POWERBROADCAST-PBT_APMRESUMECRITICAL received at " & TIME$
                       IF (CBLPARAM AND %PBTF_APMRESUMEFROMFAILURE) THEN
                         LA "WM_POWERBROADCAST--PBTF_APMRESUMEFROMFAILURE received at " & TIME$
                       END IF
              
                     CASE %PBT_APMQUERYSUSPENDFAILED 'Suspension request denied.
                       LA "WM_POWERBROADCAST--PBT_APMQUERYSUSPENDFAILED received at " & TIME$
              
                     CASE %PBT_APMQUERYSUSPENDFAILED
                       LA "WM_POWERBROADCAST-PBT_APMQUERYSUSPENDFAILED received at " & TIME$
              
                     CASE %PBT_APMPOWERSTATUSCHANGE
                       LA "WM_POWERBROADCAST-PBT_APMPOWERSTATUSCHANGE received at " & TIME$
              
                     CASE %PBT_APMBATTERYLOW
                       LA "WM_POWERBROADCAST-PBT_APMBATTERYLOW received at " & TIME$
              
                   END SELECT
              
                END SELECT
              
              END FUNCTION
              '_____________________________________________________________________________
              
              FUNCTION PBMAIN()
               LOCAL hIconBig   AS DWORD
               LOCAL hIconSmall AS DWORD
              
               DIALOG FONT "Segoe UI", 9
               DIALOG NEW %HWND_DESKTOP, "PowerBroadcast", , , 260, 130, _
               %WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_SYSMENU, %WS_EX_TOPMOST TO hDlg
              
               ExtractIconEx("Shell32.dll", 25, BYVAL VARPTR(hIconBig), BYVAL VARPTR(hIconSmall), 1)
               SetClassLong(hDlg, %GCL_HICONSM, hIconSmall) 'Set an icon
               SetClassLong(hDlg, %GCL_HICON, hIconBig) 'Set an icon
               SendMessage(hDlg, %WM_SETICON, %ICON_SMALL, hIconSmall)
               SendMessage(hDlg, %WM_SETICON, %ICON_BIG, hIconBig)
              
               CONTROL ADD BUTTON, hDlg, %ButtonSuspend, "&Suspend", 5, 10, 122, 15
               CONTROL ADD BUTTON, hDlg, %ButtonExit, "E&xit", 133, 10, 122, 15
               CONTROL ADD LISTBOX, hDlg, %Listbox, , 5, 35, 250, 100
              
               DIALOG SHOW MODAL hDlg CALL DlgProc
              
               DestroyIcon(hIconSmall)
               DestroyIcon(hIconBig)
              
              END FUNCTION
              '_____________________________________________________________________________
              '
              Last edited by Pierre Bellisle; Yesterday, 08:21 PM.

              Comment

              Working...
              X