Q:

Detect RDP connection

I connect to my PC (Windows XP Pro, which runs 24/7) through RDP on an off through the day.
I have a background process that should do some things upon RDP connection, but I couldn't figure a way to make it detect the establishing of the RDP connection.
No new processes are created, WTSQuerySessionInformation doesn't help (I connect to the same eternal Windows session).
Any ideas ?

A:

The answer is WTSRegisterSessionNotification() from wtsapi32.dll.
This signs you up for receiving WM_WTSSESSION_CHANGE notifications, whose WParam could be WTS_REMOTE_CONNECT, WTS_REMOTE_DISCONNECT. That does it.
Here is the simplest AutoIt implementation:
#include <GUIConstantsEx.au3>
#include <Date.au3>
#include <WindowsConstants.au3>

Global Const $hWTSAPI32 = DllOpen("wtsapi32.dll")
Global $i = 0, $tTime

_Main()

Func _Main()
    Local $hGUI

    ; Create GUI
    $hGUI = GUICreate("Session change detection", 600, 400)
;~  GUISetState()  ; show the window

    DllCall($hWTSAPI32, "int", "WTSRegisterSessionNotification", "hwnd", $hGUI, "dword", 1) ; NOTIFY_FOR_ALL_SESSIONS
    If @error Then 
        MsgBox(0,"", "Error calling WTSRegisterSessionNotification()")
        Exit
    EndIf

    GUIRegisterMsg(0x2B1, "WTSSESSION_CHANGE")  ; WM_WTSSESSION_CHANGE <=====================

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

EndFunc   ;==>_Main

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func WTSSESSION_CHANGE($hWndGUI, $MsgID, $WParam, $LParam)
    ; WTS_REMOTE_CONNECT = 0x3, WTS_REMOTE_DISCONNECT = 0x4
    ; WTS_SESSION_UNLOCK = 0x8, WTS_SESSION_LOGON = 0x5
    If $WParam = 3 Then
        $tTime = _Date_Time_GetSystemTime()
        MsgBox(0, "Caught a notification", "Remote session connected at " & _Date_Time_SystemTimeToDateTimeStr($tTime) )
        Exit
    EndIf
EndFunc

