how to detect windows hibernation

Posted on 2004-11-10
Last Modified: 2013-11-24
I need to detect when windows is initializing a hibernation, is it posible?
Question by:SuperDude1984
    LVL 6

    Expert Comment

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

    I am not sure if you can distinguish between a sleep and hibernate request. Anyway, I hope this helps.
    LVL 6

    Expert Comment

    sorry, mistyped that last one.

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

    I am not sure if you can distinguish between a sleep and hibernate request. Anyway, I hope this helps.

    Author Comment

    i'm gonna need a code sample (
    LVL 6

    Accepted Solution

    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
            End Select

        End Sub

    Author Comment

    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)
    LVL 6

    Expert Comment

    When you browse the MSDN, you will see a table like this at the end of the file:

    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.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now