Reacting to Windows Session events.


Sometimes it’s useful to run some code in response to an event like somebody locking or unlocking your desktop via Ctrl-Alt-Del or WindowsKey+L or the screen saver activating


 


You can use the new VFP9 BindEvent to capture WM_WTSSESSION_CHANGE to accomplish this. There are other events that can be handled as seen below.


 


See Creating a VFP application as a service to make a VFP application survive user logon/logoff events. I haven’t tried capturing these events: please let me know if you try it.


 


Another Bindevent sample: Run your code in response to a new drive being inserted


Other screen saver topics:



 


 


#define WTS_CONSOLE_CONNECT                0x1


#define WTS_CONSOLE_DISCONNECT             0x2


#define WTS_REMOTE_CONNECT                 0x3


#define WTS_REMOTE_DISCONNECT              0x4


#define WTS_SESSION_LOGON                  0x5


#define WTS_SESSION_LOGOFF                 0x6


#define WTS_SESSION_LOCK                   0x7


#define WTS_SESSION_UNLOCK                 0x8


#define WTS_SESSION_REMOTE_CONTROL         0x9


#define NOTIFY_FOR_ALL_SESSIONS     1


#define NOTIFY_FOR_THIS_SESSION     0


 


#define WM_WTSSESSION_CHANGE            0x02B1


#define GWL_WNDPROC         (-4)


 


PUBLIC oevent


oevent=NEWOBJECT(“MyEvent”)


 


DEFINE CLASS MyEvent AS session


      dwOrigWindProc=0


      PROCEDURE init


            DECLARE integer GetWindowLong IN WIN32API ;


                  integer hWnd, ;


                  integer nIndex


            DECLARE integer CallWindowProc IN WIN32API ;


                  integer lpPrevWndFunc, ;


                  integer hWnd,integer Msg,;


                  integer wParam,;


                  integer lParam


            DECLARE integer DefWindowProc IN WIN32API ;


                  integer hWnd,integer Msg,;


                  integer wParam,;


                  integer lParam


                 


            DECLARE integer WTSRegisterSessionNotification IN wtsapi32 ;


                  integer hWnd,;


                  integer flags


            DECLARE integer WTSUnRegisterSessionNotification IN wtsapi32 ;


                  integer hWnd


                 


            THIS.dwOrigWindProc =GetWindowLong(_VFP.HWnd,GWL_WNDPROC)


            BINDEVENT(_vfp.hWnd, WM_WTSSESSION_CHANGE,this,”HandleMsg”)


            WTSRegisterSessionNotification(_VFP.hWnd, NOTIFY_FOR_THIS_SESSION)


      PROCEDURE destroy


            WTSUnRegisterSessionNotification(_VFP.hWnd)


      PROCEDURE HandleMsg(hWnd as Integer, msg as Integer, wParam as Integer, lParam as Integer)


            LOCAL nRetvalue,hDC


            nRetvalue=0


            DO CASE


            CASE msg=WM_WTSSESSION_CHANGE


                  ?DATETIME(),PROGRAM(),wParam


                  DO CASE


                  CASE wParam=WTS_SESSION_LOCK


                        ??”Session Lock”


                  CASE wParam=WTS_SESSION_UNLOCK


                        ??”Session UnLock”


                  ENDCASE


                  nRetvalue= CallWindowProc(this.dwOrigWindProc ,hWnd,msg,wParam,lParam)


            ENDCASE


            RETURN nRetvalue


ENDDEFINE


 


 


 


 

Comments (4)

  1. David Fung says:

    When I run the code, it complains that it cannot find WTSRegisterSessionNotification in wtsapi32. Am I missing something? I am running VFP9 on W2KP.

  2. CalvinH says:

    The link above says WinXP required

  3. David Fung says:

    Thanks! Sorry I didn’t go into the links.

  4. Alan Wayne says:

    Using vfp as a service with postmessage() to a seperate vfp desktop session ALWAYS FAILS with bindevent. I can find nothing that will make this work. Suggestions?