Reputation: 41
GetLastInputInfo can work well on application but not a service.
GetLastInputInfo will always return LASTINPUTINFO.dwTime=0 since it is running on Session 0.
How can I achieve detecting the system is idle or not in Windows Service?
Upvotes: 2
Views: 1206
Reputation: 595402
Your service can enumerate the active sessions via WTSEnumerateSessions()
, querying each session for its WTSSessionInfo/Ex
via WTSQuerySessionInformation()
. That will give you each session's LastInputTime
(amongst other things).
PWTS_SESSION_INFO sessions = NULL;
DWORD count = 0;
if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &sessions, &count))
{
DWORD errCode = GetLastError();
...
}
else
{
for (DWORD i = 0; i < count; ++i)
{
PWTSINFO info = NULL;
DWORD size = 0;
if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, sessions[i].SessionId, WTSSessionInfo, (LPWSTR*)&info, &size))
{
DWORD errCode = GetLastError();
...
}
else
{
// use info->LastInputTime as needed...
WTSFreeMemory(info);
}
}
WTSFreeMemory(sessions);
}
If you are interested only in the session that is connected to the local machine's physical screen/keyboard/mouse, you can use WTSGetActiveConsoleSessionId()
instead of WTSEnumerateSessions()
.
Upvotes: 1