Link to home
Start Free TrialLog in
Avatar of gmailgmail
gmailgmail

asked on

my program need to know when user switch to another windows user account (logoff->switch user)

My program needs to know when user switchs to another windows user account (logoff->switch user)

I want that when the user switch to another windows account my program will know about it.

I need it since my program take lots of resource and i want to suspend it when the user switches to different account.

IMPORTANT:
If he makes log-off then there is no problem since the program is shutting down by windows.
The problem is when he makes log-off -> switch users.

How can my program can get a signal from windows that windows is going to switch user account.

OR

If there was some api like IsCurrentAccountInLogOffMode then i could use it from the first account
by using some timer.

Thanks
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Hi gmailgmail,

You need to use the WTSRegisterSessionNotification() function so that your app will receive the WM_WTSSESSION_CHANGE message when a "Fast User Switch" has occurred on the system.  More information can be found in these links:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/apcompat/apcompat/test_your_application_with_fast_user_switching.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/termserv/wtsregistersessionnotification.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/termserv/wm_wtssession_change.asp

~IM
Avatar of gmailgmail
gmailgmail

ASKER

Idle_Mind thanks


Can i have some simple example in vb.

I only found vb.new example.

i have this:
FORM:

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()
   Call WTSRegisterSessionNotification(Me.hwnd, NOTIFY_FOR_ALL_SESSIONS)
End Sub


Module:


Private Const WM_WTSSESSION_CHANGE As Long = &H2B1
Private Const WTS_CONSOLE_CONNECT As Long = 1
Private Const WTS_CONSOLE_DISCONNECT As Long = 2
Private Const WTS_REMOTE_CONNECT As Long = 3
Private Const WTS_REMOTE_DISCONNECT As Long = 4
Private Const WTS_SESSION_LOGON As Long = 5
Private Const WTS_SESSION_LOGOFF As Long = 6
Private Const WTS_SESSION_LOCK As Long = 7
Private Const WTS_SESSION_UNLOCK As Long = 8
Private Const WTS_SESSION_REMOTE_CONTROL As Long = 9

Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const LANG_NEUTRAL = &H0
Public OldProc As Long
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
                    Debug.Print "A session was connected to the console session."
                Case WTS_CONSOLE_DISCONNECT
                    Debug.Print "A session was disconnected from the console session."
                Case WTS_REMOTE_CONNECT
                    Debug.Print "A session was connected to the remote session."
                Case WTS_REMOTE_DISCONNECT
                    Debug.Print "A session was disconnected from the remote session."
                Case WTS_SESSION_LOGON
                    Debug.Print "A user has logged on to the session."
                Case WTS_SESSION_LOGOFF
                    Debug.Print "A user has logged off the session."
                Case WTS_SESSION_LOCK
                    Debug.Print "A session has been locked."
                Case WTS_SESSION_UNLOCK
                    Debug.Print "A session has been unlocked."
                Case WTS_SESSION_REMOTE_CONTROL
                    Debug.Print "A session has changed its remote controlled status. To determine the status, call GetSystemMetrics and check the SM_REMOTECONTROL metric."
            End Select
    End Select
End Function



How can i make the event to go into WindowProc function?

Thanks
?? someone?
I don't know if you can override WndProc in VB6.  You can, however, use a WH_CALLWNDPROC call to SetWindowsHookEx() to see messages before they're dispatched to the recipients.
ASKER CERTIFIED SOLUTION
Avatar of cookre
cookre
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks cookre .

But, i dont want to use hooks.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This kind of thing is clumsy and not very extendible in VB6.

VB.Net does it easily and elegantly...