怎么监听程序,当发现到A程序运行时,就关闭A程序

如何监听程序,当发现到A程序运行时,就关闭A程序

#include<windows.h>

int WINAPI WinMain(
  HINSTANCE hInstance, 
  HINSTANCE hPrevInstance, 
  LPSTR lpCmdLine, 
  int nShowCmd 
)
{
while(1)
{
LONG64 i=1;
if(FindWindow("Notepad",NULL))
while(i)
{
//关闭程序?
if(!FindWindow("Notepad",NULL)) i=0;
}
}
}


这里有3个问题请教大家:
1、如何监听程序A的运行?这里我假设A为记事本程序,我使用的是FindWindow,这里的类名好填,是Notepad,如果是其它不知类名的程序怎么办?是否可以通过检测进程名或进程PID来达到目的?
2、如代码示,使用while(1)检测会耗费大量的CPU资源,除了SLEEP外,有没有其它办法能够减少CPU资源的损耗?
3、找到A程序后,如何关闭A程序?

------解决方案--------------------
我的blog里面有一篇这样的文章
look
------解决方案--------------------
1、如何监听程序A的运行?这里我假设A为记事本程序,我使用的是FindWindow,这里的类名好填,是Notepad,如果是其它不知类名的程序怎么办?是否可以通过检测进程名或进程PID来达到目的?
>>> FindWindow 是最简单可靠的方法,如果不知道类名,也可以使用窗口名。进程名和进程PID,好像每次都不固定吧。
2、如代码示,使用while(1)检测会耗费大量的CPU资源,除了SLEEP外,有没有其它办法能够减少CPU资源的损耗?
>>> 最好是Sleep()一会,时间可以短一些。
3、找到A程序后,如何关闭A程序?
>>> TerminateProcess
------解决方案--------------------


void OnHook(WPARAM wParam, LPARAM lParam)
{
HWND hWnd = (HWND)wParam;
int nType = lParam;

CString strEventType;
CString strEventDesc;

CString strTime;
CTime tm0=CTime::GetCurrentTime();
strTime.Format("%04d-%02d-%02d %02d:%02d:%02d",tm0.GetYear(),tm0.GetMonth(),tm0.GetDay(),
tm0.GetHour(),tm0.GetMinute(),tm0.GetSecond());

CTxtLog txtlog;
CString strFileHead;

switch(nType)
{
//case HSHELL_WINDOWCREATED:
case HCBT_CREATEWND:
{
if( !CWndMessageLog::GetInstance().m_pApplicationFilter->bWindowCreate )
{
return ;
}

CString strProcess;
DWORD dwProcessId;

GetWindowThreadProcessId(hWnd, &dwProcessId);

CUtilityEx::GetProcessFilePathByPId(dwProcessId, strProcess);

strEventType = "程序启动";

strEventDesc.Format("%s", strProcess);

strFileHead = "WindowCreate";

CWndMessageLog::GetInstance().m_mapApplicationStatistic[dwProcessId] = time(NULL);
}
break;
//case HSHELL_WINDOWACTIVATED:
case HCBT_ACTIVATE:
{
if( !CWndMessageLog::GetInstance().m_pApplicationFilter->bWindowActive )
{
return ;
}

//稍微延时一下,让窗口设定好标题
Sleep(10);
char szTitle[MAX_PATH] = {0};
::GetWindowText(hWnd, szTitle, sizeof(szTitle));

strEventType = "窗口事件";
strEventDesc.Format("%s", szTitle);

strFileHead = "WindowActive";
}
break;
//case HSHELL_WINDOWDESTROYED:
case HCBT_DESTROYWND:
{
if( !CWndMessageLog::GetInstance().m_pApplicationFilter->bWindowCreate )
{
return ;
}

CString strProcess;

DWORD dwProcessId;

GetWindowThreadProcessId(hWnd, &dwProcessId);

CUtilityEx::GetProcessFilePathByPId(dwProcessId, strProcess);

strEventType = "程序关闭";

strEventDesc.Format("%s", strProcess);

strFileHead = "WindowCreate";

if( !CWndMessageLog::GetInstance().m_pApplicationFilter->bApplicationStatistic)
{
break;
}

map<DWORD, DWORD>& mapProcess = CWndMessageLog::GetInstance().m_mapApplicationStatistic;

if( mapProcess.end() != mapProcess.find(dwProcessId))
{
time_t tNow = time(NULL);
int nSecond = (int)(tNow - mapProcess[dwProcessId]);

string strRunTime;
CUtility::GetTimeStr(nSecond,strRunTime);

CString strLog;
strLog.Format("%s<-_->%s<-_->%s\r\n", strTime, strRunTime.c_str(), strProcess);
txtlog.Log("AppStatistic", strLog);

mapProcess.erase(dwProcessId);
}
}
break;
}

if( strEventDesc.GetLength() == 0 )
{
return ;
}

CString strLog;
strLog.Format("%s<-_->%s<-_->%s\r\n", strTime, strEventType, strEventDesc);
txtlog.Log(string((LPCTSTR)strFileHead), strLog);

}