Detecting when windows is shutting down

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
what's the easiest way to detect when windows is shutting down?

i know it'll require the use of a timer, set to fire every second. but what i need to do is when windows is shutting down (well about to shut down) is run another program right before the desktop dissappears
 
I think you'll need a subclass for this. Or you did in regular vb anyway. You can do it by subclassing your apps main window(top window) and wait for the WM_QUERYENDSESSION message. Though i've never done it that should start you down the right path.
 
i'll look into that tonight, i know it's an api call i just didnt have time to start researching it last night
 
One way is to override WndProc of the form. I used this method in tray application to monitor if Windows was shutting down. Here be the code:

VB.NET:
Private Const WM_QUERYENDSESSION As Integer = &H11

Protected Overrides Sub WndProc(ByRef m As Message)

        If m.Msg = WM_QUERYENDSESSION Then

            'Put the code to run the program here

        End If

        MyBase.WndProc(m)

End Sub
 
Is there a way to do the same but in a module or a class?
Could you also explain me the use of &H11, i didnt knew you could use pointers in VB, I'm still studying VB and would appreciate the help.
 
Back
Top