I want my application to be always running. I have included password so that if someone tries to close the application, it will ask for password. Now I am trying to prevent windows from shutting down, restart or logging off while my application is running. Whenever it happens, my application will flash some message. I tried MsgHook component (MsgHoo32.OCX) from http://vb.mvps.org/tools/MsgHook/ to intercept windows message WM_QUERYENDSESSION
I have 2 problems.
1. In the WM_QUERYENDSESSION description it is mentioned that the application may return FALSE to cancel shutdown. How can I return a value while using MsgHook.
2. I used following code without returning any value. It works, but some other programs (mostly in system tray) are still unloaded. After that my program shows "Shutting Down" message & then shut down is cancelled.
Code:Private Const WM_QUERYENDSESSION = &H11 Private Const ENDSESSION_LOGOFF As Long = &H80000000 Private Const WM_CANCELMODE = &H1F Private Sub Form_Load() Msghook1.HwndHook = Me.hWnd 'intercept messages for my window Msghook1.Message(WM_QUERYENDSESSION) = True 'enable event for WM_QUERYENDSESSION End Sub Private Sub Msghook1_Message(ByVal msg As Long, ByVal wp As Long, ByVal lp As Long, result As Long) If msg = WM_QUERYENDSESSION Then ' result = False MsgBox "Shutting Down" ' Else ' result = Msghook1.InvokeWindowProc(msg, wp, lp) End If End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Msghook1.Message(WM_QUERYENDSESSION) = False Msghook1.HwndHook = 0 'unhook any window End Sub
How can I prevent shutdown so that no program should be closed?
Thanks




Reply With Quote