Mike Pateras
Mike Pateras

Reputation: 15015

How do I write an app that alerts a Windows service which user is currently logged in?

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?

Upvotes: 0

Views: 191

Answers (4)

Mike Pateras
Mike Pateras

Reputation: 15015

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.

Upvotes: 0

Larry Osterman
Larry Osterman

Reputation: 16142

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.

Upvotes: 0

databyte
databyte

Reputation: 1268

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).

Upvotes: 0

Keith Adler
Keith Adler

Reputation: 21178

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

Upvotes: 1

Related Questions