Moon
Moon

Reputation: 22565

How to be notified a program execution and termination in c#?

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!

Upvotes: 2

Views: 999

Answers (2)

Paul Williams
Paul Williams

Reputation: 17020

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.

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564413

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).

Upvotes: 3

Related Questions