先枚举出所有进程,然后根据匹配的进程名进行结束操作
BOOL EndProcess(TCHAR *szEndName)
{
// Get the list of process identifiers.
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
return FALSE;
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the name and process identifier for each process.
for (i=0; i<cProcesses; i++)
{
TCHAR szProcessName[MAX_PATH] = {0};
// Get a handle to the process.
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 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 (_tcsicmp(szEndName, szProcessName) == 0)
{
::TerminateProcess( ::OpenProcess( PROCESS_TERMINATE, FALSE,aProcesses[i]), 0 );
}
}
}
CloseHandle( hProcess );
}
return TRUE;
}