Python
import os
import signal
import psutil#需pip安装
processes = psutil.process_iter()
target_process_name = "StudentMain.exe" # 指定要查找的进程名称
for process in processes:
if process.name() == target_process_name:
target_process = process
break
pid = target_process.pid
print(pid)
os.kill(pid, signal.SIGTERM) # 主动结束指定ID的程序运行
如果把
StudentMain.exe
改写成其他应用程序文件名,也可以终止其他应用程序。
其中,psutil需要使用pip安装。方法:打开CMD管理员,输入
pip install psutil
若安装失败,可能可以通过升级pip来解决。具体应看CMD报错。
本程序在没有找到指定程序时可能会报错。
C++
#include <iostream>
#include <windowsx.h>
using namespace std;
int main()
{
system("taskkill /f /im StudentMain.exe /t");
return 0;
}
直接用就行了。
如果把
StudentMain.exe
改写成其他应用程序文件名,也可以终止其他应用程序。
本程序在没有找到指定程序时可能会报错。但你将无法看到报错,因为程序在执行完之后会直接自动退出。