Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.

Windows 7: Programming: How to prevent shutdown using VB?

06 Jan 2011   #11

Windows 7

Quote   Quote: Originally Posted by Dwarf View Post
I've asked for more help.
Thanks for your help!

My System SpecsSystem Spec
06 Jan 2011   #12

Windows 7

Quote   Quote: Originally Posted by Dwarf View Post
Can you post the code so that we can have a look at it. Parts may need to be rewritten and reorganised.
This is for my i-cafe software
My System SpecsSystem Spec
06 Jan 2011   #13

Windows 7

Quote   Quote: Originally Posted by WindowsStar View Post
How about this?

Public Sub Handler_SessionEnding(ByVal sender As Object, ByVal e As Microsoft.Win32.SessionEndingEventArgs) 
        If e.Reason = Microsoft.Win32.SessionEndReasons.Logoff Then 
            MessageBox.Show("User is logging off") 
        ElseIf e.Reason = Microsoft.Win32.SessionEndReasons.SystemShutdown Then 
            MessageBox.Show("System is shutting down") 
        End If 
    End Sub 
 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load 
        AddHandler Microsoft.Win32.SystemEvents.SessionEnding, AddressOf Handler_SessionEnding 
    End Sub

SystemEvents.SessionEnding Event (Microsoft.Win32)

or a quick google:

' Original Code
' Created by E.Spencer - This code is public domain.
' Routines for running an app as an NT service.
' modified/rehashed by G.Crisp
' code modified to just detect and cancel windows shutdown - only a few unused bits deleted
'orginal code available from
Public Const GWL_WNDPROC = (-4)
Public Const WM_ENDSESSION = &H16
Public WndProc As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
' exiting from windows
Public Declare Function ExitWindowsEx Lib "user32.dll" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
'EWX_FORCE = 4 Force any applications to quit instead of prompting the user to close them.
'EWX_LOGOFF = 0 Log off the network.
'EWX_POWEROFF = 8 Shut down the system and, if possible, turn the computer off.
'EWX_REBOOT = 2 Perform a full reboot of the system.
'call this from your form
Public Sub Hook(Lwnd As Long)
Dim uProcess As Long
WndProc = SetWindowLong(Lwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
   'MsgBox "hw:" + CStr(hw) + " uMsg:" + CStr(uMsg) + " wParam:" + CStr(wParam)
   WindowProc = False 'send don't shut down
   'run code do what you want, then call ExitWindowsEx etc up to you
   Exit Function
ElseIf uMsg = WM_ENDSESSION Then
   'MsgBox "hw:" + CStr(hw) + " uMsg:" + CStr(uMsg) + " wParam:" + CStr(wParam)
   WindowProc = False
   'run code
   Exit Function
End If
WindowProc = CallWindowProc(WndProc, hw, uMsg, wParam, lParam)
End FunctionForm

Private Sub Form_Load()
Hook (Me.hwnd)
End Sub
I have tested it, but the result still same with my previous code. Windows screen dialog still appear when we try to shutdown. And your code make my another code freeze if mouse pointer in hover of my form.
My System SpecsSystem Spec


 Programming: How to prevent shutdown using VB?

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar help and support threads
Thread Forum
Windows has shutdown to prevent damage blue screen error
About a week ago I noticed my computer became uncharacteristically slow, so I ran a full computer virus scan to check for infections and nothing turned up. Yesterday I found the culprit of the slow down, it was a process called "system interrupts" it was using 25% of my processor all the time....
BSOD Help and Support
Sidebar prevent computer shutdown!
After a fresh SP1 install (with all updates) i encounter this (quite common apparently..) which is the sidebar freezing during shutdown and altering its process. So far i couldn't fix this. Probably some will respond "just turn off that sh**" but no i have some nice (and clean) widget i wanna...
General Discussion
Prevent Shutdown ????????????????????
I have to do a lot of works on my PC. But my son also play games from another standard user account. So he always shutdown my PC and I loose all my works. So is there any way to prevent shutdown while other users are logged on.
General Discussion
prevent screen-shutdown when switching user.
Is there a way that i can prevent the computer from turning off the screen while switching user? My screen reacts really slow when it gets turned off, then on again by the computer, so its just turns black for ages.
General Discussion
Prevent Network Card shutdown on ethernet cable removal
I need to know if this is a Windows 7 issue... I am running software which is licensed through my network card ID. I have recently bought a new laptop, running Windows 7 for the first time. Unless there is a network cable plugged in, I have no access at all to my card and so the software does...
Network & Sharing
Programming Help - Please!
Hey Guys, I'm trying to create a personal website for my new born daughter. I have a few ideas/thoughts in mind on how I want it to act and function. I already purchased the domain name & beginning to work on a few stuff. I need some programming help making the site "e-mail access only", meaning...
Chillout Room

Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 18:37.

Twitter Facebook Google+

Windows 7 Forums

Seven Forums Android App Seven Forums IOS App