Why doesn’t Get­Last­Input­Info() return info for the user I’m impersonating?

The Old New Thing (Raymond Chen) Tools

Summary

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>WTS­Query­User­Token()</code> to get the token for each user, use that token to impersonate the user, and then call <code>Get­Last­Input­Info()</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>Get­Last­Input­Info()</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 &#8220;Does this work when impersonating?&#8221; is &#8220;No&#8221;</a>. And in fact, the documentation for <code>Get­Last­Input­Info()</code> explicitly says that it doesn&#8217;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&#8217;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 &#8220;Does <code>Get­Async­Key­State</code> from a service?&#8221; The answer is technically yes, it works. However it probably doesn&#8217;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&#8217;t &lt;CODE&gt;Get&shy;Last&shy;Input&shy;Info()&lt;/CODE&gt; return info for the user I&#8217;m impersonating?</a> appeared first on <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a>.</p>
Original Article
View Cached Full Text

Cached at: 06/20/26, 02:22 PM

# Why doesn't Get­Last­Input­Info() 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`WTS­Query­User­Token\(\)`to get the token for each user, use that token to impersonate the user, and then call`Get­Last­Input­Info\(\)`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`Get­Last­Input­Info\(\)`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`Get­Last­Input­Info\(\)`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`Get­Async­Key­State`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 Chen](https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2019/02/RaymondChen_5in-150x150.jpg) 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\.

Similar Articles

We Are the Last People Who Know How It Works

Hacker News Top

A reflective essay on how the rise of frictionless AI assistants is causing a loss of deep, personal acquaintance with computers, contrasting the hands-on computing era of the 1990s.

The Illusion of Listening

Reddit r/ArtificialInteligence

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.

When Impressive Performance Gains Do Not Matter

Lobsters Hottest

A blog post discussing how performance improvements that fail to cross the 10-second attention threshold may not yield user experience benefits, using examples from database query optimization and process automation.