none
Regarding Power Notification RRS feed

  • Question

  • Hi All,
    
    This is continuation to my previous thread.
    
    #include <windows.h>
    #include <msgqueue.h>
    #include <pm.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
    {
    	HANDLE hQ;
    	DWORD dwWait;
    	MSGQUEUEOPTIONS mqopts;
    	HANDLE hReqPwr;
    	HANDLE hOpenMsgQ;
    	POWER_BROADCAST pb;
    	DWORD dwBytesRead;
    	DWORD dwFlags;
    
    	hQ = NULL;
    
    	mqopts.dwSize = 10 * sizeof(MSGQUEUEOPTIONS);
    	mqopts.dwFlags = MSGQUEUE_NOPRECOMMIT ;
    	mqopts.dwMaxMessages = 10;
    	mqopts.cbMaxMessage = sizeof(POWER_BROADCAST) + sizeof(POWER_BROADCAST_POWER_INFO);
    	mqopts.bReadAccess = TRUE;
    	hQ = CreateMsgQueue (NULL, &mqopts);
    	if(NULL == hQ)
    	{
    		ERRORMSG (1,(L"Messgae Queue Creation Failed\r\n"));
    
    		return FALSE;
    	}
    
    //	hReqPwr = RequestPowerNotifications (hQ, PBT_POWERINFOCHANGE | PBT_POWERSTATUSCHANGE | PBT_RESUME | PBT_TRANSITION);
    	hReqPwr = RequestPowerNotifications (hQ, PBT_TRANSITION);
    	if(NULL == hReqPwr)
    	{
    		ERRORMSG (1,(L"RequestPwrNotifications for PBT_TRANSITION failed\r\n"));
    	}
    
    	hOpenMsgQ = OpenMsgQueue(GetCurrentProcess(), hQ, &mqopts);
    	if(NULL == hOpenMsgQ)
    	{
    		ERRORMSG (1,(L"Open Message Queue Failed\r\n"));
    	}
    
    	do{
    	dwWait = WaitForSingleObject (hQ, INFINITE);
    	dwFlags = MSGQUEUE_MSGALERT;
    	if(WAIT_OBJECT_0 == dwWait)
    	{
    		RETAILMSG (1,(L"read msg\r\n"));
    		if(ReadMsgQueue(hQ, &pb, sizeof(POWER_BROADCAST), &dwBytesRead, INFINITE, &dwFlags))
    		{
    			RETAILMSG (1,(L"Read Message Queue Done\r\n"));
    			RETAILMSG (1,(L"QMsg Msg=0x%x Flags=0x%x len=0x%x powerState=%s\r\n",
    				pb.Message, pb.Flags, pb.Length, pb.SystemPowerState));
    		}
    	}
    	}while(POWER_STATE_CRITICAL != (pb.Flags & POWER_STATE_CRITICAL));
    	
    	return 1;
    }
    
    

    I also referred the following URL:http://geekswithblogs.net/BruceEitman/archive/2009/02/14/windows-ce-monitor-power-state-changes.aspx

    My Test setup is as given below

    Setup: 1. The application which has the above code will be alunched manually from Target RunPrograms by me.

     2. This application is used to test on DeviceEmulator.

    3. In BatteryPDDGetStatus (battif.c), I have created a static variable with a value equals to 0.

     After the power notification application is launched I will keep a breakpoint inside this BatteryPDDGetStatus and set this variable equals to 1.

    When ever this static variable is set to 1, battery flag set to the following statement. BatteryFlag = BATTERY_FLAG_CRITICAL;

    After this statement execution is completed I can see a Low Power Notification on the DeviceEmulator. Why my application is not able to get that message?

    Luca Calligaris,

    I followed your suggestion of increasing cbMaxMessage value but didn't received a low power notification in the application.

    Thank You & Regards,

    GSR

     

    Tuesday, April 26, 2011 9:39 AM

