I searched and found a way to terminate a process only by name:
void KillProcessByName(TCHAR *szPName)
{
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof (pEntry);
BOOL hRes = Process32First(hSnapShot, &pEntry);
while(hRes)
{
if(!_tcsicmp(pEntry.szExeFile, szPName))
{
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
(DWORD) pEntry.th32ProcessID);
if (hProcess != NULL)
{
TerminateProcess(hProcess, 9);
CloseHandle(hProcess);
}
}
hRes = Process32Next(hSnapShot, &pEntry);
}
CloseHandle(hSnapShot);
}
However, I can’t find a member called szLocation or sth alike in the PROCESSENTRY32 structure. I need to distinguish two processes with the same name, but different locations.
You can simply read
szExeFilefromPROCESSENTRY32.Or you can use
GetModuleFileNameExto return the full path to the executable file associated with a process handle.