2012-08-28 62 views
2

我通过RDP在一天之内关闭了连接到我的PC(Windows XP Pro,它运行24/7)。 我有一个后台进程,应该在RDP连接时做一些事情,但我无法想出一个方法来检测RDP连接的建立。检测RDP连接

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

任何想法?

回答

1

答案是wtsapi32.dll中的WTSRegisterSessionNotification()。 这标志着您接收WM_WTSSESSION_CHANGE通知,其WParam可能是WTS_REMOTE_CONNECT,WTS_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