tags:

views:

23

answers:

1

Hello everyone, I need to obtain the Handle to a process using its Image Name so that I can use it with TerminateProcess function. Any help will be appreciated

Farid

A: 

Use CreateToolhelp32Snapshot and Process32First/Next to iterate the running processes. Figuring out which one you'd want to abort if there's more than one instance of the process is of course not really possible.

Hans Passant
Thank you so much for your help Mr PassantFor those who like examples I one writtenusing the above functions:#include <windows.h>#include <Tlhelp32.h>int main(){ HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);PROCESSENTRY32 pe32;pe32.dwSize = sizeof(PROCESSENTRY32);Process32First(hSnapshot, do {string ProcExe = pe32.szExeFile;//Now, say you want to terminate Notepad:if (ProcExe == "notepad.exe"){HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pe32.th32ProcessID);TerminateProcess(hProcess, NULL);}} while(Process32Next(hSnapshot, }
Farid