MFC对进程的处理

//通过进程名获得进程句柄(HANDLE)

//通过进程名获得进程ID(DWORD)

//通过进程名判断进程是否在运行

//通过多个进程名判断进程是否在运行

//通过进程名杀死进程

//通过多个进程名杀死多个进程

 

//

//通过进程名获得进程句柄(HANDLE)

//

HANDLE CProcessPro::GetProcessHandle(CString strProcessName)

{

HANDLE handleResult = NULL;

HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

if (INVALID_HANDLE_VALUE == hSnapShot)

return handleResult;

 

PROCESSENTRY32 processInfo;

processInfo.dwSize = sizeof(PROCESSENTRY32);

 

//strProcessName.MakeLower();

 

while(Process32Next(hSnapShot, &processInfo)!=FALSE)

{

CString strExeFile(processInfo.szExeFile);

//strExeFile.MakeLower();

if(strExeFile.Compare(strProcessName) == 0)

{

handleResult = OpenProcess(PROCESS_ALL_ACCESS, TRUE, processInfo.th32ProcessID);

//TerminateProcess

//bResult = TRUE;

break;

}

}

CloseHandle(hSnapShot);

return handleResult;

}

 

//

//通过进程名获得进程ID(DWORD)

//

DWORD CProcessPro::GetProcessId(CString strProcessName)

{  

DWORD idProcess = 0;

HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); 

if (INVALID_HANDLE_VALUE == hSnapShot)

return idProcess;

 

PROCESSENTRY32 processInfo;

processInfo.dwSize = sizeof(PROCESSENTRY32);

 

//strProcessName.MakeLower();

 

while(Process32Next(hSnapShot, &processInfo)!=FALSE)

{

CString strExeFile(processInfo.szExeFile);

//strExeFile.MakeLower();

if(strExeFile.Compare(strProcessName) == 0)

{

idProcess = processInfo.th32ProcessID;

break;

}

}

CloseHandle(hSnapShot);

return idProcess; 

}

 

//

//通过进程名判断进程是否在运行

//

BOOL CProcessPro::IsProcessRun(CString strProcessName)

{

BOOL bResult = FALSE;

HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

if (INVALID_HANDLE_VALUE == hSnapShot)

return bResult;

 

PROCESSENTRY32 processInfo;

processInfo.dwSize = sizeof(PROCESSENTRY32);

 

//strProcessName.MakeLower();

 

while(Process32Next(hSnapShot, &processInfo)!=FALSE)

{

CString strExeFile(processInfo.szExeFile);

//strExeFile.MakeLower();

if(strExeFile.Compare(strProcessName) == 0)

{

bResult = TRUE;

break;

}

}

CloseHandle(hSnapShot);

return bResult;

}

 

//

//通过多个进程名判断进程是否在运行

//

BOOL CProcessPro::IsProcessRun(CStringArray& strArrayPro)

{

BOOL bResult = FALSE;

HANDLE hSnapShot = NULL;

int vCount = strArrayPro.GetCount();

 

if (vCount == 0)

return FALSE;

hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

if (INVALID_HANDLE_VALUE == hSnapShot)

return bResult;

 

PROCESSENTRY32 processInfo;

processInfo.dwSize = sizeof(PROCESSENTRY32);

 

 

while(Process32Next(hSnapShot, &processInfo)!=FALSE)

{

CString strExeFile(processInfo.szExeFile);

//strExeFile.MakeLower();

for (int i=0; i<vCount; i++)

{

CString findProName = strArrayPro.GetAt(i);

if(strExeFile.Compare(findProName) == 0)

{

bResult = TRUE;

break;

}

}

}

CloseHandle(hSnapShot);

return bResult;

}

 

//

//通过进程名杀死进程

//

BOOL CProcessPro::KillProcess(CString strProcessName)

{

BOOL bResult = FALSE;

HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

if (INVALID_HANDLE_VALUE == hSnapShot)

return bResult;

 

PROCESSENTRY32 processInfo;

processInfo.dwSize = sizeof(PROCESSENTRY32);

 

//strProcessName.MakeLower();

 

while(Process32Next(hSnapShot, &processInfo)!=FALSE)

{

CString strExeFile(processInfo.szExeFile);

//strExeFile.MakeLower();

if(strExeFile.Compare(strProcessName) == 0)

{

HANDLE handleResult = OpenProcess(PROCESS_ALL_ACCESS, TRUE, processInfo.th32ProcessID);

DWORD dExitCode = 0;

TerminateProcess(handleResult, dExitCode);

bResult = TRUE;

break;

}

}

CloseHandle(hSnapShot);

return bResult;

}

 

//

//通过多个进程名杀死多个进程

//

BOOL CProcessPro::KillProcess(CStringArray& strArrayPro)

{

 

BOOL bResult = FALSE;

HANDLE hSnapShot = NULL;

int vCount = strArrayPro.GetCount();

 

if (vCount == 0)

return TRUE;

hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

if (INVALID_HANDLE_VALUE == hSnapShot)

return bResult;

 

PROCESSENTRY32 processInfo;

processInfo.dwSize = sizeof(PROCESSENTRY32);

 

while(Process32Next(hSnapShot, &processInfo)!=FALSE)

{

CString strExeFile(processInfo.szExeFile);

//strExeFile.MakeLower();

for (int i=0; i<vCount; i++)

{

CString findProName = strArrayPro.GetAt(i);

if(strExeFile.Compare(findProName) == 0)

{

HANDLE handleResult = OpenProcess(PROCESS_ALL_ACCESS, TRUE, processInfo.th32ProcessID);

DWORD dExitCode = 0;

TerminateProcess(handleResult, dExitCode);

bResult = TRUE;

break;

}

}

}

CloseHandle(hSnapShot);

return bResult;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值