반응형

Windows Mobile 전원 상태 감시

Windows Mobile의 경우, 장치의 특성상
일정 시간 입력이 없는 경우 suspend모드로 변경되는 경우가 잦게 일어 난다.

본 포스트에서는 특정 어플리케이션에서 전원상태의 변환을 감지하는 방법에 대해 기술한다.

주제의 핵심은
RequestPowerNotifications() 함수로 요약된다.
이 함수를 통해 전원의 변화 상태를 통지받겠다고 등록하면,
전원 상태가 변경될때마다, 통지를 해준다.
. 일반 윈도우 메시지 큐로 전달되는것이 아니라 별도의 윈도우 메시지 큐를 생성해야 한다.


// 전원변화상태통지를요청

BOOL RegisterPwNotify()

{

             m_nBufferSize = (MAX_PATH+sizeof(POWER_BROADCAST))*MAX_MESSAGES;

             m_pBuffer = new BYTE[m_nBufferSize];

             MSGQUEUEOPTIONS msgOptions = {0};

             msgOptions.dwSize = sizeof(MSGQUEUEOPTIONS);

             msgOptions.dwFlags = 0;

             msgOptions.dwMaxMessages = MAX_MESSAGES;

             msgOptions.cbMaxMessage = sizeof(POWER_BROADCAST) + MAX_PATH;

             msgOptions.bReadAccess = TRUE;

             m_hQueue = CreateMsgQueue(NULL, &msgOptions);

             if (! m_hQueue)

             {

                           MessageBox(g_hWnd, TEXT("Error: CreateMsgQueue"), TEXT("Error!"),

MB_OK|MB_APPLMODAL|MB_ICONSTOP);

 

                           return FALSE;

             }

 

             HANDLE hThread = CreateThread(0, 0, ThreadProc, 0, 0, 0);

             if (! hThread)

             {

                           MessageBox(g_hWnd, TEXT("Error: CreateThread"), TEXT("Error!"),

MB_OK|MB_APPLMODAL|MB_ICONSTOP);

 

                           return FALSE;

             }

 

             m_hRequest = ::RequestPowerNotifications(m_hQueue, POWER_NOTIFY_ALL);

             if (!m_hRequest)

             {

                           MessageBox(g_hWnd, TEXT("Error: RequestPowerNotifications"), TEXT("Error!"),

MB_OK|MB_APPLMODAL|MB_ICONSTOP);

                           return FALSE;

             }

             CloseHandle( hThread );

             return TRUE;

}

 

// Thread를통해위에서생성한메시지큐를감시한다.

void Run()

{

             TCHAR szChar[100];

 

             memset(m_pBuffer, 0, m_nBufferSize);

 

             DWORD dwReaded = 0;

 

             DWORD dwFlags = 0;

 

             while ( TRUE )

             {

                           memset(m_pBuffer, 0, m_nBufferSize);

                           DWORD dwReaded = 0;

                           DWORD dwFlags = 0;

                           if (::ReadMsgQueue(m_hQueue, m_pBuffer, m_nBufferSize, &dwReaded,

MAX_MESSAGES, &dwFlags))

                           {

                                        if (dwReaded >= sizeof(POWER_BROADCAST))

                                        {

                                                     WCHAR szCahr[256] = {0};

 

                                                     POWER_BROADCAST* pb = (POWER_BROADCAST*)m_pBuffer;

                                                     if ( pb->Message & PBT_POWERINFOCHANGE )

                                                     {

                                                                  wsprintf(szChar, L"msg=PBT_POWERINFOCHANGE");

                                                     }

                                                     else if ( pb->Message & PBT_POWERSTATUSCHANGE )

                                                     {

                                                                  wsprintf(szChar, L"msg=PBT_POWERINFOCHANGE");

                                                     }

                                                     else if ( pb->Message & PBT_POWERINFOCPBT_RESUMEHANGE )

                                                     {

                                                                  wsprintf(szChar, L"msg=PBT_RESUME");

                                                     }

                                                     else if ( pb->Message & PBT_TRANSITION )

                                                     {

                                                                  wsprintf(szChar, L"msg=PBT_TRANSITION");

                                                     }

 

                                                     if ( pb->Flags & POWER_STATE_BOOT )

                                                     {

                                                                  wsprintf(szChar, L"%s, flag=POWER_STATE_BOOT", szChar);

                                                     }

                                                     else if ( pb->Flags & PPOWER_STATE_CRITICALOWER_STATE_BOOT )

                                                     {

                                                                  wsprintf(szChar, L"%s, flag=

PPOWER_STATE_CRITICALOWER_STATE_BOOT", szChar);

                                                     }

                                                     else if ( pb->Flags & POWER_STATE_IDLE )

                                                     {

                                                                  wsprintf(szChar, L"%s, flag=POWER_STATE_IDLE", szChar);

                                                     }

                                                     else if ( pb->Flags & POWER_STATE_ON )

                                                     {

                                                                  wsprintf(szChar, L"%s, flag=POWER_STATE_ON", szChar);

                                                     }

                                                     else if ( pb->Flags & POWER_STATE_OFF )

                                                     {

                                                                  wsprintf(szChar, L"%s, flag=POWER_STATE_OFF", szChar);

                                                     }

                                                     else if ( pb->Flags & POWER_STATE_RESET )

                                                     {

                                                                  wsprintf(szChar, L"%s, flag=POWER_STATE_RESET", szChar);

                                                     }

                                                     else if ( pb->Flags & POWER_STATE_SUSPEND )

                                                     {

                                                                  wsprintf(szChar, L"%s, flag=POWER_STATE_SUSPEND",

szChar);

                                                     }

                                                     if ( pb->Message&PBT_POWERINFOCHANGE )

                                                     {

                                                                  wsprintf(szChar, L"%s, SystemPowerState=", szChar);

                                                     }

                                                     else

                                                     {

                                                                  wsprintf(szChar, L"%s, SystemPowerState=%s", szChar,

pb->SystemPowerState);

                                                     }

                                                     MessageBox(g_hWnd, szChar, TEXT("test"),

MB_OK|MB_APPLMODAL|MB_ICONSTOP);

                                        }

                           }

             }

}

 

// 통지등록을제거한다.

BOOL UnRegisterPwNotify()

{

             if ( m_hQueue)

             {

                           CloseMsgQueue(m_hQueue);

                           m_hQueue = NULL;

             }

             if ( m_hRequest )

             {

                           StopPowerNotifications( m_hRequest );

                           m_hRequest = NULL;

             }

             return TRUE;

}

 

 

 

 

 

반응형

+ Recent posts