Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 473
ExpertNoob Avatar asked Sep 12 '26 07:09

ExpertNoob


1 Answers

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
like image 81
ExpertNoob Avatar answered Sep 16 '26 07:09

ExpertNoob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!