I am writing a Windows service that needs to know if there are users on the computer who are currently logged in.
So far I have tried Win32_LogonSession(WMI) and LsaEnumerateLogonSessions/ LsaGetLogonSessionData(secur32.dll).
Both work and seem to return the same data, but they are too slow to update when the user logs out:
- When the system starts up, they return "0 interactive users". (OK)
- When I log in, they return "1 interactive user". (OK)
- But then, when I log out, the number of users is saved to 1. After a new login, the number is 2, etc.
So Win32_LogonSession or LsaEnumerateLogonSessions are good enough. The service should know within 5 minutes after the last interactive user left.
Even SysInternals LogonSessions.exe does not respond to the most recent answers. In addition, the answer cannot be βtrack login and output events and have a counter variableβ, because the service can be started at any time.
source
share