3 Replies - 2196 Views - Last Post: 17 November 2012 - 08:50 AM Rate Topic: -----

#1 VbDev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-November 12

Prevent windows shutdown

Posted 09 November 2012 - 07:21 AM

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.




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

Is This A Good Question/Topic? 0
  • +

Replies To: Prevent windows shutdown

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,384
  • Joined: 10-May 07

Re: Prevent windows shutdown

Posted 09 November 2012 - 07:27 AM

I wouldn't do this with a program. I would do it with the Windows Policies. After all security & environment is what they are there for :

Quick Google Search
Was This Post Helpful? 0
  • +
  • -

#3 VbDev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-November 12

Re: Prevent windows shutdown

Posted 09 November 2012 - 11:45 PM

Thanks. As I am already developing the application, I am thinking to add this optional feature. "Prevent windows shutdown while software is running"

Anyway is there some setting in windows to prevent shutdown. If yes, how can I change the policies in the code?
Was This Post Helpful? 0
  • +
  • -

#4 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Prevent windows shutdown

Posted 17 November 2012 - 08:50 AM

Well, you could start by reading the link that no2pencil provided. It says "Quick google search". Try pressing that...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1