强制结束进程
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;
}