Programming: How to prevent shutdown using VB?

crossnet

New member
Local time
2:46 AM
Messages
32
Location
Sorowako
Hello, sorry if i post in wrong thread. I'm in project to build an i-cafe software for my own i-cafe. I need the code to prevent windows 7 for shutdown if my application still running. My current code work fine in windows xp without any problem, but not in vista and 7. There are anyone here can help me fo this issue?
Any help would be appreciate, thank you!
 

My Computer

OS
Windows 7
CPU
INTEL DUAL CORE E5500 2.8GHz
Motherboard
FOXCONN G41MX-K
Memory
VISIPRO DDR2 1GBX2 PC6400
Graphics Card(s)
ONBOARD
Sound Card
ONBOARD
Monitor(s) Displays
LG LCD 17" (L177WSB)
Screen Resolution
1440 X 900
Hard Drives
SEAGATE BARACUDA SATA 250GB 7200RPM
PSU
STANDARD (CASING BUILT-IN) 450W
Case
ADVANCE
Cooling
STANDARD (INTEL PROCESSOR HEATSHINK)
Keyboard
STANDARD
Mouse
OPTIC STANDARD
Can you post the code so that we can have a look at it. Parts may need to be rewritten and reorganised.
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dwarf Dwf/11/2012 r09/2013
OS
Windows 8.1 Pro RTM x64
CPU
Intel Core-i5-3570K 4-core @ 3.4GHz (Ivy Bridge) (OC 4.4GHz)
Motherboard
ASRock Z77 Extreme4-M
Memory
4 x 4GB DDR3-1600 Corsair Vengeance CMZ8GX3M2A1600C9B (16GB)
Graphics Card(s)
MSI GeForce GTX770 Gaming OC 2GB
Sound Card
Realtek High Definition on board solution (ALC 898)
Monitor(s) Displays
ViewSonic VA1912w Widescreen (VGA)
Screen Resolution
1440x900
Hard Drives
OCZ Agility 3 SSD 120GB SATA III x2 (RAID 0)
Samsung HD501LJ 500GB SATA II x2
Hitachi HDS721010CLA332 1TB SATA II
Iomega 1.5TB Ext USB 2.0
WD 2.0TB Ext USB 3.0
PSU
XFX Pro Series 850W Semi-Modular
Case
Gigabyte IF233
Cooling
1 x 120mm Front Inlet 1 x 120mm Rear Exhaust
Keyboard
Microsoft Comfort Curve Keyboard 3000 (USB)
Mouse
Microsoft Comfort Mouse 3000 for Business (USB)
Internet Speed
NetGear DG834Gv3 ADSL Modem/Router (Ethernet) ~4.0 Mb/s (O2)
Antivirus
Avast! 8.0.1497
Browser
IE 11
Other Info
Optical Drive: HL-DT-ST BD-RE BH10LS30 SATA Bluray
Lexmark S305 Printer/Scanner/Copier (USB)
WEI Score: 8.1/8.1/8.5/8.5/8.25
Asus Eee PC 1011PX Netbook (Windows 7 x86 Starter)
Here my code:

FYI, i use this code in Microsoft Access 2003

Code:
Option Explicit
 
Dim exittes As Boolean, cekexit As Boolean 
Dim wCaption As String
 
Private Declare Function GetActiveWindow Lib "user32" () As Long 
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
 
 
Private Sub Form_Load()
exittes = False
cekexit = False
End Sub
 
Private Sub Form_Unload(Cancel As Integer)
'Code for prevent close and windows shutdown
cekexit = True
If exittes = False Then
Cancel = 1
End If
End Sub
 
Private Sub Form_Timer()
'This code for closing screen dialog to force clossing the application in vista & 7 
Dim WsShell
Set WsShell = CreateObject("WScript.Shell")
If (IsNull(wCaption)) And
(Isnull(GetFileName(GetHwndEXE(GetForegroundWindow())))) Then
Me.SetFocus
WsShell.SendKeys "{ESC}"
End If
If cekexit = True Then
Me.SetFocus
WsShell.SendKeys "{ESC}"
cekexit = False
End If
End Sub
 
Function GetWindowCaption()
Dim foreground_hwnd As Long
Dim Length As Long
 
foreground_hwnd = GetForegroundWindow()
 
wCaption= Space$(1024)
Length = GetWindowText(foreground_hwnd, wCaption, Len(txt))
wCaption= Left$(txt, Length)
End Function
 
Private Function CheckVersion() As Long
Dim tOS As OSVERSIONINFO
tOS.dwOSVersionInfoSize = Len(tOS)
Call GetVersionEx(tOS)
CheckVersion = tOS.dwPlatformId
End Function
 
