tags:

views:

235

answers:

4

I want to make an application in c or c++ which have to monitor some specific processes. Kindly guide me how I can make it possible in c.

A: 

Before you write your own, have you looked at Process Monitor v2.8?

Process Monitor is an advanced monitoring tool for Windows that shows real-time file system, Registry and process/thread activity. It combines the features of two legacy Sysinternals utilities, Filemon and Regmon, and adds an extensive list of enhancements including rich and non-destructive filtering, comprehensive event properties such session IDs and user names, reliable process information, full thread stacks with integrated symbol support for each operation, simultaneous logging to a file, and much more.

Mitch Wheat
+1  A: 

You start a process in Windows with the CreateProcess() function. It returns a HANDLE to the process in PROCESS_INFORMATION.hProcess. That handle will be signaled when the process terminates, allowing you to keep track of its lifetime. Use WaitForSingleObject() or WaitForMultipleObjects() to do so. There's a code sample available here...

Hans Passant
or if someone else started the process, you can use OpenProcess to turn a PID into a HANDLE to the process. Make sure to request SYNCHRONIZE access if you want to wait for it.
Ben Voigt
A: 

Boost.Process

sample for Win32 Platform.

TheMachineCharmer
+2  A: 

You said that you have tomaonitor "some specific processes". If your application started the processes, you can extract the process handles from the PROCESS_INFORMATION structure (field hProcess) you passed to the CreateProcess function. If the process you want to track has been launched in some different way, you need the process' ID (PID), and use it as third argument of OpenProcess to obtain an handle. So you can use the WaitForSingleObject or WaitForMultipleObjects functions to wait for the process completion. Optionally you can obtain the process' exit code with the GetExitCodeProcess function. There are other ways by which an application can start a new process (e.g. by the _system() library function), but I strongly suggest to use CreateProcess directly in your code, since you can control the child process' behaviour completely (e.g. you can select the priority, pass stdin/stdout/stderr handles, decide the startup window's characteristics...).

Suggested example: http://msdn.microsoft.com/en-us/library/ms682512%28VS.85%29.aspx

Giuseppe Guerrini