是否可以从.NET中的全局键盘挂钩中确定当前用户
时间:2020-03-06 14:35:01 来源:igfitidea点击:
我想创建一个键盘和鼠标挂钩,它将作为Windows服务启动。我想监视全天使用该系统的各种用户的活动。即哪些用户在什么时候处于活动状态。
是否可以确定哪个用户将接收事件? (该服务将以单独的用户身份运行,因此不适合使用getCurrentUser)
解决方案
否,Environment.UserName不起作用,在输入接收方的上下文中未调用挂接过程。
确实,我认为_LL钩子是不可能的,如果使用.NET,则_LL钩子无疑是低级钩子。在我看来,它们在Windows甚至确定哪个桌面/应用程序将接收该事件之前就已执行得很好。尽管我自己从未使用过_LL挂钩,但我可能是错的。
@TcKs嗯,关于快速用户切换呢?
我不知道这些钩子是否会从远程桌面键盘接收事件?如果他们只获得本地键盘,那么我认为我们需要找到WinSta0的所有者。
其他方式:
WTSGetActiveConsoleSessionId函数允许我们获取活动会话的ID。具体的:
The WTSGetActiveConsoleSessionId function retrieves the Terminal Services session that is currently attached to the physical console. The physical console is the monitor, keyboard, and mouse. Note that it is not necessary that Terminal Services be running for this function to succeed.
然后,我们可以使用WTSQueryUserToken来获取用户的令牌,然后我们应该能够获得有关带有令牌的用户的信息。
此功能来自终端服务,但是文档说:
Note that it is not necessary that Terminal Services be running for this function to succeed.
所以我认为,这可以是方法。