Answers

  • Take at how is implemented in the taskbar code (%_WINCEROOT%\PUBLIC\SHELL\OAK\HPC\EXPLORER\TASKBAR\taskbar.cpp) or in control panel (%_WINCEROOT%\PUBLIC\WCESHELLFE\OAK\CTLPNL\CPLMAIN\power.cpp). See also the following code:

    	BYTE pbMsgBuf[sizeof(POWER_BROADCAST) + sizeof(POWER_BROADCAST_POWER_INFO)];
    	MSGQUEUEOPTIONS msgopts;
    
    	HANDLE rghWaits = NULL;
    	HANDLE hReq = NULL;
    	DWORD dwRet = 1; // 1 is error
    
    	// Create our message queue
    	memset(&msgopts, 0, sizeof(msgopts));
      msgopts.dwSize = sizeof(msgopts);
      msgopts.dwFlags = 0;
      msgopts.dwMaxMessages = 0;
      msgopts.cbMaxMessage = sizeof(pbMsgBuf);
      msgopts.bReadAccess = TRUE;
    
    	rghWaits = CreateMsgQueue(NULL, &msgopts);
    	if (!rghWaits) {
    		DEBUGMSG(1, (TEXT("Could not create power message queue\r\n")));
    		goto EXIT;
    	}
    
    	// Request notifications
    	hReq = RequestPowerNotifications(rghWaits, PBT_POWERINFOCHANGE);
    	if (!hReq) {
    		DEBUGMSG(1, (TEXT("RequestPowerNotifications() failed\r\n")));
    		goto EXIT;
    	}
    
      while(TRUE)
      {
        DWORD dwWaitCode = WaitForSingleObject(rghWaits, INFINITE);
        if(dwWaitCode == WAIT_OBJECT_0)
        {
    			DWORD dwSize, dwFlags;
    
          if (ReadMsgQueue(rghWaits[0], pbMsgBuf, sizeof(pbMsgBuf), &dwSize, 0, &dwFlags)) {
            DEBUGMSG(ZONE_POWER, (TEXT("Received power notification\r\n")));
    				SendMessage(hDlg, WM_USER_NOTIFICATION, 0, (LPARAM) pbMsgBuf);
          }
    			else {
    				DEBUGCHK(FALSE); // We should never get here
    			}
        }
        else {
          // got terminate signal
          break;
        }
      }
    
    	dwRet = 0; // Success!

     

     

     

     


    Luca Calligaris lucaDOTcalligarisATeurotechDOTcom www.eurotech.com Check my blog: http://lcalligaris.wordpress.com
    • Marked as answer by GSRid Thursday, April 28, 2011 8:08 AM
    Tuesday, April 26, 2011 10:19 AM
  • Note that Luca's code is requesting notifications for PBT_POWERINFOCHANGE which is used to get battery status change messages.
    Bruce Eitman (eMVP)
    Senior Engineer
    Bruce.Eitman AT Eurotech DOT com
    My BLOG http://geekswithblogs.net/bruceeitman

    Eurotech Inc.
    www.Eurotech.com
    • Marked as answer by GSRid Thursday, April 28, 2011 8:08 AM
    Tuesday, April 26, 2011 5:09 PM
    Moderator

All replies

  • Take at how is implemented in the taskbar code (%_WINCEROOT%\PUBLIC\SHELL\OAK\HPC\EXPLORER\TASKBAR\taskbar.cpp) or in control panel (%_WINCEROOT%\PUBLIC\WCESHELLFE\OAK\CTLPNL\CPLMAIN\power.cpp). See also the following code:

    	BYTE pbMsgBuf[sizeof(POWER_BROADCAST) + sizeof(POWER_BROADCAST_POWER_INFO)];
    	MSGQUEUEOPTIONS msgopts;
    
    	HANDLE rghWaits = NULL;
    	HANDLE hReq = NULL;
    	DWORD dwRet = 1; // 1 is error
    
    	// Create our message queue
    	memset(&msgopts, 0, sizeof(msgopts));
      msgopts.dwSize = sizeof(msgopts);
      msgopts.dwFlags = 0;
      msgopts.dwMaxMessages = 0;
      msgopts.cbMaxMessage = sizeof(pbMsgBuf);
      msgopts.bReadAccess = TRUE;
    
    	rghWaits = CreateMsgQueue(NULL, &msgopts);
    	if (!rghWaits) {
    		DEBUGMSG(1, (TEXT("Could not create power message queue\r\n")));
    		goto EXIT;
    	}
    
    	// Request notifications
    	hReq = RequestPowerNotifications(rghWaits, PBT_POWERINFOCHANGE);
    	if (!hReq) {
    		DEBUGMSG(1, (TEXT("RequestPowerNotifications() failed\r\n")));
    		goto EXIT;
    	}
    
      while(TRUE)
      {
        DWORD dwWaitCode = WaitForSingleObject(rghWaits, INFINITE);
        if(dwWaitCode == WAIT_OBJECT_0)
        {
    			DWORD dwSize, dwFlags;
    
          if (ReadMsgQueue(rghWaits[0], pbMsgBuf, sizeof(pbMsgBuf), &dwSize, 0, &dwFlags)) {
            DEBUGMSG(ZONE_POWER, (TEXT("Received power notification\r\n")));
    				SendMessage(hDlg, WM_USER_NOTIFICATION, 0, (LPARAM) pbMsgBuf);
          }
    			else {
    				DEBUGCHK(FALSE); // We should never get here
    			}
        }
        else {
          // got terminate signal
          break;
        }
      }
    
    	dwRet = 0; // Success!

     

     

     

     


    Luca Calligaris lucaDOTcalligarisATeurotechDOTcom www.eurotech.com Check my blog: http://lcalligaris.wordpress.com
    • Marked as answer by GSRid Thursday, April 28, 2011 8:08 AM
    Tuesday, April 26, 2011 10:19 AM
  • Note that Luca's code is requesting notifications for PBT_POWERINFOCHANGE which is used to get battery status change messages.
    Bruce Eitman (eMVP)
    Senior Engineer
    Bruce.Eitman AT Eurotech DOT com
    My BLOG http://geekswithblogs.net/bruceeitman

    Eurotech Inc.
    www.Eurotech.com
    • Marked as answer by GSRid Thursday, April 28, 2011 8:08 AM
    Tuesday, April 26, 2011 5:09 PM
    Moderator
  • Hi Luca Calligaris and Bruce Eitman,

    Thank You for reading and replying for my query.

    After changing RequestPowerNotifications flag, my console application able to receive the battery critical status.

    Thank You & Regards,

    GSR

    Thursday, April 28, 2011 8:10 AM