troubleshooting Question

Preventing shutdown works, preventing suspend doesn't

Avatar of Jappy-o
Jappy-o asked on
Visual Basic.NET
3 Comments1 Solution762 ViewsLast Modified:
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
Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 3 Comments.
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>


Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 3 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004