Home > Software engineering >  VB calling CallWindowProc, Visual basic has stopped work?
VB calling CallWindowProc, Visual basic has stopped work?

Time:11-06

On the Internet, I found the following method to judge whether the computer is turned off, and then decide whether program exits confirmation dialog box, the HTML code is as follows, but each exit the program, will appear "Visual basic has stopped work", please help to find out what is going on

Private Sub Form_Load ()


LpPrevWndProc=SetWindowLong (Me) hWnd GWL_WNDPROC, AddressOf WindowProcShutdown)
GShutdown=False

End Sub

Private Sub Form_QueryUnload (Cancel the As an Integer, UnloadMode As Integer)

Dim Temp As Long
Dim f As the Form
Dim res As Long

'not shutdown confirm software requirements when closing
If gShutdown=True Then
Res=vbYes
The Else
Res=MsgBox (" Are you sure to exit?" , vbInformation + vbYesNo + vbDefaultButton2, Caption)
End the If

If res=vbNo Then
Cancel Cancel=1 '
The Else 'res=vbYes
'window function to restore the original

Temp=SetWindowLong (Me) hWnd GWL_WNDPROC, lpPrevWndProc)

For Each f Forms In
Unload f
Next

End 'Close

End the If

End Sub


========module==========================

Public lpPrevWndProc As Long 'function to store the original window handle
Public Const GWL_WNDPROC=4 & amp; 'specified window function to replace the original constant
Public gShutdown As Boolean 'shutdown set this variable to bypass the software shut confirmation dialog
Public fno As Long
'specified window function called the API function definition
Public 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
'set the Windows long parameters of API function definition
'(this procedure is set in the specified window function to replace the original window function, window function to return to the original address, when nIndex=GWL_WNDPROC)
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

'our own window function
The Public Function WindowProcShutdown (ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam) As Long As Long

If uMsg=17 Then 'WM_QUERYENDSESSION
GShutdown=True
The Else
If uMsg=22 Then 'WM_ENDSESSION
If wParam=0 Then 'representative will smooth shutdown or LogOff, there will have to do normal end the program operation
GShutdown=True
End the If
End the If
End the If
'will be sent to the Window Procedure of the original
WindowProcShutdown=CallWindowProc (lpPrevWndProc hWnd, uMsg, wParam, lParam)

End the Function
  • Related