Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Power Change Notification: Resume

235 views
Skip to first unread message

David Jones

unread,
Sep 5, 2006, 8:00:02 AM9/5/06
to
Searching for some sample code to notify an application when power state
changes.
In particular upon Resume.

Docs suggest using RequestPowerNotifications
Anyone got some sample code?
Thx
--
David Jones
Senior Lecturer
School of Electrical & Computer Engineering
RMIT University
+61 3 99255318

David Jones

unread,
Sep 5, 2006, 7:23:01 PM9/5/06
to
How can I get your sample code?
Thanks

--
David Jones
Senior Lecturer
School of Electrical & Computer Engineering
RMIT University
+61 3 99255318


"John Baik" wrote:

> I have sample code which I have tested in my device.
> John Baik
>
> "David Jones" <dave...@rmit.edu.au.nospamplease> wrote in message
> news:66CAABFB-765E-458A...@microsoft.com...

Dean Ramsier

unread,
Sep 6, 2006, 10:05:59 AM9/6/06
to
It's attached to his posting and available from the reader, at least in
OE...

--
Dean Ramsier - eMVP
BSQUARE Corporation


"David Jones" <dave...@rmit.edu.au.nospamplease> wrote in message

news:50D3C9F7-9F89-4891...@microsoft.com...

John Baik

unread,
Sep 6, 2006, 1:17:04 PM9/6/06
to
I cut and pasted the source code for who can't see attached file.
I'm using Visual Studio .NET 2005 for build app.

// John Baik: Sample code for Power Notification

#include <windows.h>
#include <PM.h>
// from pmimpl.h file.
#ifndef QUEUE_ENTRIES
#define QUEUE_ENTRIES 3
#endif
#ifndef MAX_NAMELEN
#define MAX_NAMELEN 128
#endif

#ifndef QUEUE_SIZE
#define QUEUE_SIZE (QUEUE_ENTRIES * (sizeof(POWER_BROADCAST) +
(MAX_NAMELEN * sizeof(TCHAR))))
#endif

int WINAPI PMNotifyThread(LPVOID pvParam);
void PMNotification(HANDLE hMsgQ);

HANDLE ghPMNotifyQ = NULL;

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSGQUEUEOPTIONS msgQpm = {0};
HANDLE hPwrNotify=NULL; // Power manager handle
HANDLE hPMThread=NULL;

// create a message queue for Power Manager notifications
msgQpm.dwSize = sizeof(MSGQUEUEOPTIONS);
msgQpm.dwFlags = 0;
msgQpm.dwMaxMessages = QUEUE_ENTRIES;
msgQpm.cbMaxMessage = sizeof(POWER_BROADCAST) + MAX_NAMELEN;
msgQpm.bReadAccess = TRUE;

ghPMNotifyQ= CreateMsgQueue(NULL, &msgQpm);

if (ghPMNotifyQ == NULL)
{
DWORD dwErr = GetLastError();
RETAILMSG(1, (TEXT(" PMNotify:CreateMessageQueue ERROR:%d \r\n"), dwErr));
return 1;
}

// request Power notifications
hPwrNotify = RequestPowerNotifications(ghPMNotifyQ, PBT_TRANSITION |
PBT_RESUME);

if (hPwrNotify == NULL)
{
DWORD dwErr = GetLastError();
RETAILMSG(1, (TEXT(" PMNotify:RequestPowerNotifications ERROR:%d
\r\n"), dwErr));
return 2;
}

// Create PMNotifyThread
hPMThread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE )PMNotifyThread, NULL, 0, NULL);

if(hPMThread)
{
// wait for PMNotifyThread done
WaitForSingleObject(hPMThread, INFINITE);
CloseHandle(hPMThread);

if(hPwrNotify)
{
StopPowerNotifications(hPwrNotify);
}
}

return 0;

}

// PMNotifyThread: Wait for Message from PM Driver.
//
int WINAPI PMNotifyThread(LPVOID pvParam)
{
DWORD dwStatus;

while (TRUE)
{
dwStatus = WaitForSingleObject(ghPMNotifyQ, INFINITE);

if(dwStatus == WAIT_OBJECT_0)
{
PMNotification(ghPMNotifyQ);
}
else
{
RETAILMSG(1, (TEXT(" PMNotify: WaitForSingleObject returned %d
(error %d)\r\n"),dwStatus, GetLastError()));
break;
}
}

return 0;
}

void PMNotification(HANDLE hMsgQ)
{
UCHAR pmbuf[QUEUE_SIZE];
int nBytesRead=0;
DWORD dwFlags = 0;
int dwCount = 0;

memset(pmbuf, 0, sizeof(pmbuf));

if ( !ReadMsgQueue(hMsgQ,
pmbuf,
QUEUE_SIZE,
(LPDWORD)&nBytesRead,
INFINITE, // Timeout
&dwFlags))
{
DWORD dwErr = GetLastError();
RETAILMSG(1, (TEXT(" ProcessPowerNotification: ReadMsgQueue:
ERROR:%d\n"), dwErr));
}
else if(nBytesRead >= sizeof(POWER_BROADCAST))
{
// process power notifications
//-----------------------------
PPOWER_BROADCAST pPB = (PPOWER_BROADCAST) pmbuf;

switch (pPB->Message)
{
case PBT_RESUME:
RETAILMSG(1, (TEXT(" PMNotify:PBT_RESUME \r\n")));
break;

case PBT_POWERSTATUSCHANGE:
RETAILMSG(1, (TEXT(" PMNotify:PBT_POWERSTATUSCHANGE: \r\n")));
break;

case PBT_POWERINFOCHANGE:
RETAILMSG(1, (TEXT(" PMNotify:PBT_POWERSTATUSCHANGE: \r\n")));
break;

case PBT_TRANSITION:
{
switch (POWER_STATE(pPB->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:
break;
case POWER_STATE_RESET:
break;
default:
break;
}
break;
}

default:
break;
}
}
}

Thanks
John Baik

"David Jones" <dave...@rmit.edu.au.nospamplease> wrote in message

news:50D3C9F7-9F89-4891...@microsoft.com...

David Jones

unread,
Sep 6, 2006, 9:32:02 PM9/6/06
to
Thanks for that, both of you.
I'll try that code

===================================
I use the web interface so I guess it doesn't show.(The attachment)

>>> Suggestion to NG software people: <<<<<<<<

Can the web interface have an icon to show when there is attached code?
Then one will know to use OE.
===================================

I also found some other code (.NET) at:
http://blogs.msdn.com/anthonywong/archive/2005/06/07/426392.aspx
Got that working.
Thanks to Anthony as well.

0 new messages