How to detect system suspend?

We’d need to detect when system leaves and reenters S0 state. It is easy in kernel driver and standard app but we’d need it in a service which can’t have a window at Vista. Is there some easy way which’d work since w2k? An API both documented or undocumented, system wide event, WMI query, anything.

Something simpler than a helper app or driver which’d inform service about it. In general, we’d need a way which can be implemented in a library which can be used from any app or service and doesn’t depends on anything else.

Thanks.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]

Michal,

We have service power notifications. Check out http://msdn2.microsoft.com/en-us/library/ms683241.aspx and look for the “SERVICE_CONTROL_POWEREVENT” event type. The service has to use a HandlerEx and has to call SetServiceStatus and indicate it would like to receive power notifications.

Let me know whether this looks like what you need.

Dave

Dave,

thanks. It is a bit more complicated (we have to decide if it is for app or service) but can solve our problem. I wonder why there aren’t two system wide named notification events (SystemSuspended, SystemResumed) for which could any piece of code in system wait.

BTW, the docs you posted states:

dwEventType:

If dwControl is SERVICE_CONTROL_POWEREVENT, this parameter is PBT_POWERSETTINGCHANGE.

Am I right it is incomplete and dwEventType can be one of power broadcast codes as PBT_APMSUSPEND used in the example here: http://msdn2.microsoft.com/en-us/library/ms810440.aspx ?

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]


From: xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on behalf of xxxxx@microsoft.com[SMTP:xxxxx@microsoft.com]
Reply To: Windows System Software Devs Interest List
Sent: Wednesday, March 07, 2007 9:14 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] How to detect system suspend?

Michal,

We have service power notifications. Check out http://msdn2.microsoft.com/en-us/library/ms683241.aspx and look for the “SERVICE_CONTROL_POWEREVENT” event type. The service has to use a HandlerEx and has to call SetServiceStatus and indicate it would like to receive power notifications.

Let me know whether this looks like what you need.

Dave


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Having a named event:

  1. doesn’t scale well - everything you want to know about becomes a
    different event.
  2. requires everyone who wants the notification to burn a thread waiting
    on the event.
  3. can’t provide any data about what happened, only that something
    happened. What should be signaled if the attempt to suspend the system
    started but then failed? Do we need a third event?

-p

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Michal Vodicka
Sent: Wednesday, March 07, 2007 12:41 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] How to detect system suspend?

Dave,

thanks. It is a bit more complicated (we have to decide if it is for app
or service) but can solve our problem. I wonder why there aren’t two
system wide named notification events (SystemSuspended, SystemResumed)
for which could any piece of code in system wait.

BTW, the docs you posted states:

dwEventType:

If dwControl is SERVICE_CONTROL_POWEREVENT, this parameter is
PBT_POWERSETTINGCHANGE.

Am I right it is incomplete and dwEventType can be one of power
broadcast codes as PBT_APMSUSPEND used in the example here:
http://msdn2.microsoft.com/en-us/library/ms810440.aspx ?

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]


From:
xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com
] on behalf of xxxxx@microsoft.com[SMTP:xxxxx@microsoft.com]
Reply To: Windows System Software Devs Interest List
Sent: Wednesday, March 07, 2007 9:14 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] How to detect system suspend?

Michal,

We have service power notifications. Check out
http://msdn2.microsoft.com/en-us/library/ms683241.aspx and look for the
“SERVICE_CONTROL_POWEREVENT” event type. The service has to use a
HandlerEx and has to call SetServiceStatus and indicate it would like to
receive power notifications.

Let me know whether this looks like what you need.

Dave


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

> ----------

From: xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on behalf of Peter Wieland[SMTP:xxxxx@windows.microsoft.com]
Reply To: Windows System Software Devs Interest List
Sent: Wednesday, March 07, 2007 10:04 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] How to detect system suspend?

Having a named event:

  1. doesn’t scale well - everything you want to know about becomes a
    different event.

So why there is so much events of this type in BaseNamedObjects? :slight_smile: One for many: DHCPNEWIPADDRESS.

Generally, I’d agree if there is an easy way how to check current system state. Win32 API would be quite sufficient. But there isn’t any, if I understand correctly. Sorry but a need to create a separate window and thread for a console app just to find this info doesn’t make me happy.

  1. requires everyone who wants the notification to burn a thread waiting
    on the event.

Why? App which doesn’t need to wait can just read event state (wait with zero timeout). In this case threads need to wait for requested system state or don’t care about it.

  1. can’t provide any data about what happened, only that something
    happened.

Which is exactly what is needed in this case. I implemented such two events in my driver and everything works great but we need a solution for other interfaces when only OS supplied drivers are loaded.

What should be signaled if the attempt to suspend the system
started but then failed? Do we need a third event?

