I read Raymond’s post about detecting workstation events: http://blogs.msdn.com/oldnewthing/archive/2006/01/04/509194.aspx

 

To detect shell events, such as a new drive being added, see my blog: Run your code in response to a new drive being inserted

 

To detect workstation events, such as logon or session lock, run the code below which uses WTSRegisterSessionNotification. Lock the workstation (Try Windows Key + L). Unlock it. Connect/disconnect via Remote Desktop.

 

Tip: I edit the Remote Desktop properties (printer, password, sound properties, etc.) as I like, save them to a RDP file. I open the folder containing that file in Explorer. Just hit enter on that file to connect. Alt-Space, C to close the RD session. Voila: no low bandwidth input device (mouse) needed and minimal high bandwidth device (keyboard) input required.

 

#define NOTIFY_FOR_ALL_SESSIONS     1

#define NOTIFY_FOR_THIS_SESSION     0

#define WM_WTSSESSION_CHANGE            0x02B1

 

#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 SM_REMOTESESSION        0x1000

 

PUBLIC oTS

oTS = CREATEOBJECT("TS")

 

DEFINE CLASS TS as custom

      PROCEDURE init

            DECLARE integer WTSRegisterSessionNotification IN wtsapi32 integer hWnd, integer flags

            DECLARE integer WTSUnRegisterSessionNotification IN wtsapi32 integer hWnd

            DECLARE integer Beep IN WIN32API integer dwFreq, integer dwMillisec

            DECLARE integer GetSystemMetrics IN WIN32API integer

            WTSRegisterSessionNotification(_vfp.HWnd,NOTIFY_FOR_THIS_SESSION)

            BINDEVENT(_VFP.hWnd, WM_WTSSESSION_CHANGE,this,"HandleMsg")

            CREATE CURSOR LOG (dt t, msg c(40),Remote i)

            SET REFRESH TO 2

            BROWSE LAST NOWAIT

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

            LOCAL nRetvalue

            nRetvalue=0

            ?PROGRAM(),DATETIME(),TRANSFORM(wParam,"@0x"),TRANSFORM(lParam,"@0x")," ",GetSystemMetrics(SM_REMOTESESSION)

            Beep(wParam*100,400)

            DO CASE

            CASE  wParam=WTS_CONSOLE_CONNECT

                  INSERT INTO LOG VALUES (DATETIME(),"WTS_CONSOLE_CONNECT",GetSystemMetrics(SM_REMOTESESSION))

                  ?"WTS_CONSOLE_CONNECT"

            CASE  wParam=WTS_CONSOLE_DISCONNECT

                  INSERT INTO LOG VALUES (DATETIME(),"WTS_CONSOLE_DISCONNECT",GetSystemMetrics(SM_REMOTESESSION))

                  ?"WTS_CONSOLE_DISCONNECT"

            CASE  wParam=WTS_REMOTE_CONNECT

                  INSERT INTO LOG VALUES (DATETIME(),"WTS_REMOTE_CONNECT",GetSystemMetrics(SM_REMOTESESSION))

                  ?"WTS_REMOTE_CONNECT"

            CASE  wParam=WTS_REMOTE_DISCONNECT

                  INSERT INTO LOG VALUES (DATETIME(),"WTS_REMOTE_DISCONNECT",GetSystemMetrics(SM_REMOTESESSION))

                  ?"WTS_REMOTE_DISCONNECT"

            CASE  wParam=WTS_SESSION_LOGON

                  INSERT INTO LOG VALUES (DATETIME(),"WTS_SESSION_LOGON",GetSystemMetrics(SM_REMOTESESSION))

                  ?"WTS_SESSION_LOGON"

            CASE  wParam=WTS_SESSION_LOGOFF

                  INSERT INTO LOG VALUES (DATETIME(),"WTS_SESSION_LOGOFF",GetSystemMetrics(SM_REMOTESESSION))

                  ?"WTS_SESSION_LOGOFF"

            CASE  wParam=WTS_SESSION_LOCK

                  INSERT INTO LOG VALUES (DATETIME(),"WTS_SESSION_LOCk",GetSystemMetrics(SM_REMOTESESSION))

                  ?"WTS_SESSION_LOCK"

            CASE  wParam=WTS_SESSION_UNLOCK

                  INSERT INTO LOG VALUES (DATETIME(),"WTS_SESSION_UNLOCK",GetSystemMetrics(SM_REMOTESESSION))

                  ?"WTS_SESSION_UNLOCK"

            CASE  wParam=WTS_SESSION_REMOTE_CONTROL

                  INSERT INTO LOG VALUES (DATETIME(),"WTS_SESSION_REMOTE_CONTROL",GetSystemMetrics(SM_REMOTESESSION))

                  ?"WTS_SESSION_REMOTE_CONTROL"

            ENDCASE

            RETURN nRetvalue

      PROCEDURE destroy

            ?PROGRAM()

            WTSUnRegisterSessionNotification(_vfp.HWnd)

           

ENDDEFINE