We help IT Professionals succeed at work.

Preventing shutdown works, preventing suspend doesn't

Jappy-o asked
Last Modified: 2012-06-27
Dear all,

I'm working on a little piece of code that should prevent my laptop from getting stolen at work. It monitors the network and AC powerline connections and raises an alarm when one of these is lost. In order to work, my program also needs to prevent shutdown and suspend (by the user, not through power saving options). To do this, I tried to use the piece of code below. The problem is: a shutdown attempt is succesfully cancelled, but suspend is not. My question is two-fold: why doesn't the code below prevent suspend (via the Start menu) and how can I make it to work?

Thank you in advance.
Sincerely, Jasper

CODE: (from my main form, constants are declared in a module also listed below)

Protected Overrides Sub WndProc(ByRef m As Message)
        If alarmStatus = AlarmStat.DisArmed Then 'Alarm is disarmed
        Else 'Alarm is armed
            Select Case m.Msg
                Case WM_QUERYENDSESSION
                    'Handle log-off and shutdown
                    Dim x As New Message
                    x.Msg = WM_CANCELMODE
                    Call RaiseAlarmDelay()
                Case WM_POWERBROADCAST
                    'Handle power messages
                    Select Case m.WParam
                        Case PBT_APMQUERYSTANDBY
                            'Handle standby request
                            Dim x As New Message
                            x.Msg = BROADCAST_QUERY_DENY
                            Call RaiseAlarmDelay()
                        Case PBT_APMQUERYSUSPEND
                            'Handle suspend request
                            Dim x As New Message
                            x.Msg = BROADCAST_QUERY_DENY
                            Call RaiseAlarmDelay()
                        Case Else
                            'Handle other power messages
                    End Select
                Case Else
                    'Handle all other messages
            End Select
        End If
    End Sub


    Public Const WM_QUERYENDSESSION As System.Int32 = &H11
    Public Const WM_CANCELMODE As System.Int32 = &H1F

    Public Const PBT_APMQUERYSUSPEND As System.Int32 = &H0
    Public Const PBT_APMQUERYSTANDBY As System.Int32 = &H1

    Public Const BROADCAST_QUERY_DENY As System.Int32 = &H424D5144
    Public Const WM_POWERBROADCAST As System.Int32 = &H218
End Module
Watch Question

Most Valuable Expert 2012
Top Expert 2008
This one is on us!
(Get your first solution completely free - no credit card required)


Hi Bob,

Thank you for your reply. Your code however still doesn't seem to prevent windows from suspending. I've been searching for a solution for quite some time now, and I just can't imagine there is no way of preventing a user from suspending or hibernating the computer.



Hi Bob,

At first I thought your solution didn't work, but in fact it does (my mistake, sorry). I tried your solution while 'MyBase.WndProc(m)' was still in the code, so my override WndProc function didn't properly return the message result. Anyway, it works now and your answer really pointed me in the right direction. I consider it answered. Thank you.


Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.