// 전원변화상태통지를요청
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;
}
|