您当前的位置:首页 > 计算机 > 编程开发 > VC/VC++

强制结束进程

时间:09-16来源:作者:点击数:

强制结束进程

void MainFrame::CloseCutterServer()
{
	TSTRING strName = _T("XXXXXXXX.exe");

	DWORD nProcessid = GetProcessidFromName(strName.c_str());
	if (nProcessid != 0)
	{
		LOG4CPLUS_INFO(m_Log, "XXXXXXXX正在运行,强制关闭XXXXXXXX进程");
		if (KillProcess(nProcessid))
		{
			Sleep(100);//等待进程彻底退出
		}
	}
}


//获取线程ID
DWORD GetProcessidFromName(LPCTSTR name)
{
	PROCESSENTRY32 pe;
	DWORD id = 0;
	HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	pe.dwSize = sizeof(PROCESSENTRY32);
	if (!Process32First(hSnapshot, &pe))
		return 0;
	while (1)
	{
		pe.dwSize = sizeof(PROCESSENTRY32);
		if (Process32Next(hSnapshot, &pe) == FALSE)
			break;
		if (StrCmpW(pe.szExeFile, name) == 0)
		{
			id = pe.th32ProcessID;

			break;
		}
	}
	CloseHandle(hSnapshot);
	return id;
}


//杀进程
bool KillProcess(DWORD dwPid)
{
	HANDLE killHandle = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION |   // Required by Alpha
		PROCESS_CREATE_THREAD |   // For CreateRemoteThread
		PROCESS_VM_OPERATION |   // For VirtualAllocEx/VirtualFreeEx
		PROCESS_VM_WRITE,             // For WriteProcessMemory);
		FALSE, dwPid);
	if (killHandle == NULL)
		return false;
	TerminateProcess(killHandle, 0);
	return true;
}
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