Skip to content
TopicTracker
来自 devblogs.microsoft.com/oldnewthing查看原文
译文语言译文语言

为什么 GetLastInputInfo() 不返回我所模拟用户的信息?

GetLastInputInfo() 函数不关心模拟(impersonation)状态,它只返回当前线程所在会话的上次用户输入时间,而非被模拟用户的信息。文档中已明确说明了这一点。

背景速读

- <b>GetLastInputInfo()</b> 是 Windows 系统 API(应用程序编程接口),用于查询系统最后一次接收用户输入(如键盘或鼠标操作)的时间。在多用户场景下,该函数返回的是当前会话(session)的全局最后输入时间,而非某个特定用户的。<br>- <b>Impersonation(模拟)</b> 是 Windows 的安全机制:一个线程可以临时“模拟”另一个用户的身份,以便访问该用户有权访问的资源。这是服务程序或后台进程处理用户请求时的常见做法。<br>- 本文解答了一个典型问题:当一个线程模拟了用户 A,调用 GetLastInputInfo() 时,它得到的仍然是当前桌面会话的最后输入时间,而不是用户 A 专属的最后输入时间。原因是该 API 设计上不考虑线程模拟——它只反映物理输入设备的活动,而这是会话级别的全局状态。<br>- 作者 Raymond Chen 是微软资深工程师,从 2003 年起运营博客《The Old New Thing》,专门解释 Windows 内部工作原理和各种“为什么这样设计”的细节。

相关报道

  • The article explains that GetLastInputInfo() returns the last input time for the current desktop's input queue, not for a specific user or thread. Even if impersonating another user, the function still reports the last input on the same interactive desktop session. To track input per user, separate desktop sessions or custom monitoring is needed.