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
Printable View
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
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