tags:

views:

213

answers:

5

I want to create a keyboard and mouse hook which will be started as a windows service. I want to monitor the activity of the various users who use the system throughout the day. i.e. which users are active at what times.

Is is possible to determine which user will be receiving the events? (The service will be running as a separate user so getCurrentUser is not appropriate)

A: 

Environment.UserName would give you the name of the current user, who I suppose will be the recipient of your events as well.

Abbas
not sure. The service will be running as another user - probably admin
paul
+1  A: 

No, Environment.UserName does not work - the hook procedure is not called under the context of the input receiver.

Indeed, I think this is not possible - the _LL hooks, which you are no doubt using if using .NET, are low-level hooks. It seems to me that they are executed well before Windows even determines which desktop/application will receive the event. I may be wrong, though - I have never used the _LL hooks myself.

Sander
A: 

@TcKs - Um, what about Fast User Switching?

Damien_The_Unbeliever
Yes its true - I forgot.Maybe the ( http://msdn.microsoft.com/en-us/library/ms997634.aspx#winxpfus_switching ) can help.
TcKs
A: 

I don't know about these hooks - do they receive events from Remote Desktop keyboards? If they only get the local keyboard, then I think you need to find the owner of WinSta0.

Damien_The_Unbeliever
A: 

Another way:

The WTSGetActiveConsoleSessionId function allows you get a ID of active session. Concrete:

The WTSGetActiveConsoleSessionId function retrieves the Terminal Services session that is currently attached to the physical console. The physical console is the monitor, keyboard, and mouse. Note that it is not necessary that Terminal Services be running for this function to succeed.

Then you can use a WTSQueryUserToken to geting user's token, then you should be able to get a information about user with the token.

This functions are from Terminal Services, but documentation says:

Note that it is not necessary that Terminal Services be running for this function to succeed.

So I think, this can be way.

TcKs