Explains that GetLastInputInfo() does not work with impersonation because it returns data for the calling session, not the impersonated user; to get last input from another session, you must use a helper process in that session.
<p>A customer had a Windows NT service process, and from that service process, they wanted to obtain the last input time for all signed-in users. Their strategy was to use <code>WTSQueryUserToken()</code> to get the token for each user, use that token to impersonate the user, and then call <code>GetLastInputInfo()</code> to get the last input time for that user. Unfortunately, the function always return the last input info for the service session, and since services are not interactive, it always says that there has been no input since the system booted.</p>
<p>Does <code>GetLastInputInfo()</code> work with impersonation?</p>
<p>Recall that <a title="Does this operation work when impersonating? The default answer is NO" href="https://devblogs.microsoft.com/oldnewthing/20110928-00/?p=9533"> the default answer to “Does this work when impersonating?” is “No”</a>. And in fact, the documentation for <code>GetLastInputInfo()</code> explicitly says that it doesn’t, if you read it closely.</p>
<blockquote class="q"><p>This function is useful for input idle detection. However, <b>GetLastInputInfo</b> does not provide system-wide user input information across all running sessions. Rather, <b>GetLastInputInfo</b> provides session-specific user input information <u>for only the session that invoked the function</u>.</p></blockquote>
<p>I underlined the important part. It reports on the last input information for the session that invoked the function. When the service impersonates, it updates its security context to align with that of the user being impersonated, but it doesn’t change the fact that that it is still running in session zero, the service session.</p>
<p>If you need to get last input information from another session, you will need a friend in that session to call it for you. Typically this is done by launching a helper process into the target session: The helper process collects the information you want and then sends the information to the service.</p>
<p><b>Bonus chatter</b>: A related question is “Does <code>GetAsyncKeyState</code> from a service?” The answer is technically yes, it works. However it probably doesn’t work the way you think. It returns the asynchronous key state for the desktop that the service is running in. And since services run in a non-interactive session, that desktop will never see any keyboard activity.</p>
<p>The post <a href="https://devblogs.microsoft.com/oldnewthing/20260618-00/?p=112444">Why doesn’t <CODE>Get­Last­Input­Info()</CODE> return info for the user I’m impersonating?</a> appeared first on <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a>.</p>
# Why doesn't GetLastInputInfo() return info for the user I'm impersonating? - The Old New Thing
Source: [https://devblogs.microsoft.com/oldnewthing/20260618-00?p=112444](https://devblogs.microsoft.com/oldnewthing/20260618-00?p=112444)
A customer had a Windows NT service process, and from that service process, they wanted to obtain the last input time for all signed\-in users\. Their strategy was to use`WTSQueryUserToken\(\)`to get the token for each user, use that token to impersonate the user, and then call`GetLastInputInfo\(\)`to get the last input time for that user\. Unfortunately, the function always return the last input info for the service session, and since services are not interactive, it always says that there has been no input since the system booted\.
Does`GetLastInputInfo\(\)`work with impersonation?
Recall that[the default answer to “Does this work when impersonating?” is “No”](https://devblogs.microsoft.com/oldnewthing/20110928-00/?p=9533)\. And in fact, the documentation for`GetLastInputInfo\(\)`explicitly says that it doesn’t, if you read it closely\.
> This function is useful for input idle detection\. However,**GetLastInputInfo**does not provide system\-wide user input information across all running sessions\. Rather,**GetLastInputInfo**provides session\-specific user input informationfor only the session that invoked the function\.
I underlined the important part\. It reports on the last input information for the session that invoked the function\. When the service impersonates, it updates its security context to align with that of the user being impersonated, but it doesn’t change the fact that that it is still running in session zero, the service session\.
If you need to get last input information from another session, you will need a friend in that session to call it for you\. Typically this is done by launching a helper process into the target session: The helper process collects the information you want and then sends the information to the service\.
**Bonus chatter**: A related question is “Does`GetAsyncKeyState`from a service?” The answer is technically yes, it works\. However it probably doesn’t work the way you think\. It returns the asynchronous key state for the desktop that the service is running in\. And since services run in a non\-interactive session, that desktop will never see any keyboard activity\.
### Category
## Author

Raymond has been involved in the evolution of Windows for more than 30 years\. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie\-jeebies\. The Web site spawned a book, coincidentally also titled The Old New Thing \(Addison Wesley 2007\)\. He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information\.
The article analyzes the psychological 'illusion of listening' where users perceive AI as empathetic due to linguistic cues, despite the lack of genuine understanding. It proposes design guidelines to ensure transparency and prevent users from outsourcing human connection to automated systems.
A developer shares a personal experience of their AI agent producing hallucinated data and the lessons learned about verification and prompt specificity.
A debugging story about a Windows program hanging when the user changes keyboard layouts due to a background thread that created a window but wasn't pumping messages. The fix is to either pump messages or destroy the window.
A company recounts a customer complaint about an AI agent's incorrect output, but they could not reconstruct the exact prompt and model version due to poor versioning practices, highlighting the need for better traceability in AI deployments.