我搜索并找到了一种仅按名称终止进程的方法:
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 或类似名称的成员。我需要区分两个名称相同但位置不同的进程。