开发者

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开发者_Go百科 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?


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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