none
STANDBY power broadcast RRS feed

  • Question

  • Hi,

    I am working on interrupting the standby and hibernate of Windows Vista and am handling WM_POWERBROADCAST. I found that in XP the hibernate/standby msg parameter is PBT_APMQUERYSTANDBY while in vista it is now something else. can someone point me as to what the value changed to? I cannot allow the machine to go into hibernate or standby.

    Thursday, June 29, 2006 11:05 PM

Answers

  • Thanks for the help mike, I found that this is a feature that is not implemented in XP and was correctly done in vista. The app should perform as you have suggested. XP has the ability to allow the app to block the hibernation or standby modes even when forced. Thanks again.

    BTW the app is copying data to cd/dvds

    Wednesday, July 5, 2006 6:50 AM

All replies

  • Update:

    It seems what was once interpreted as PBT_APMQUERYSTANDBY is now PBT_APMSUSPEND and the hibernate and standby are mapped directly to interrupt the hardware without letting the software reply to the message.

    I can't imagine that this was done as a design. Does anyone know how to stop the standby? I tried to set the power state using SetSystemPowerState(FALSE, FALSE); but that did not work either. It returned an error code 1314 "ERROR_PRIVILEGE_NOT_HELD, A required privilege is not held by the client."

    Friday, June 30, 2006 5:34 PM
  • Aborting a hibernate might not be in the best interests of a user. I have my notebook set to hibernate when the battery is low (yes I still have a notebook that gets a respectable battery life). I constantly have half-composed blog entries, forum postings and interesting websites open that I would like not to lose should my battery die while I'm attending to life.

    That being said, I'd prefer not to have an application pop up some dialog or otherwise prevent my computer from hibernating (thus ensuring a full shutdown) should it need to.

    So if Vista prevents this behavior, it's a positive in my books. (Of course I'll probably have a new notebook by the time vista comes out where the battery serves as little more than a means to move from the living room to the kitchen without powering off, but it's the principle).

    I'm interested, what does your application do that hibernation is so destructive to it?

    Saturday, July 1, 2006 4:58 AM
  • Thanks for the help mike, I found that this is a feature that is not implemented in XP and was correctly done in vista. The app should perform as you have suggested. XP has the ability to allow the app to block the hibernation or standby modes even when forced. Thanks again.

    BTW the app is copying data to cd/dvds

    Wednesday, July 5, 2006 6:50 AM
  • Hi,

    I have one problem. I'm calling SetSystemPowerState call to send the system to a sleep state in Windows Vista on a timer event. WM_POWERBROADCAST message is received, but the wParam is not set to PBT_APMSUSPEND. Instead, I'm getting PBT_RESUMECRITICAL message. Please explain me where the problem is coming and how can I solve that. Please note I'm working on WINDOWS VISTA and NOT XP (In XP, it perfectly works). For your convenience I'm attaching the code.

    Many thanks in advance.

    #define _WIN32_WINNT 0x0500

    #include <windows.h>
    #include <stdio.h>
    #include <string.h>
    #include <wchar.h>
    #include <tchar.h>



    #define TIMER_ONE 1
    #define TIMER_TWO 2

    void    Suspend (HWND);
    void    Resume (HWND);

    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

    BOOL    flagHibernate = FALSE;
    TCHAR    *btnTxt     = "START";
    int        btnStyle = BS_PUSHBUTTON;

    TCHAR    *start = "START";
    TCHAR    *stop = "STOP";

    HANDLE            hTimer;
    LARGE_INTEGER    li;


    FILE    *fp;
    SYSTEMTIME            sysTime;
    short int cycle = 0;


    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static TCHAR szAppName[] = TEXT ("HelloWin") ;
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASS     wndclass ;


         fp = fopen ("C:\\SchedulerLog.txt", "w+");

         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = NULL ;
         wndclass.lpszClassName = szAppName ;

         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
         hwnd = CreateWindow (szAppName,                  // window class name
                              TEXT ("The Hello Program"), // window caption
                              WS_OVERLAPPEDWINDOW,        // window style
                              CW_USEDEFAULT,              // initial x position
                              CW_USEDEFAULT,              // initial y position
                              CW_USEDEFAULT,              // initial x size
                              CW_USEDEFAULT,              // initial y size
                              NULL,                       // parent window handle
                              NULL,                       // window menu handle
                              hInstance,                  // program instance handle
                              NULL) ;                     // creation parameters
        
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
        
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }



    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
         HDC                hdc ;
         PAINTSTRUCT        ps ;
         RECT                rect ;
         static  char        sec_remaining = 60;
         static HWND        hwndBtn;
         static int            cxChar, cyChar ;
         TCHAR                str[100], strsec[20];
         HANDLE                hToken; // handle to process token
         TOKEN_PRIVILEGES    tkp; // pointer to token structure
         
       

         switch (message)
         {
         case WM_CREATE:

             MessageBox (hwnd, TEXT("Hello World"), TEXT ("Info"), MB_SERVICE_NOTIFICATION);
              cxChar = LOWORD (GetDialogBaseUnits ()) ;
              cyChar = HIWORD (GetDialogBaseUnits ()) ;


             OpenProcessToken(
                GetCurrentProcess(),
                TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
                &hToken); // Get the LUID for shutdown privilege.   
             
            LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
            tkp.PrivilegeCount = 1; // one privilege to set
            tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;  
            AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);

            hwndBtn = CreateWindow ( TEXT("button"),
                                     btnTxt,
                                     WS_CHILD | WS_VISIBLE | btnStyle,
                                     cxChar, cyChar * (1 + 2 * 1),
                                     20 * cxChar, 7 * cyChar / 4,
                                     hwnd, (HMENU) 1,
                                     ((LPCREATESTRUCT) lParam)->hInstance, NULL);




              return 0 ;

        case WM_TIMER:

            switch (wParam)
            {
            case TIMER_ONE:
                  Suspend (hwnd);
                  break ;

            case TIMER_TWO:
                    //MessageBox (hwnd, TEXT ("TIMER_TWO"), TEXT ("TIMER"), MB_OK);
                    sec_remaining -= 1;
                    InvalidateRect (hwnd, NULL, 1);

            }
            return 0;

        case WM_POWERBROADCAST:
                    switch (wParam)
                    {

                        case PBT_APMSUSPEND:            // System gonna sleep
                        {
                            li.QuadPart = -600000000;
                            hTimer=CreateWaitableTimer(NULL,TRUE,"WAITABLETIMER");
                            SetWaitableTimer (hTimer, &li, 0, NULL, NULL, TRUE);
                            GetLocalTime (&sysTime);
                            fprintf (fp, "Suspending %d.%d.%d %d:%d:%d.%d\n", sysTime.wDay, sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);
                            break;
                        }
                   
                        case PBT_APMRESUMEAUTOMATIC:    // The system is resuming automatically
                        {
                            GetLocalTime (&sysTime);
                            fprintf (fp, "Resuming: %d.%d.%d %d:%d:%d.%d\n", sysTime.wDay, sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);
                            if (flagHibernate == FALSE)
                                flagHibernate = TRUE;
                            else
                                flagHibernate = FALSE;
                            SetThreadExecutionState(ES_DISPLAY_REQUIRED);
                            CancelWaitableTimer (hTimer);

                            cycle++;
                            GetLocalTime (&sysTime);
                            fprintf (fp, "Resuming Setting the timer.: %d.%d.%d %d:%d:%d.%d\n", sysTime.wDay, sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);
                            if (cycle % 2 == 0)
                                fprintf (fp, "=======================================================\n");
                            SetTimer (hwnd, TIMER_ONE, 60000, NULL);
                            KillTimer (hwnd, TIMER_TWO);
                            sec_remaining = 60;
                            SetTimer (hwnd, TIMER_TWO, 1000, NULL);
                            break;
                        }

                    }
                    return 0 ;

        case WM_COMMAND:
           
            GetWindowText (hwndBtn, str, 12);
            if (_tcscmp (str, start) == 0)
            {
                SetTimer (hwnd, TIMER_ONE, 60000, NULL);
                SetTimer (hwnd, TIMER_TWO, 1000, NULL);
                SetWindowText (hwndBtn, stop) ;
            }
            else
            {
                SetWindowText (hwndBtn, start) ;
                KillTimer (hwnd, TIMER_ONE);
                KillTimer (hwnd, TIMER_TWO);
                CancelWaitableTimer (hTimer);
            }
           
            //SetSystemPowerState (TRUE, TRUE);
            return 0;



         case WM_PAINT:
              hdc = BeginPaint (hwnd, &ps) ;
             
              GetClientRect (hwnd, &rect) ;

              wsprintf (strsec, "%d Seconds to go", sec_remaining);
             
              DrawText (hdc, TEXT (strsec), -1, &rect,
                        DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
              EndPaint (hwnd, &ps) ;
              return 0 ;

         case WM_CLOSE:
             //MessageBox (hwnd, TEXT("Closing"), TEXT("WM_CLOSE"), 0);
             fclose (fp);
             
         case WM_DESTROY:
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }


    void Suspend (HWND hwnd)
    {
        KillTimer (hwnd, TIMER_ONE);
        RequestWakeupLatency (LT_LOWEST_LATENCY);
        GetLocalTime (&sysTime);
        fprintf (fp, "Calling Suspend (%d): %d.%d.%d %d:%d:%d.%d\n", flagHibernate, sysTime.wDay, sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);
        SetSystemPowerState (flagHibernate, TRUE);
    }


    /*
    void Resume (HWND hwnd)
    {
        //RequestDeviceWakeup (hwnd);
        SetTimer (hwnd, TIMER_ONE, 60000, 0);
    }
    */
    Wednesday, August 23, 2006 4:18 AM
  • calling SetThreadExecutionState(ES_SYSTEM_REQUIRED) to info the system that the processor is in use, so system will not getting suspend or hinernate

    calling SetThreadExecutionState(ES_CONTINUOUS) to info the system that the processor is not in use, , so the suspend or hinernate mode is free to go

    Friday, June 15, 2007 9:19 AM