我全天休息时通过RDP连接到我的PC (Windows XP Pro,它全天候运行)。我有一个后台进程,它应该在RDP连接上做一些事情,但我想不出一种方法来让它检测RDP连接的建立。
没有创建新的进程,WTSQuerySessionInformation没有帮助(我连接到同一个永恒的Windows会话)。
发布于 2012-09-04 17:55:44
答案是来自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
https://stackoverflow.com/questions/12165149
复制相似问题