No. In my driver I set events from power state callback which is called when system leaves S0 and when reenters it. No need to care about suspend fail, it is like other S0 -> Sx -> S0 transition. App is blocked for a while and resumed on fail.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]

> Sorry but a need to create a separate window and

thread for a console app just to find this info doesn’t make me happy.

I fully agree with Michal here. Power event notifications for *console* apps
are very inconvenient.

Dmitriy Budko
VMware

Here are a couple of answers/comments for this post:

(1) Yes, the MSDN documentation is wrong. SERVICE_CONTROL_POWEREVENT does indeed take the other power types (e.g., PBT_APMSUSPEND, PBT_APMRESUMEAUTOMATIC, etc.) and not just PBT_POWERSETTINGCHANGE. This was a new setting added in Vista, and it looks like there was a mistake in editing the MSDN docs for this page. I’ll look into geting the MSDN help updated correctly.

(2) In addition to not scaling well, using global named events for indicating power state transitions has one big problem: how would the OS “know” that listeners of these events have finished whatever work they needed to do when the event was signalled? Using directed window messages and service notifications, we do know when you’re done and that’s it’s safe to move on.

(3) I’m not sure how a Win32 API to query the current power state of the system would be of any use, plus it would require a polling infrastructure (which should almost always be avoided). What you really want is a notification that the state is changing “right now” and a guarnateed handshake mechanim with the OS where you have a chance to take some action (which is what we have).

(4) Yes, notifications in consoles is painful. We fully understand this and are looking a ways of making this easier in future versions of Windows (e.g., don’t need to spin up extra threads, create hidden windows to receive messages, etc.).

thanks,
Nick, MS

> ----------

From: xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on behalf of xxxxx@microsoft.com[SMTP:xxxxx@microsoft.com]
Reply To: Windows System Software Devs Interest List
Sent: Friday, March 09, 2007 6:30 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] How to detect system suspend?

Here are a couple of answers/comments for this post:

(1) Yes, the MSDN documentation is wrong. SERVICE_CONTROL_POWEREVENT does indeed take the other power types (e.g., PBT_APMSUSPEND, PBT_APMRESUMEAUTOMATIC, etc.) and not just PBT_POWERSETTINGCHANGE. This was a new setting added in Vista, and it looks like there was a mistake in editing the MSDN docs for this page. I’ll look into geting the MSDN help updated correctly.

Great, thanks.

(2) In addition to not scaling well, using global named events for indicating power state transitions has one big problem: how would the OS “know” that listeners of these events have finished whatever work they needed to do when the event was signalled? Using directed window messages and service notifications, we do know when you’re done and that’s it’s safe to move on.

I didn’t mention to replace current solution with global named events. Instead, add more options. Some application just need to be notified about power state transitions and don’t need a handshake with OS. Notification events are ideal for this purpose; simple and efficient. App can both wait for them or just read the current state.

BTW, the handshake could be implemented using two named events but it is rather inelegant solution.

(3) I’m not sure how a Win32 API to query the current power state of the system would be of any use, plus it would require a polling infrastructure (which should almost always be avoided). What you really want is a notification that the state is changing “right now” and a guarnateed handshake mechanim with the OS where you have a chance to take some action (which is what we have).

What *I* really want is a way how to block app until system awakes from suspend. And a way how to know if the system was suspended since last query. We currently implemented a temporary solution for a service using one notification event and one counter. Service notification handler sets the event when system is in S0 and resets when leaves it and increments the counter when system is leaving S0. Clients (the library) can wait for the event and check the counter when necessary.

(4) Yes, notifications in consoles is painful. We fully understand this and are looking a ways of making this easier in future versions of Windows (e.g., don’t need to spin up extra threads, create hidden windows to receive messages, etc.).

It would be nice. Please consider that code which needs notification may need to be separated from the app/service. In our case we provide a DLL with an API allowing to communicate unified way with our different hardware. The DLL should be independent piece of code and shouldn’t need anything from the caller and shouldn’t influence behaviour of the process which uses it. It should also work from any process the same way. Current notifications don’t fit these requirements even for standard app. It is ugly when library creates a hidden window and thread even for app with windows. Yes, we’re doing it but currently it stopped work with services.

I guess the ideal solution would be some kind of callbacks which’d code register and which wouldn’t influence anything else. Similarly as in kernel when a driver can register power state and power settings change callbacks.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]

Win32_PowerManagementEvent perhaps.

m.

Michal Vodicka wrote:

We’d need to detect when system leaves and reenters S0 state. It is easy in kernel driver and standard app but we’d need it in a service which can’t have a window at Vista. Is there some easy way which’d work since w2k? An API both documented or undocumented, system wide event, WMI query, anything.

Something simpler than a helper app or driver which’d inform service about it. In general, we’d need a way which can be implemented in a library which can be used from any app or service and doesn’t depends on anything else.

Thanks.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer