我搜索并找到了一种仅通过名称终止进程的方法:
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);
}
但是,我在PROCESSENTRY32结构中找不到名为szLocation或类似的成员。我需要区分具有相同名称但位置不同的两个进程。