views:

59

answers:

4

I have a Windows service that should run for all users that needs to know which user is currently logged in. I'd like to write an app that gets started when a user logs in that will alert the service that that user is the one that's currently logged in. It would also need to handle when the user is switched (meaning both are still running).

Is this possible and the best way of keeping my service aware of what user is currently logged in and using the computer, and if so, how might I go about creating/installing my app so that it starts when the user logs in (does it just go in the Startup folder for that user?) and determining if users were switched?

If not, what is the best way to do this?

+1  A: 

The problem is, that multiple people can easily log into the same machine so what you need to actually do is hook into the event of a user authenticating interactively. Use this API:

http://msdn.microsoft.com/en-us/library/aa374783(VS.85).aspx

Nissan Fan
That's a doc about Winlogon notification dlls. They were useful, but Microsoft dropped support for them, beginning with Vista. They discuss it here:http://technet.microsoft.com/en-us/library/cc721961%28WS.10%29.aspx
Ed
A: 

If you already have a Windows Service, I would query the local machine similarly to how PsLoggedOn does. Honestly, depending on what you're trying to do - you could just query AD and determine who's logged in where over the entire domain without relying on each machine to manage their own state locally.

Here's an example script (the first one in the list).

databyte
A: 

The Windows service APIs support this scenario directly. In your HandlerEx function, the SERVICE_CONTROL_SESSIONCHANGE notification is sent when a user logs on or off.

Larry Osterman
A: 

I ended up using SENS, which I found from the link provided by Ed. Ed, if you write your comment as an answer, I'll mark it as correct.

Mike Pateras