Click to See Complete Forum and Search --> : How do I kill a proccess just knowing its name?


GeorgeM
June 2nd, 1999, 12:01 PM
How can I kill all instances of say test.exe programatically? Anyone know of how to do this or have a sample of how it is done?

Thanks

George

ALM
June 2nd, 1999, 02:29 PM
You basically need to get the process ID, pass it to OpenProcess, and then call TerminateProcess to kill it.

The tricky part is getting the process ID and for that you can use the classes found here in CodeGuru. I found a nice set at http://www.codeguru.com/misc/process.shtml and here's how you can use them:


CProcessList* pProcessList = CProcessList::Create();
pProcessList->SnapShot();

CString strFile = "c:\\the file you're interested in";

for (CProcess* pProcess; (pProcess = pProcessList->GetNextProcess()); )
{
if (pProcess->GetFilename() == strFile)
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pProcess->GetPID());
TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
}
}




Hope this helps!
Alvaro