1

Possible Duplicate:
How to detect if windows is going to hibernate or suspend?

Can someone refresh my memory, from a standpoint of a program running on Windows there's no way to know if the system is entering a sleep mode or hibernating, correct? (I need to know the mode that the system entered, or was just resumed from.)

Community
  • 1
  • 1
c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • 1
    There should be a standard system message warning people that sleep mode is about to be entered, so apps which need to prepare can actually do so. What that is, I have no idea. – Marc B Aug 14 '12 at 00:55
  • There's one, but can a program tell if it's a sleep or hibernation that the system enters into? – c00000fd Aug 14 '12 at 00:57
  • Technically there's not too much different between the two states, other than the processor S-state the system ends up in later. Either way, apps aren't running, but hibernation goes 'deeper' than mere sleep. – Marc B Aug 14 '12 at 00:58
  • There's a difference concerning power consumption. – c00000fd Aug 14 '12 at 01:00
  • @MarcB, `WM_QUERYENDSESSION` or something. The thing is that Vista or 7 changes the whole procedure so you can't stop the shutdown for too long. On the other hand, [`WM_POWERBROADCAST`](http://msdn.microsoft.com/en-us/library/aa373247(VS.85).aspx) might be what you're after. – chris Aug 14 '12 at 01:01
  • 4
    You will *never* get is a promise that Windows shuts down a certain way. It is not around to ensure that the promise is accurate. It is just as dead when the machine sleeps vs hibernates. No way it can guarantee that machine won't drain the battery and turn the sleep into a reboot. – Hans Passant Aug 14 '12 at 01:09

1 Answers1

7

From what I've gathered, there's no way for user mode code to differentiate between suspend and hibernate. There is a kernel mode enum called SYSTEM_POWER_STATE that contains the states you are looking for but you can't get that state in user mode.

Best you can do is set the callback to catch WM_POWERBROADCAST messages. The PBT_APMQUERYSUSPEND message is sent when a suspend or hibernate is about to occur. After that, if the suspend is not cancelled, you will receive PBT_APMSUSPEND. On resume, the PBT_APMRESUMEAUTOMATIC message will be sent.

VoidStar
  • 581
  • 3
  • 6
  • Thanks VoidStar. I was wondering if I can get to that SYSTEM_POWER_STATE struct from a local service? How would I use it to make a distinction though? – c00000fd Aug 14 '12 at 03:38
  • 2
    See [my answer here](http://stackoverflow.com/a/6187658/103167), which explains why it isn't possible. – Ben Voigt Aug 14 '12 at 03:41
  • Yes, thank you. I guess I see your point about system transitioning from one state to another. But in that case wouldn't it be nice for the OS to report the state it entered first and then the state it resumed from. But, I guess that's asking too much, isn't it? – c00000fd Aug 14 '12 at 03:50