Public Function GetHwndEXE(ByVal hWnd As Long) As String
Dim lProcessID As Long, lThread As Long
Dim lProcessHandle As Long
Dim sName As String, lModule As Long
Dim bMore As Boolean, tPROCESS As PROCESSENTRY32
Dim lSnapShot As Long
 
lThread = GetWindowThreadProcessId(hWnd, lProcessID)
 
If CheckVersion() = VER_PLATFORM_WIN32_WINDOWS Then
'Windows 9x
'Create a SnapShot of the Currently Running Processes
lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If lSnapShot < 0 Then Exit Function
 
tPROCESS.dwSize = Len(tPROCESS)
 
'Enumerate those processes until we find a match
bMore = Process32First(lSnapShot, tPROCESS)
While bMore And tPROCESS.th32ProcessID <> lProcessID
bMore = Process32Next(lSnapShot, tPROCESS)
Wend
 
'If a match was found, get the EXE Path and Filename
If tPROCESS.th32ProcessID = lProcessID Then
sName = Left$(tPROCESS.szExeFile, InStr(tPROCESS.szExeFile, Chr(0)) - 1)
GetHwndEXE = sName
End If
 
Else
'Win NT
'Create an Instance of the Process
lProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0&, lProcessID)
 
'If the Process was successfully created, get the EXE
If lProcessHandle Then
'Just get the First Module, all we need is the Handle to get the Filename..
If EnumProcessModules(lProcessHandle, lModule, 4, 0&) Then
sName = Space(260)
Call GetModuleFileNameExA(lProcessHandle, lModule, sName, Len(sName))
GetHwndEXE = sName
End If
'Close the Process Handle
Call CloseHandle(lProcessHandle)
End If
End If
End Function
 
 
Public Function GetFileName(flname As String) As String
 
Dim posn As Integer, i As Integer
Dim fName As String
 
