Option Explicit
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
Public Declare Function SetWindowLong _
Lib "user32" _
Alias "SetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Const WM_WTSSESSION_CHANGE As Long = &H2B1
Public Const WTS_CONSOLE_CONNECT As Long = 1
Public Const WTS_CONSOLE_DISCONNECT As Long = 2
Public Const WTS_REMOTE_CONNECT As Long = 3
Public Const WTS_REMOTE_DISCONNECT As Long = 4
Public Const WTS_SESSION_LOGON As Long = 5
Public Const WTS_SESSION_LOGOFF As Long = 6
Public Const WTS_SESSION_LOCK As Long = 7
Public Const WTS_SESSION_UNLOCK As Long = 8
Public Const WTS_SESSION_REMOTE_CONTROL As Long = 9
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Public Const LANG_NEUTRAL = &H0
Public Const GWL_WNDPROC As Long = (-4)
Public Const WM_DESTROY = &H2
Public lPrevProc As Long
Public Sub HookWindow(ByVal lHandle As Long)
If lPrevProc = 0 Then
lPrevProc = SetWindowLong(lHandle, GWL_WNDPROC, AddressOf WindowProc)
Else
Call SetWindowLong(lHandle, GWL_WNDPROC, lPrevProc)
End If
End Sub
Public Function WindowProc(ByVal hWnd As Long, _
ByVal iMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Select Case iMsg
Case WM_WTSSESSION_CHANGE
Select Case wParam
Case WTS_CONSOLE_CONNECT
MsgBox "A session was connected to the console session at" & Now
Case WTS_CONSOLE_DISCONNECT
MsgBox "A session was disconnected from the console session at" & Now
Case WTS_REMOTE_CONNECT
MsgBox "A session was connected to the remote session at" & Now
Case WTS_REMOTE_DISCONNECT
MsgBox "A session was disconnected from the remote session at" & Now
Case WTS_SESSION_LOGON
MsgBox "A user has logged on to the session at" & Now
Case WTS_SESSION_LOGOFF
MsgBox "A user has logged off the session at" & Now
Case WTS_SESSION_LOCK
MsgBox "A session has been locked at" & Now
Case WTS_SESSION_UNLOCK
MsgBox "A session has been unlocked at" & Now
Case WTS_SESSION_REMOTE_CONTROL
MsgBox "A session has changed its remote controlled status. To determine the status, call GetSystemMetrics and check the SM_REMOTECONTROL metric at" & Now
End Select
Case WM_DESTROY
HookWindow Form1.hWnd
End Select
WindowProc = CallWindowProc(lPrevProc, hWnd, iMsg, wParam, lParam)
End Function