检测RDP连接

3

我通过RDP连接我的电脑(Windows XP Pro,全天候运行)。 我有一个后台进程,应该在RDP连接时执行一些操作,但是我无法找到一种方法使其检测到建立的RDP连接。

没有创建新进程,WTSQuerySessionInformation也没有帮助(我连接到相同的永久Windows会话)。

1个回答

2
答案是WTSRegisterSessionNotification(),来自wtsapi32.dll
这将使您接收WM_WTSSESSION_CHANGE通知,其WParam可能为WTS_REMOTE_CONNECTWTS_REMOTE_DISCONNECT。就这样。
以下是最简单的AutoIt实现:
#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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接