Jump to content
Sign in to follow this  

Translate from VB please

Recommended Posts


I found 2 little pieces of code written in VB, it's to interrupt windows shutdown.

I want to use it in AutoIt to do just a quick thing before windows shuts down.

Please translate, thanks in advance!

1st one :

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = vbAppWindows Then
        'Windows is shutting down
        'do your code here!
    End If
End Sub

2nd one :

' 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 Function

Form Code from 2nd script :

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

Share this post

Link to post
Share on other sites

Just register a message, and call that function when the user has it restarted/shutdowned (is that a word?)

Edited by Swift

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.