Code Repo    |     RSS
MD's Technical Sharing



Tuesday, November 4, 2008

Capturing Power Status Change on WM Devices

Using RequestPowerNotifications (http://msdn.microsoft.com/en-us/library/ms919833.aspx) to be informed when device goes to or gets out of suspend/standby mode


Sample code in C++:


// callback function when power status changed

int WINAPI PMNotifyThread(LPVOID pvParam);

void PMNotification(HANDLE hMsgQ);

HANDLE ghPMNotifyQ = NULL;


// create a message queue for Power Manager notifications

MSGQUEUEOPTIONS msgQpm = {0};

HANDLE hPwrNotify=NULL; // Power manager handle

HANDLE hPMThread=NULL;

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);


// request Power notifications

hPwrNotify = RequestPowerNotifications(ghPMNotifyQ, PBT_TRANSITION | PBT_RESUME);


// Create PMNotifyThread

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


// wait for PMNotifyThread done

WaitForSingleObject(hPMThread, INFINITE);

CloseHandle(hPMThread);


Download sample source code here


Full source code:

C++: http://groups.google.com/group/microsoft.public.windowsce.platbuilder/msg/a9b58a9e96342ef3


C#.NET (via P/Invoke) : http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3907661&SiteID=1

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.