2007年1月4日 星期四

How to know your child appcication is running?

The subject what we want to discuss now is how to verify your child application is running or disappeared, for this case what we will do?

Follow below steps:
1. we can use the function called EnumProcesses() to get a list of all PIDs running status.
2. calling OpenProcess() to get a handle to the passed-in process ID
3. calling EnumProcessModules() and GetModuleBaseName() to get procedss name

after you finished above 3 steps, you can know your all child process running status.

below code is the sample function:

bool IsProcessRunning(LPCTSTR process_name)
{
DWORD aProcesses[1024]; // Array of process IDs
DWORD cbNeeded; // Byte count returned...
DWORD cProcesses; // The number of 'em obtained
DWORD dwExitCode = STILL_ACTIVE;

unsigned int i;

// Get a list of all current PIDs running
if(!EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded))
return false;

// Get the count of PIDs in the array
cProcesses = cbNeeded / sizeof(DWORD);

// Print the name and process identifier for each process
for(i = 0; i < cProcesses; i++)
{
char szProcessName[MAX_PATH] = "unknown";
// Get a handle to the passed-in process ID
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION
|PROCESS_ALL_ACCESS,
FALSE,
aProcesses[i] );

// Get the process name
if(hProcess)
{
HMODULE hMod;
DWORD cbNeeded;

if(::EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
{
::GetModuleBaseName(hProcess,
hMod,
szProcessName,
sizeof(szProcessName));
}

if( stricmp(szProcessName, process_name) == 0)
return true;
}
}
return false;
}

you can also copy this paragraph code into your project, to enrich the power of your application.

沒有留言: