[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 964
  • Last Modified:

how to detect windows hibernation

Hello.
I need to detect when windows is initializing a hibernation, is it posible?
0
SuperDude1984
Asked:
SuperDude1984
  • 4
  • 2
1 Solution
 
abdullah716Commented:
I think you need to listen to the WM_POWERBROADCAST message and see if its value is PBT_APMQUERYSUSPENDFAILED

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/power/base/wm_powerbroadcast.asp

I am not sure if you can distinguish between a sleep and hibernate request. Anyway, I hope this helps.
0
 
abdullah716Commented:
sorry, mistyped that last one.
=====

I think you need to listen to the WM_POWERBROADCAST message and see if its value is PBT_APMQUERYSUSPEND

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/power/base/wm_powerbroadcast.asp

I am not sure if you can distinguish between a sleep and hibernate request. Anyway, I hope this helps.
0
 
SuperDude1984Author Commented:
i'm gonna need a code sample (vb.net)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
abdullah716Commented:
Ok. Here it is. Create a Windows Form. and paste this code into it. The code will intercept and halt Sleep" and "Hibernate".

Since this is a broadcast, you will see as many msgbox as you have open Windows.

I was not able to distinguish between "Sleep" and "Hibernate" as I told you.

<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Protected Overrides Sub WndProc(ByRef m As Message)

        Dim WM_POWERBROADCAST As Integer = &H218         ' 0x0218
        Dim PBT_APMQUERYSUSPEND As Integer = &H0         ' 0x0000
        Dim PBT_APMQUERYSTANDBY As Integer = &H1         ' 0x0001
        Dim BROADCAST_QUERY_DENY As Integer = &H424D5144 ' 0x424D5144
             
        ' Listen for WM_POWERBROADCAST message
        Select Case m.Msg
            Case WM_POWERBROADCAST
                ' Intercept the message
                MsgBox("WM_POWERBROADCAST Received")
                Select Case m.WParam.ToInt32
                    Case PBT_APMQUERYSUSPEND
                        MsgBox("PBT_APMQUERYSUSPEND Received")

                    Case PBT_APMQUERYSTANDBY
                        MsgBox("PBT_APMQUERYSTANDBY Received")
                End Select

                MsgBox("Cancelling the WM_POWERBROADCAST message")
                Dim pResult As New IntPtr(BROADCAST_QUERY_DENY)
                m.Result = pResult
            Case Else
                ' Pass the message to base class
                MyBase.WndProc(m)
        End Select

    End Sub
0
 
SuperDude1984Author Commented:
Thanks for the help dude...

Just wondering...
the constants that you use, like this one:
Dim WM_POWERBROADCAST As Integer = &H218

Where do you get them from?  how can you know the value? (&H218)
0
 
abdullah716Commented:
When you browse the MSDN, you will see a table like this at the end of the file:

---
Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation 4.0, Windows Me, Windows 98, and Windows 95.
Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT Server 4.0.
Header: Declared in Winuser.h; include Windows.h.
---

This tells that constants are declared in the Winuser.h file. This C++ header file is part of the Platform SDK. It comes with VS and you should find all files under:

C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\ *

Whenever, you need to know the value of a constant, search through that folder or the entire "C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7" folder. Then copy the values and declare it in you VB app.

* assuming that is your installation folder.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now