说不完的故事

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
HWND CProcessManage::GetHwnd(CString strProcessName,bool &bProcessExist)
{
bProcessExist
= false;
HWND hwnd
= NULL;
DWORD dwPID
= GetPID(strProcessName);
if (dwPID > 0)
{
bProcessExist
= true;
CMainWindowIterator mainWindwoIter(dwPID);
hwnd
= mainWindwoIter.First();
}
return hwnd;
}
//通过PID Kill进程
bool CProcessManage::KillProcess(DWORD dwPID,bool bZap /* = true */)
{
//关闭进程
bool bKill = false;
HWND hwnd
= NULL;
CMainWindowIterator mainWindwoIter(dwPID);
hwnd
= mainWindwoIter.First();//获取当前的句柄
if (NULL != hwnd)
{
DWORD dwOKTOKill
= 0;
SendMessageTimeout(hwnd,WM_QUERYENDSESSION,
0,0,SMTO_ABORTIFHUNG|SMTO_NOTIMEOUTIFNOTHUNG,100,&dwOKTOKill);
if (dwOKTOKill == 0)
{
return bKill; //Window不想关闭该线程
}
PostMessage(hwnd,WM_CLOSE,
0,0);
bKill
= true;
}
HANDLE hp
=OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE,FALSE,dwPID);
if (hp != NULL)
{
if (WaitForSingleObject(hp,5000) != WAIT_OBJECT_0)
{
if (bZap)
{
TerminateProcess(hp,
0);
bKill
= true;
}
else
{
bKill
= false;
}
}
CloseHandle(hp);
}
return bKill;
}
//获取进程的PID
DWORD CProcessManage::GetPID(CString strProcessName)
{
TCHAR szName[_MAX_PATH];
//用来存放进程名
memset(szName,0,_MAX_PATH);
DWORD dwPID
= 0;
CProcessIterator ProcessIter;
//PID队列
for (dwPID = ProcessIter.First();dwPID;)
{
CProcessModuleIterator ProcessModuleIter(dwPID);
HMODULE hModule
= ProcessModuleIter.First();
if (hModule)
{
GetModuleBaseName(ProcessModuleIter.GetProcessHandle(),hModule,szName,_MAX_PATH);
if (szName == strProcessName)
{
break;
}
}

dwPID
= ProcessIter.Next();
}
return dwPID;
}

posted on 2011-04-15 15:27  说不完的故事  阅读(4091)  评论(0编辑  收藏  举报