views:

117

answers:

2

Hi,

is there a way to be notified when a program is executed or terminated by a user in c#? I am talking about all the programs that a user can execute, not just a certain program. I would like to be notified whenever a user execute .exe files.

I can't even think of a keyword to google.

any suggestions would be appreciated!

+3  A: 

The closest thing I know of would be to use Hooks.

You can use WH_SHELL Hooks to receive notification any time a new, non-owned, top level window is created or destroyed by the system. This isn't the same as a process, but it's pretty close in many cases, and potentially more useful in others (since it'd show a new word document window opening after one was already opened, even though they're using a shared process).

You might be able to combine that with EnumProcess to check to see if the process list has changed. This would work for tracking windows applications (but not services or console-based applications running in an existing console).

Reed Copsey
actually, this is exactly what I was looking for. Thank you!
Moon
+1  A: 

In Microsoft .NET Framework 3.5, you can get a list of Processes and register for the Process.Exited event. I suppose someone could implement a polling system in which they continually looked for new Processes, but that doesn't really notify you when something launches.

-- EDIT --

You might find this article useful. If you're willing to write a kernel mode driver, you can control every process start and finish.

Now, if you really want to get wild, you can hook into Microsoft Detours. Here is an interesting article about that.

Paul Williams
-1: That doesn't help much in being notified when a process executes.
John Saunders
Hope the edit doesn't offend you as much. :)
Paul Williams
@Paul Williams: Thank you for extra links and info!
Moon
@Paul Williams: Microsoft Detours looks very interesting
Moon
@Paul: read the FAQ or hover over the downarrow. It's got nothing to do with being offended, your answer was not helpful.
John Saunders