自动登录"如果窗口被设为QUOT GetLastInputInfo API不起作用; [英] GetLastInputInfo API doesn't work if windows is set to "Automatic Login"
问题描述
我使用Windows API调用GetLastInputInfo以确定系统是否处于闲置状态。这在几乎任何情况下,当Windows设置为绕过用户名/密码和自动登录除外。
I use a Windows API call to GetLastInputInfo to determine if the system is idle. This works in pretty much any scenario except when windows is set to bypass the username/password and login automatically.
在这种情况下,查询GetLastInputInfo总是返回0。我在正常条件,它会返回系统时钟计数,最后输入时有发生。
In this case, querying GetLastInputInfo always returns 0. I normal conditions, it would return the system tick count when the last input occurred.
有谁知道为什么吗?还是没有人知道的另一种方法来确定一个Windows会话处于闲置状态?
Does anyone know why? Or does anyone know an alternate method to determine if a windows session is idle?
修改
这是代码是怎么写的来检测当前系统的空闲时间。发生在计时器的检测也不管有多长定时器运行,GetLastInputInfo永远当Windows设置为自动登录返回0。
This is how the code is written to detect the current system idle time. The detection occurs on a timer and regardless of how long the timer runs, GetLastInputInfo will always return 0 while windows is set to "auto login".
long lastInputTicks = 0;
long idleTicks = 0;
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
lastInputInfo.dwTime = 0;
if (GetLastInputInfo(ref lastInputInfo)) {
lastInputTicks = lastInputInfo.dwTime;
var systemUptime = GetTickCount();
idleTicks = systemUptime - lastInputTicks;
}
return new TimeSpan(0, 0, (int)(idleTicks / 1000));
感谢
Thanks
更新
我确认这仅是因为代码在服务运行。
I've confirmed that this only happens because the code runs in a service.
要澄清:在哪里自动登录不被使用时,用户登录在控制台和上面的代码正常情况下 - 即使作为服务运行 - 将返回正确的用户闲置时间
To clarify: Under a normal scenario where auto-login is not being used, the user logs in at the console and the above code -- even if running as a service -- will return the proper user inactivity time.
不过,如果自动登录被打开,那么窗口会出现无法运行作为一个典型的控制台会话,并因此,不会跟踪。这仅适用于当代码从一个服务,而不是从用户空间运行 - 如果从用户空间中运行它的伟大工程,在所有情况下
However, if auto-login is turned on, then the windows session appears to not run as a typical console session, and is therefore NOT tracked. This applies only when the code is ran from a service and not from the "user space" - it works great in all cases if running from a user space.
这说我真的很想找到在每个场景服务的工作的的解决方案。
That said, I'd really love to find a solution that works from a service in every scenario.
推荐答案
它返回0秒的时候GetLastInput()返回FALSE。这显然是不正确。不能在一个服务中使用这样的代码,它没有一个方法来检索用户输入。如果GetLastInput()曾经返回FALSE(有可能与自动登录),那么它将返回FALSE,直到永远。
It returns 0 seconds when GetLastInput() returns FALSE. Which is obviously incorrect. You cannot use code like this in a service, it doesn't have a way to retrieve user input. If GetLastInput() ever returns FALSE (likely with auto-login) then it will return FALSE forever.
有服务了解用户会话的是一个失败的事业在Windows中,一个服务在它自己的会话中运行,并且不需要用户为登录项。这是摆在首位使用服务的主要原因。而不是年代开始有计划的,也就是说,在启动文件夹的快捷方式。此代码可能会意外在XP中工作过,但它绝对不会工作在Vista和Windows 7,谷歌会话0隔离的更多细节。
Having services be aware of user sessions is a lost cause in Windows, a service runs in its own session and doesn't need a user to be logged-in. Which is the major reason to use a service in the first place. Instead of a program that's started with, say, a shortcut in the Startup folder. This code might have worked by accident in XP but it definitely won't work in Vista and Windows 7. Google "session 0 isolation" for more details.
这篇关于自动登录"如果窗口被设为QUOT GetLastInputInfo API不起作用;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!