tags:

views:

469

answers:

3

I would like to get notification of when any/all programs are being actively used.

To do this via windows hooks I'd use an unmanaged C++ dll that talks to C#. There is an article that explains this process here.

I was wondering though if there is any alternative to using system hooks though.

A: 

List running processes:

http://codefreezer.com/codedetail.php?article=13

Determine which process is topmost:

http://www.pinvoke.net/default.aspx/user32/getwindowthreadprocessid.html

Nissan Fan
+1  A: 

C++ is not required to set a global windows hook, you just need to call the necessary winapis.

A global hook would be more efficient and more reliable than polling, assuming you are only interested in applications that have a window associated with the process. For example, a CMD prompt may not generate GWH events.

Ultimately the most reliable and efficient mechanism would be a system-wide injection, a good start would be the Detours library from Microsoft Research. Jefffrey Richter's classic "Advanced Windows Programming" book covered this in detail, offering 3 mechanisms for injection (including how to set up a system wide hook). Again, this doesn't require C++, and wouldn't necessarily tell you when an application was in use. Ultimately global window hooking is going to make the most sense.

Shaun Wilson
+2  A: 

I haven't looked at all of the details of this library, but it might help you out.

Managed Windows API

Jeremy Wiebe