Windows下C++程序有多种方式可以关闭进程:
- 通过执行命令行程序,如taskkill等。
- 向进程发送关闭信号
- 通过Windows API来关闭进程,如TerminateProcess函数
以下函数通过调用TerminateProcess来关闭指定名称的进程。
bool kill_process(const char* lpszProcessName)
{
unsigned int pid = -1;
bool retval = true;
if (lpszProcessName == NULL)
return false;
DWORD dwRet = 0;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof(PROCESSENTRY32);
int flag = Process32First(hSnapshot, &processInfo);
while (flag != 0)
{
if (strcmp(processInfo.szExeFile, lpszProcessName) == 0)
{
pid = processInfo.th32ProcessID;
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
if (TerminateProcess(hProcess, 0) != TRUE)
{
retval = false;
break;
}
}
flag = Process32Next(hSnapshot, &processInfo);
}
CloseHandle(hSnapshot);
if (pid == -1)
return false;
return retval;
}