BOOL g_FlagExitThrd=FALSE;
HANDLE hNotifications;
HANDLE hReadMsgQ;
MSGQUEUEOPTIONS msgOptions = {0};
// Create a message queue for Power Manager notifications.
msgOptions.dwSize = sizeof(MSGQUEUEOPTIONS);
msgOptions.dwFlags = 0;
msgOptions.dwMaxMessages = QUEUE_ENTRIES;
msgOptions.cbMaxMessage = sizeof(POWER_BROADCAST) + MAX_NAMELEN;
msgOptions.bReadAccess = TRUE;
hReadMsgQ = CreateMsgQueue(NULL, &msgOptions);
if ( hReadMsgQ == NULL )
{
DEBUGMSG(ZONE_ERROR, (TEXT("PWM: CreateMsgQueue(Read): error %d
"), GetLastError()));
g_FlagExitThrd = TRUE;
}
// Request Power notifications
hNotifications = RequestPowerNotifications(hReadMsgQ, POWER_NOTIFY_ALL);
if ( ! hNotifications )
{
DEBUGMSG(ZONE_ERROR, (TEXT("PWM: RequestPowerNotifications: error %d
"), GetLastError()));
g_FlagExitThrd = TRUE;
}
while(!g_FlagExitThrd)
{
static HANDLE hMemio = INVALID_HANDLE_VALUE;
BOOL result=FALSE;
int iBytesInQueue = 0;
DWORD dwFlags;
UCHAR buf[QUEUE_SIZE];
PPOWER_BROADCAST pB = (PPOWER_BROADCAST) buf;
memset(buf, 0, QUEUE_SIZE);
RETAILMSG(1, (TEXT("----2222222222222:ReadMsgQueue
")));
// Read message from queue.
if (!ReadMsgQueue(hReadMsgQ, buf, QUEUE_SIZE, (LPDWORD)&iBytesInQueue, INFINITE, &dwFlags) )
{
RETAILMSG(1, (TEXT("PWM: ReadMsgQueue: ERROR:%d
"), GetLastError()));
}
else if (iBytesInQueue < sizeof(POWER_BROADCAST) )
{
RETAILMSG(1, (TEXT("PWM: Received short message: %d bytes, expected: %d
"),
iBytesInQueue, sizeof(POWER_BROADCAST)));
}
else
{
switch ( pB->Message )
{
case PBT_TRANSITION:
//RETAILMSG(1, (TEXT("000000000000000000 power state [Flags: 0x%x]: '%s'
"),pB->Flags, pB->SystemPowerState));
switch ( POWER_STATE(pB->Flags) )
{
case POWER_STATE_ON:
break;
case POWER_STATE_OFF:
break;
case POWER_STATE_CRITICAL:
break;
case POWER_STATE_BOOT:
break;
case POWER_STATE_IDLE:
break;
case POWER_STATE_SUSPEND:
RETAILMSG(1, (TEXT("----111111:POWER_STATE_SUSPEND net_CloseConnection()
")));
result = net_CloseConnection();
break;
case POWER_STATE_RESET:
break;
case POWER_STATE_PASSWORD:
break;
default:
break;
}
break;
case PBT_RESUME:
break;
case PBT_POWERSTATUSCHANGE:
break;
case PBT_POWERINFOCHANGE:
break;
default:
break;
}
}
}
if ( hNotifications )
{
StopPowerNotifications(hNotifications);
hNotifications = NULL;
}
if ( hReadMsgQ )
{
CloseMsgQueue(hReadMsgQ);
hReadMsgQ = NULL;
}