taskkill /f /im *.exe 的作用

最近在看到一个同事使用Qt编写的进程监控程序中使用taskkill命令根据进程名称来杀死进程,
关于taskkill命令的具体用法可以参考MSDN文档:taskkill

taskkill [/s <computer> [/u [<domain>\]<username> [/p [<password>]]]] {[/fi <filter>] [...] [/pid <processID> | /im <imagename>]} [/f] [/t]
参数描述
/s Specifies the name or IP address of a remote computer (do not use backslashes). The default is the local computer.
/u <username>Runs the command with the account permissions of the user who is specified by or by <username>. The /u parameter can be specified only if /s is also specified. The default is the permissions of the user who is currently logged on to the computer that is issuing the command.
/p Specifies the password of the user account that is specified in the /u parameter.
/fi Applies a filter to select a set of tasks. You can use more than one filter or use the wildcard character (*) to specify all tasks or image names. The valid filters are listed in the Filter names, operators, and values section of this article.
/pid Specifies the process ID of the process to be terminated.
/im Specifies the image name of the process to be terminated. Use the wildcard character (*) to specify all image names.
/fSpecifies that processes be forcefully ended. This parameter is ignored for remote processes; all remote processes are forcefully ended.
/tEnds the specified process and any child processes started by it.

taskkill是Windows命令行里终止指定程序“进程”的命令。

/f 表示强制终止
/im 表示指定的进程名称,例如“explor.exe"

如果不使用名称,使用进程号,则用/PID,例如(假设已知道某进程的PID号是3352,PID号可以在windows任务管理器中查看):
taskkill /f /pid 3352

使用场景:

会在开发定时脚本中用到,用于关闭进程;当然也可以用于进程监控程序的编写,例如如下的Qt代码片段:

void ProViewWidget::onBtnClicked()
{
	// 处理启动和停止按钮事件
	if (m_bRunning)
	{
		QFileInfo finfo(str2qstr(m_pcfg->exePath));

		std::string temp = std::string("TASKKILL /F /IM ") + qstr2str(finfo.fileName());
		//system(temp.c_str());
		WinExec(temp.c_str(), 0);
	}
	else
	{
		STARTUPINFO si;
		PROCESS_INFORMATION pi;

		char path[1000] = {NULL};

		std::string strpath = m_pcfg->exePath;

		strcpy(path, strpath.c_str());

		ZeroMemory(&si, sizeof(si));
		si.cb = sizeof(si);
		ZeroMemory(&pi, sizeof(pi));
		if (!CreateProcessA(NULL,   // No module name (use command line)
			path,        // Command line
			NULL,           // Process handle not inheritable
			NULL,           // Thread handle not inheritable
			FALSE,          // Set handle inheritance to FALSE
			0,              // No creation flags
			NULL,           // Use parent's environment block
			NULL,           // Use parent's starting directory 
			&si,            // Pointer to STARTUPINFO structure
			&pi)           // Pointer to PROCESS_INFORMATION structure
			)
		{
			inputLineToEdit("进程启动失败!");
		}
	}
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值