2

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?

Marcus
  • 1,105
  • 2
  • 21
  • 35

1 Answers1

4

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 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 Mar 11 '09 at 15:11
  • Just have the service restart the application. – Samuel Mar 11 '09 at 15:16
  • 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 Mar 11 '09 at 15:19
  • 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 Mar 11 '09 at 15:43