16,471
社区成员
发帖
与我相关
我的任务
分享
BOOL CProcessManager::FindProcess2Kill(LPCTSTR lpcExeFile, KillInfoCallback callback)
{
HANDLE hProcessSnap = NULL;
BOOL bRet = TRUE;
BOOL bFind = FALSE;
PROCESSENTRY32 pe32 = {0};
CString csInfo = _T("");
CString csTagName = lpcExeFile;
csTagName.MakeUpper();
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
return FALSE;
// Fill in the size of the structure before using it.
pe32.dwSize = sizeof(PROCESSENTRY32);
// Walk the snapshot of the processes, and for each process,
// display information.
if (Process32First(hProcessSnap, &pe32))
{
DWORD dwPriorityClass;
BOOL bGotModule = FALSE;
MODULEENTRY32 me32 = {0};
do
{
CString csOrgName = pe32.szExeFile;
csOrgName.MakeUpper();
if (csTagName == csOrgName)
{
bFind = TRUE;
HANDLE hProcess;
// Get the actual priority class.
hProcess = OpenProcess (PROCESS_ALL_ACCESS,
FALSE, pe32.th32ProcessID);
dwPriorityClass = GetPriorityClass (hProcess);
DWORD dwExitCode = 0;
try
{
GetExitCodeProcess(hProcess, &dwExitCode);
BOOL bKill = TerminateProcess(hProcess, dwExitCode);
if (NULL != callback)
{
csInfo.Format(_T("%d - %s "), pe32.th32ProcessID, csOrgName);
if (bKill)
csInfo += "干掉了!";
else
csInfo += "杀不掉啊!";
callback(csInfo);
}
}
catch (CException* e)
{
char szError[MAX_PATH];
e->GetErrorMessage((LPTSTR)szError, MAX_PATH);
}
CloseHandle (hProcess);
}
}
while (Process32Next(hProcessSnap, &pe32));
}
if (!bFind)
{
csInfo = "没有找到进程";
csInfo += csTagName;
callback(csInfo);
}
// Do not forget to clean up the snapshot object.
CloseHandle (hProcessSnap);
return bRet;
}
BOOL FindProcessModule (DWORD dwPID, LPCTSTR lpcModuleName)
{
BOOL bRet = FALSE;
BOOL bFound = FALSE;
HANDLE hModuleSnap = NULL;
MODULEENTRY32 me32 = {0};
// Take a snapshot of all modules in the specified process.
hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
if (hModuleSnap == INVALID_HANDLE_VALUE)
return (FALSE);
// Fill the size of the structure before using it.
me32.dwSize = sizeof(MODULEENTRY32);
// Walk the module list of the process, and find the module of
// interest. Then copy the information to the buffer pointed
// to by lpMe32 so that it can be returned to the caller.
if (Module32First(hModuleSnap, &me32))
{
do
{
CString csOrgName = me32.szModule;
csOrgName.MakeUpper();
if (csOrgName == lpcModuleName)
{
bFound = TRUE;
}
}
while (!bFound && Module32Next(hModuleSnap, &me32));
bRet = bFound; // if this sets bRet to FALSE, dwModuleID
// no longer exists in specified process
}
else
bRet = FALSE; // could not walk module list
// Do not forget to clean up the snapshot object.
CloseHandle (hModuleSnap);
return (bRet);
}
hWnd=::FindWindow(NULL,"*******");
if(hWnd)
{
SendMessage(hWnd, WM_QUIT, ...);
}