VC++信息安全编程(5)实现进程监视清除多余进程
创建多进程处理程序的时候,需要对多进程进行监视,例如QQ启动多了,内存很卡,就得清除一些多余进程。
详细请见代码分析,实现进程监视与清除多余进程
#include "stdafx.h"
#include "GetAllInfo.h"
#include "GetAllInfoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
UINT Thread(LPVOID param){
CGetAllInfoDlg *mys=(CGetAllInfoDlg*)param;
mys->OnGetProcess();
do{
mys->ScanProcess();
}while(mys->status);
return 1;
}
/////////////////////////////////////////////////////////////////////////////
// CGetAllInfoDlg dialog
CGetAllInfoDlg::CGetAllInfoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGetAllInfoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGetAllInfoDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
status=0;
}
void CGetAllInfoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGetAllInfoDlg)
DDX_Control(pDX, IDC_BgetAll, m_BgetAll);
DDX_Control(pDX, IDC_LIST1, m_list);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGetAllInfoDlg, CDialog)
//{{AFX_MSG_MAP(CGetAllInfoDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BgetAll, OnBgetAll)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGetAllInfoDlg message handlers
BOOL CGetAllInfoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
TotalFileNum=0;
fp.Open("info.txt",CFile::modeCreate|CFile::modeWrite);
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
aProcesses= new DWORD [1024];
pagain= new DWORD[1024];
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CGetAllInfoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CGetAllInfoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CGetAllInfoDlg::OnBgetAll()
{
if(!status){
status=1;
AfxBeginThread(&Thread,this,THREAD_PRIORITY_BELOW_NORMAL,0,0);
m_BgetAll.SetWindowText("暂停搜索");
}
else{
m_BgetAll.SetWindowText("查所有信息");
status=0;
}
}
void CGetAllInfoDlg::OnGetProcess()
{
DWORD cbNeeded;
//unsigned int i;
//枚举系统进程ID列表
if(!EnumProcesses( aProcesses, 1024*sizeof(DWORD), &cbNeeded ) )return;
// Calculate how many process identifiers were returned.
//计算进程数量
cProcesses1 = cbNeeded / sizeof(DWORD);
// 输出每个进程的名称和ID
//for ( i = 0; i < cProcesses1; i++ )PrintProcessNameAndID( aProcesses[i]);
}
void CGetAllInfoDlg::PrintProcessNameAndID(DWORD processID)
{
char szProcessName[MAX_PATH] = "unknown";
//取得进程的句柄
HANDLE hProcess=OpenProcess( PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,processID);
//取得进程名称
<
补充:综合编程 , 安全编程 ,