Results 1 to 6 of 6

Thread: [RESOLVED] Using 'switch user' as a trigger

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    3

    Resolved [RESOLVED] Using 'switch user' as a trigger

    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

  2. #2
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Detect Fast User Switching

    Welcome to VBForums !

    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:
    1. Option Explicit
    2.  
    3. Private Declare Function WTSRegisterSessionNotification _
    4.                 Lib "Wtsapi32" (ByVal hWnd As Long, _
    5.                                 ByVal THISSESS As Long) As Long
    6.  
    7. Private Declare Function WTSUnRegisterSessionNotification _
    8.                 Lib "Wtsapi32" (ByVal hWnd As Long) As Long
    9.  
    10. Private Const NOTIFY_FOR_ALL_SESSIONS As Long = 1
    11. Private Const NOTIFY_FOR_THIS_SESSION As Long = 0
    12.  
    13. Private Sub Form_Load()
    14.  
    15.     HookWindow Me.hWnd
    16.     Call WTSRegisterSessionNotification(Me.hWnd, NOTIFY_FOR_ALL_SESSIONS)
    17.  
    18. End Sub

    Module Code
    vb Code:
    1. Option Explicit
    2.  
    3. Public Declare Function CallWindowProc _
    4.                Lib "user32" _
    5.                Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
    6.                                         ByVal hWnd As Long, _
    7.                                         ByVal msg As Long, _
    8.                                         ByVal wParam As Long, _
    9.                                         ByVal lParam As Long) As Long
    10.  
    11. Public Declare Function SetWindowLong _
    12.                Lib "user32" _
    13.                Alias "SetWindowLongA" (ByVal hWnd As Long, _
    14.                                        ByVal nIndex As Long, _
    15.                                        ByVal dwNewLong As Long) As Long
    16.  
    17. Public Const WM_WTSSESSION_CHANGE As Long = &H2B1
    18. Public Const WTS_CONSOLE_CONNECT As Long = 1
    19. Public Const WTS_CONSOLE_DISCONNECT As Long = 2
    20. Public Const WTS_REMOTE_CONNECT As Long = 3
    21. Public Const WTS_REMOTE_DISCONNECT As Long = 4
    22. Public Const WTS_SESSION_LOGON As Long = 5
    23. Public Const WTS_SESSION_LOGOFF As Long = 6
    24. Public Const WTS_SESSION_LOCK As Long = 7
    25. Public Const WTS_SESSION_UNLOCK As Long = 8
    26. Public Const WTS_SESSION_REMOTE_CONTROL As Long = 9
    27. Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
    28. Public Const LANG_NEUTRAL = &H0
    29. Public Const GWL_WNDPROC As Long = (-4)
    30. Public Const WM_DESTROY = &H2
    31. Public lPrevProc As Long
    32.  
    33. Public Sub HookWindow(ByVal lHandle As Long)
    34.  
    35.     If lPrevProc = 0 Then
    36.         lPrevProc = SetWindowLong(lHandle, GWL_WNDPROC, AddressOf WindowProc)
    37.     Else
    38.         Call SetWindowLong(lHandle, GWL_WNDPROC, lPrevProc)
    39.     End If
    40.  
    41. End Sub
    42.  
    43. Public Function WindowProc(ByVal hWnd As Long, _
    44.                            ByVal iMsg As Long, _
    45.                            ByVal wParam As Long, _
    46.                            ByVal lParam As Long) As Long
    47.  
    48.     Select Case iMsg
    49.         Case WM_WTSSESSION_CHANGE
    50.  
    51.             Select Case wParam
    52.                 Case WTS_CONSOLE_CONNECT
    53.                     MsgBox "A session was connected to the console session at" & Now
    54.                 Case WTS_CONSOLE_DISCONNECT
    55.                     MsgBox "A session was disconnected from the console session at" & Now
    56.                 Case WTS_REMOTE_CONNECT
    57.                     MsgBox "A session was connected to the remote session at" & Now
    58.                 Case WTS_REMOTE_DISCONNECT
    59.                     MsgBox "A session was disconnected from the remote session at" & Now
    60.                 Case WTS_SESSION_LOGON
    61.                     MsgBox "A user has logged on to the session at" & Now
    62.                 Case WTS_SESSION_LOGOFF
    63.                     MsgBox "A user has logged off the session at" & Now
    64.                 Case WTS_SESSION_LOCK
    65.                     MsgBox "A session has been locked at" & Now
    66.                 Case WTS_SESSION_UNLOCK
    67.                     MsgBox "A session has been unlocked at" & Now
    68.                 Case WTS_SESSION_REMOTE_CONTROL
    69.                     MsgBox "A session has changed its remote controlled status. To determine the status, call GetSystemMetrics and check the SM_REMOTECONTROL metric at" & Now
    70.             End Select
    71.  
    72.         Case WM_DESTROY
    73.             HookWindow Form1.hWnd
    74.     End Select
    75.  
    76.     WindowProc = CallWindowProc(lPrevProc, hWnd, iMsg, wParam, lParam)
    77.  
    78. 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
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    3

    Re: Using 'switch user' as a trigger

    Thanks for the welcome - and a million thank-you's! Exactly what I was looking for!

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Using 'switch user' as a trigger

    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    3

    Re: [RESOLVED] Using 'switch user' as a trigger

    My apologies.... tis now done Cheers!

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] Using 'switch user' as a trigger

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width