根据进程名称,关掉进程

最近研究了一下怎样自动打开浏览器,自动关闭浏览器,写在这里

以便忘记的时候能够及时查找,相关源文件在这里:点击打开链接

C++

BOOL FindAndKillProcessByName(LPCTSTR strProcessName)
{
if(NULL == strProcessName)
{
return FALSE;
}
HANDLE handle32Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == handle32Snapshot)
{
return FALSE;
}
PROCESSENTRY32 pEntry;       
pEntry.dwSize = sizeof( PROCESSENTRY32 );
//Search for all the process and terminate it
if(Process32First(handle32Snapshot, &pEntry))
{
BOOL bFound = FALSE;
if (!_tcsicmp(pEntry.szExeFile, strProcessName))
{
bFound = TRUE;
}
while((!bFound)&&Process32Next(handle32Snapshot, &pEntry))
{
if (!_tcsicmp(pEntry.szExeFile, strProcessName))
{
bFound = TRUE;
}
}
if(bFound)
{
CloseHandle(handle32Snapshot);
HANDLE handLe =  OpenProcess(PROCESS_TERMINATE , FALSE, pEntry.th32ProcessID);
BOOL bResult = TerminateProcess(handLe,0);
return bResult;
}
}
CloseHandle(handle32Snapshot);
return FALSE;
}

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页