Hi peeps,
I need to write a little prog that's triggered when a user switches user (as opposed to logs off).
My VB skills are.. well.. limited lol so I'm hoping for something fairly easy if poss!
Cheer all
Printable View
Hi peeps,
I need to write a little prog that's triggered when a user switches user (as opposed to logs off).
My VB skills are.. well.. limited lol so I'm hoping for something fairly easy if poss!
Cheer all
Welcome to VBForums ! :wave:
You'll need to use WTSRegisterSessionNotification() API and also need to subclass your form to receive the WM_WTSSESSION_CHANGE message.
Here is a VB6 example:
Form Code
vb Code:
Option Explicit Private Declare Function WTSRegisterSessionNotification _ Lib "Wtsapi32" (ByVal hWnd As Long, _ ByVal THISSESS As Long) As Long Private Declare Function WTSUnRegisterSessionNotification _ Lib "Wtsapi32" (ByVal hWnd As Long) As Long Private Const NOTIFY_FOR_ALL_SESSIONS As Long = 1 Private Const NOTIFY_FOR_THIS_SESSION As Long = 0 Private Sub Form_Load() HookWindow Me.hWnd Call WTSRegisterSessionNotification(Me.hWnd, NOTIFY_FOR_ALL_SESSIONS) End Sub
Module Code
vb Code:
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
Also see these links for detailed description:
http://support.microsoft.com/kb/310153
http://support.microsoft.com/kb/841291
http://msdn.microsoft.com/library/de..._switching.asp
http://msdn.microsoft.com/library/de...tification.asp
http://msdn.microsoft.com/library/de...ion_change.asp
Thanks for the welcome - and a million thank-you's! Exactly what I was looking for! :thumb: :bigyello:
If you consider this resolved, you could help us out by pulling down the Thread Tools menu and click the Mark Thread Resolved menu item. That will let everyone know that you have your answer.
Thank you. :)
My apologies.... tis now done :) Cheers!
No apologies are necessary.
It is just something that we make sure all of our newer members are aware of. It really makes things a lot easier on folks that surf through different sections looking for threads that still require some assistance. :)