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

Detecting Hibernation / Standby and resume in a Service

1,493 views
Skip to first unread message

Mike Collins

unread,
Mar 12, 2009, 2:08:46 PM3/12/09
to
Hi all, apologies if this is not the best newsgroup for this post...

I have a service written that does some interaction (polling) with some 3rd
party USB tokens. We have found over a period of time that interface to
these tokens appears to stop responding when the system goes into
hibernation and standby. The token manufactures suggested that this is
caused because our polling thread continues to poll whilst the system is
entering a power-down state. This has been confirmed (or resolved) in a
prototype desktop application that detects the WM_POWERBROADCAST message and
suspends the polling thread until the system has resumed.

Now I'm trying to achieve the same thing in our service. I originally
thought that if I used the SERVICE_ACCEPT_POWEREVENT flag in the
SERVICE_STATUS structure, I would receive SERVICE_CONTROL_POWEREVENT
messages in HandlerEx. However, this message does not provide any
information pertaining to Hibernation / Standby, only changes in the power
profile. So I created a hidden message window within my service in the hope
that it would capture the WM_POWERBROADCAST message but to no effect.

My code looks like this:

WNDCLASSEX WndClsEx;
memset(&WndClsEx, 0, sizeof(WndClsEx));
WndClsEx.cbSize = sizeof(WndClsEx);
WndClsEx.hInstance = HInstance;
WndClsEx.lpfnWndProc = WndProc;
WndClsEx.lpszClassName = L"Test";
ATOM atom = ::RegisterClassEx(&WndClsEx);
if (atom)
{
//HWND_MESSAGE <- does not recieve broadcast messages
hPowerMsgWnd = CreateWindowEx(WS_EX_TOOLWINDOW, WndClsEx.lpszClassName,
L"", 0, 0, 0,
0, 0, NULL, NULL, WndClsEx.hInstance, NULL);
}

LRESULT CALLBACK TServiceEx::WndProc(HWND hWnd, UINT Msg, WPARAM wParam,
LPARAM lParam)
{
switch (Msg)
{
case WM_CREATE : return 0;

case WM_DESTROY : return 0;

case WM_POWERBROADCAST : Beep(1000, 1000);
return
TRUE;

default : return DefWindowProc(hWnd, Msg, wParam,
lParam);
}
}

WndProc receives the WM_CREATE message and if I used SendMessage(hWnd,
WM_POWERBROADCAST, 0, 0) from within my service, it receives it, but when I
power down or back up, I get nothing send from the OS.

Does any one have any suggestions? Is this possible from a service and
would it work on Vista and XP?

Many thanks in advance,

Mike C


Jeroen Mostert

unread,
Mar 13, 2009, 6:13:53 PM3/13/09
to
Mike Collins wrote:
> I have a service written that does some interaction (polling) with some 3rd
> party USB tokens. We have found over a period of time that interface to
> these tokens appears to stop responding when the system goes into
> hibernation and standby. The token manufactures suggested that this is
> caused because our polling thread continues to poll whilst the system is
> entering a power-down state. This has been confirmed (or resolved) in a
> prototype desktop application that detects the WM_POWERBROADCAST message and
> suspends the polling thread until the system has resumed.
>
> Now I'm trying to achieve the same thing in our service. I originally
> thought that if I used the SERVICE_ACCEPT_POWEREVENT flag in the
> SERVICE_STATUS structure, I would receive SERVICE_CONTROL_POWEREVENT
> messages in HandlerEx. However, this message does not provide any
> information pertaining to Hibernation / Standby, only changes in the power
> profile.

Actually, SERVICE_CONTROL_POWEREVENT should contain exact analogues to the
information in WM_POWERBROADCAST, including whether it's a suspend/resume
(PBT_APMSUSPEND, PBT_APMRESUMEAUTOMATIC), which should also cover
hibernate/resume. See the table in http://msdn.microsoft.com/library/ms995870 .

> So I created a hidden message window within my service in the hope
> that it would capture the WM_POWERBROADCAST message but to no effect.
>

No, nothing of this sort will work, and that's exactly why
SERVICE_CONTROL_POWEREVENT was created in the first place. You could
presumably get this to work by making your service run as an interactive
service, but you don't want to do that (and even then I'm not sure it would
work in Vista).

--
J.

Mike Collins

unread,
Mar 13, 2009, 7:49:10 PM3/13/09
to
Hay Jeroen,

Thanks for the confirmation. This is actually exactly what I have found.
However the MSDN documentation, specifically the docs for HandlerEx
(http://msdn.microsoft.com/en-us/library/ms683241(VS.85).aspx) are pretty
vague about this area. If you read these docs, it look more like the
service will only receive notification with the POWERBROADCAST_SETTING
structure.

After messing on for a while, I traced this through the service HandlerEx
and as you say, I get the correct messages.

Many thanks,

Mike C

"Jeroen Mostert" <jmos...@xs4all.nl> wrote in message
news:49badaa5$0$185$e4fe...@news.xs4all.nl...

Pavel A.

unread,
Mar 13, 2009, 8:47:13 PM3/13/09
to
In addition to the POWERBROADCAST, you probably want to handle
surprise removal of the dongle, also when the removal occurs
while the machine is in standby.
For this, you'll need to RegisterDeviceNotification for your deivce handle
and listen to
WM_DEVICECHANGE notifications with DBT_DEVICEQUERYREMOVE parameter:

http://msdn.microsoft.com/en-us/library/aa363427(VS.85).aspx

Dealing with an USB device, can't assume that it will never be removed.

Regards,
--pa

"Mike Collins" <its@TheBottomOfThePost> wrote in message
news:OlR5qTD...@TK2MSFTNGP03.phx.gbl...

0 new messages