posn = 0
'find the position of the last "\" character in filename
For i = 1 To Len(flname)
If (Mid(flname, i, 1) = "\") Then posn = i
Next i
 
'get filename without path
fName = Right(flname, Len(flname) - posn)
 
GetFileName = fName
End Function
 
Last edited by a moderator:

My Computer

OS
Windows 7
CPU
INTEL DUAL CORE E5500 2.8GHz
Motherboard
FOXCONN G41MX-K
Memory
VISIPRO DDR2 1GBX2 PC6400
Graphics Card(s)
ONBOARD
Sound Card
ONBOARD
Monitor(s) Displays
LG LCD 17" (L177WSB)
Screen Resolution
1440 X 900
Hard Drives
SEAGATE BARACUDA SATA 250GB 7200RPM
PSU
STANDARD (CASING BUILT-IN) 450W
Case
ADVANCE
Cooling
STANDARD (INTEL PROCESSOR HEATSHINK)
Keyboard
STANDARD
Mouse
OPTIC STANDARD
:sleepy::sleepy::sleepy::sleepy::sleepy::sleepy::sleepy:
 

My Computer

OS
Windows 7
CPU
INTEL DUAL CORE E5500 2.8GHz
Motherboard
FOXCONN G41MX-K
Memory
VISIPRO DDR2 1GBX2 PC6400
Graphics Card(s)
ONBOARD
Sound Card
ONBOARD
Monitor(s) Displays
LG LCD 17" (L177WSB)
Screen Resolution
1440 X 900
Hard Drives
SEAGATE BARACUDA SATA 250GB 7200RPM
PSU
STANDARD (CASING BUILT-IN) 450W
Case
ADVANCE
Cooling
STANDARD (INTEL PROCESSOR HEATSHINK)
Keyboard
STANDARD
Mouse
OPTIC STANDARD
I've asked for more help. :)
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dwarf Dwf/11/2012 r09/2013
OS
Windows 8.1 Pro RTM x64
CPU
Intel Core-i5-3570K 4-core @ 3.4GHz (Ivy Bridge) (OC 4.4GHz)
Motherboard
ASRock Z77 Extreme4-M
Memory
4 x 4GB DDR3-1600 Corsair Vengeance CMZ8GX3M2A1600C9B (16GB)
Graphics Card(s)
MSI GeForce GTX770 Gaming OC 2GB
Sound Card
Realtek High Definition on board solution (ALC 898)
Monitor(s) Displays
ViewSonic VA1912w Widescreen (VGA)
Screen Resolution
1440x900
Hard Drives
OCZ Agility 3 SSD 120GB SATA III x2 (RAID 0)
Samsung HD501LJ 500GB SATA II x2
Hitachi HDS721010CLA332 1TB SATA II
Iomega 1.5TB Ext USB 2.0
WD 2.0TB Ext USB 3.0
PSU
XFX Pro Series 850W Semi-Modular
Case
Gigabyte IF233
Cooling
1 x 120mm Front Inlet 1 x 120mm Rear Exhaust
Keyboard
Microsoft Comfort Curve Keyboard 3000 (USB)
Mouse
Microsoft Comfort Mouse 3000 for Business (USB)
Internet Speed
NetGear DG834Gv3 ADSL Modem/Router (Ethernet) ~4.0 Mb/s (O2)
Antivirus
Avast! 8.0.1497
Browser
IE 11
Other Info
Optical Drive: HL-DT-ST BD-RE BH10LS30 SATA Bluray
Lexmark S305 Printer/Scanner/Copier (USB)
WEI Score: 8.1/8.1/8.5/8.5/8.25
Asus Eee PC 1011PX Netbook (Windows 7 x86 Starter)
crossnet , have a look at this Visual Basic Component Shutdown Rules and also look at this code
  1. <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1">'constants needed, form level

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> Private Const WM_QUERYENDSESSION As System.Int32 = &H11

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> Private Const WM_CANCELMODE As System.Int32 = &H1F

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1">
    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> 'the sub to intercept the windows messages

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> Protected Overrides Sub WndProc(ByRef ex As Message)

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> If ex.Msg = WM_QUERYENDSESSION Then

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> 'cancel the message

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> Dim MyMsg As New Message

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> MyMsg.Msg = WM_CANCELMODE

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> MyBase.WndProc(MyMsg)

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> Else

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> 'send the message as normal

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> MyBase.WndProc(ex)

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> End If
  2. End Sub
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
self build
OS
win 7 ultimate32bit, Win8.1pro wmc 32bit
CPU
amd phenom x4 9600
Motherboard
asus m2n32-sli deluxe
Memory
corsair twinxs 2x2gb
Graphics Card(s)
2x nvidia 1gb 8500gt
Sound Card
onboard
Monitor(s) Displays
23" PB Viseo 233d
Screen Resolution
1920x1080
Hard Drives
maxtor sata 500gb
maxtor sata 320gb
fujitsu sata200gb
PSU
oryxx tornado 750w
Case
thermaltake xaser lll
Cooling
artic freezer64 pro + 7 case fans
crossnet, why do you want to prevent shut down? ,if the application crashes you could be up the creek without a paddle :(
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
self build
OS
win 7 ultimate32bit, Win8.1pro wmc 32bit
CPU
amd phenom x4 9600
Motherboard
asus m2n32-sli deluxe
Memory
corsair twinxs 2x2gb
Graphics Card(s)
2x nvidia 1gb 8500gt
Sound Card
onboard
Monitor(s) Displays
23" PB Viseo 233d
Screen Resolution
1920x1080
Hard Drives
maxtor sata 500gb
maxtor sata 320gb
fujitsu sata200gb
PSU
oryxx tornado 750w
Case
thermaltake xaser lll
Cooling
artic freezer64 pro + 7 case fans
How about this?

Code:
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

or

http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionending.aspx

or a quick google:

Code:
' 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
'http://www.ilook.fsnet.co.uk/vb/vbntserv.htm
'
Public Const GWL_WNDPROC = (-4)
Public Const WM_ENDSESSION = &H16
Public Const WM_QUERYENDSESSION = &H11
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.
'EWX_SHUTDOWN = 1
'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
If uMsg = WM_QUERYENDSESSION Then
   '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

Form

Code:
Private Sub Form_Load()
Hook (Me.hwnd)
End Sub
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell OP7010
OS
Windows 7 Enterprise (x64); Windows Server 2008 R2 (x64)
Memory
16GB
Monitor(s) Displays
4 Dell 24" LCD
Screen Resolution
1280x1024
Keyboard
Dell
Mouse
Dell Optical
Internet Speed
40meg
crossnet , have a look at this Visual Basic Component Shutdown Rules and also look at this code
  1. <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1">'constants needed, form level

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> Private Const WM_QUERYENDSESSION As System.Int32 = &H11

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> Private Const WM_CANCELMODE As System.Int32 = &H1F

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1">
    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> 'the sub to intercept the windows messages

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> Protected Overrides Sub WndProc(ByRef ex As Message)

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> If ex.Msg = WM_QUERYENDSESSION Then

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> 'cancel the message

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> Dim MyMsg As New Message

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> MyMsg.Msg = WM_CANCELMODE

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> MyBase.WndProc(MyMsg)

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> Else

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> 'send the message as normal

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> MyBase.WndProc(ex)

    <LI style="FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace; COLOR: black; FONT-WEIGHT: normal" itxtvisited="1"> End If
  2. End Sub

Thank you, i will test it
 

My Computer

OS
Windows 7
CPU
INTEL DUAL CORE E5500 2.8GHz
Motherboard
FOXCONN G41MX-K
Memory
VISIPRO DDR2 1GBX2 PC6400
Graphics Card(s)
ONBOARD
Sound Card
ONBOARD
Monitor(s) Displays
LG LCD 17" (L177WSB)
Screen Resolution
1440 X 900
Hard Drives
SEAGATE BARACUDA SATA 250GB 7200RPM
PSU
STANDARD (CASING BUILT-IN) 450W
Case
ADVANCE
Cooling
STANDARD (INTEL PROCESSOR HEATSHINK)
Keyboard
STANDARD
Mouse
OPTIC STANDARD
How about this?

Code:
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
or

SystemEvents.SessionEnding Event (Microsoft.Win32)

or a quick google:

Code:
' 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
'http://www.ilook.fsnet.co.uk/vb/vbntserv.htm
'
Public Const GWL_WNDPROC = (-4)
Public Const WM_ENDSESSION = &H16
Public Const WM_QUERYENDSESSION = &H11
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.
'EWX_SHUTDOWN = 1
'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
If uMsg = WM_QUERYENDSESSION Then
   '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
Form

Code:
Private Sub Form_Load()
Hook (Me.hwnd)
End Sub

Thank you, i will test it and will report the result immediately.
 

My Computer

OS
Windows 7
CPU
INTEL DUAL CORE E5500 2.8GHz
Motherboard
FOXCONN G41MX-K
Memory
VISIPRO DDR2 1GBX2 PC6400
Graphics Card(s)
ONBOARD
Sound Card
ONBOARD
Monitor(s) Displays
LG LCD 17" (L177WSB)
Screen Resolution
1440 X 900
Hard Drives
SEAGATE BARACUDA SATA 250GB 7200RPM
PSU
STANDARD (CASING BUILT-IN) 450W
Case
ADVANCE
Cooling
STANDARD (INTEL PROCESSOR HEATSHINK)
Keyboard
STANDARD
Mouse
OPTIC STANDARD

My Computer

OS
Windows 7
CPU
INTEL DUAL CORE E5500 2.8GHz
Motherboard
FOXCONN G41MX-K
Memory
VISIPRO DDR2 1GBX2 PC6400
Graphics Card(s)
ONBOARD
Sound Card
ONBOARD
Monitor(s) Displays
LG LCD 17" (L177WSB)
Screen Resolution
1440 X 900
Hard Drives
SEAGATE BARACUDA SATA 250GB 7200RPM
PSU
STANDARD (CASING BUILT-IN) 450W
Case
ADVANCE
Cooling
STANDARD (INTEL PROCESSOR HEATSHINK)
Keyboard
STANDARD
Mouse
OPTIC STANDARD
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 Computer

OS
Windows 7
CPU
INTEL DUAL CORE E5500 2.8GHz
Motherboard
FOXCONN G41MX-K
Memory
VISIPRO DDR2 1GBX2 PC6400
Graphics Card(s)
ONBOARD
Sound Card
ONBOARD
Monitor(s) Displays
LG LCD 17" (L177WSB)
Screen Resolution
1440 X 900
Hard Drives
SEAGATE BARACUDA SATA 250GB 7200RPM
PSU
STANDARD (CASING BUILT-IN) 450W
Case
ADVANCE
Cooling
STANDARD (INTEL PROCESSOR HEATSHINK)
Keyboard
STANDARD
Mouse
OPTIC STANDARD
How about this?

Code:
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
or

SystemEvents.SessionEnding Event (Microsoft.Win32)

or a quick google:

Code:
' 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
'http://www.ilook.fsnet.co.uk/vb/vbntserv.htm
'
Public Const GWL_WNDPROC = (-4)
Public Const WM_ENDSESSION = &H16
Public Const WM_QUERYENDSESSION = &H11
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.
'EWX_SHUTDOWN = 1
'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
If uMsg = WM_QUERYENDSESSION Then
   '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
Form

Code:
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 Computer

OS
Windows 7
CPU
INTEL DUAL CORE E5500 2.8GHz
Motherboard
FOXCONN G41MX-K
Memory
VISIPRO DDR2 1GBX2 PC6400
Graphics Card(s)
ONBOARD
Sound Card
ONBOARD
Monitor(s) Displays
LG LCD 17" (L177WSB)
Screen Resolution
1440 X 900
Hard Drives
SEAGATE BARACUDA SATA 250GB 7200RPM
PSU
STANDARD (CASING BUILT-IN) 450W
Case
ADVANCE
Cooling
STANDARD (INTEL PROCESSOR HEATSHINK)
Keyboard
STANDARD
Mouse
OPTIC STANDARD
Back
Top