views:

343

answers:

1

I am writing a C# service which has to retrieve information from the currently logged on user like the active window or the last mouse movement. I already learned that I can retrieve these information by using the user32.dll but this does only work from within the user context which calls the methods.

This way my service could only retrieve information about itself but not about the "real world" user. I also heared that this should be possible by using WTSEnumerateSessions, OpenWindowStation, EnumDesktops, and so on but I haven't found an example showing me how to do this.

Does anybody have a clue (or maybe some code examples) how to achieve my goal?

+4  A: 

That sounds a lot like a job for an app running in the user's session. A well-behaved service shouldn't be snooping on the user like that. There may be ways to do it (I honestly don't know), but it doesn't sound very... proper.

Note that with recent versions of windows the console session (session 0) is limited to admin purposes, and the user won't (normally) be there. You may have zero, one, or many non-console sessions.

Marc Gravell
Indeed. I dont like this way very much either but if the app runs in the user's session, the user would be able to cancel it. This could be prevented in case it runs as a service.
Marcus
Just have the service restart the application.
Samuel
I thought about creating an user app and an additional service too but how could a service restart an application in the user's session without having the user's credentials?
Marcus
Take a look at http://stackoverflow.com/questions/418119/c-run-windows-form-application-from-service-and-in-vista and http://stackoverflow.com/questions/267838/how-can-a-windows-service-execute-a-gui-application
Samuel