怎么杀掉有自己输入的进程名
#include<stdlib.h>int main(int argc, char *argv[])
{
system("taskkill /IM abc.exe /f");
return 0;
}
上面的程序不灵活,编译好了只能杀掉abc.exe,该怎么写,让自己输入进程名啊,杀掉自己想杀掉的进程
#include <Windows.h> #include <TlHelp32.h> #include <stdio.h> int main(int argc, char* argv[]) { if (2 != argc) { printf("参数错误.\n"); return -1; } PROCESSENTRY32 pe32; pe32.dwSize = sizeof(pe32); HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (INVALID_HANDLE_VALUE == hProcessSnap) { printf("CreateToolhelp32Snapshot调用失败.\n"); return -1; } bool IsExist = Process32First(hProcessSnap, &pe32); while (IsExist) { if (0 == strcmp(argv[1], pe32.szExeFile)) { TerminateProcess(OpenProcess(PROCESS_ALL_ACCESS, false, pe32.th32ProcessID),0); printf("进程 %s 已被关闭.\n", argv[1]); CloseHandle(hProcessSnap); return 0; } IsExist = Process32Next(hProcessSnap,&pe32); } printf("没有找到进程 %s.\n", argv[1]); CloseHandle(hProcessSnap); return 0; }