///////////////////////////////////////////////////////////////
/*
Process v1.0
作者:黄利龙(Email:landyu@163.com)
时间:2003-7-21
*/
///////////////////////////////////////////////////////////////
#include<afxwin.h>
#include<afxcmn.h>
#include<afxdisp.h>
#include<afxext.h>
#include<Tlhelp32.h>
#include"HyperLink.h"
#include"resource.h"
#define ID_PRO 188
#define ID_CHK 189
#define WM_MYMESSAGE WM_USER+6
class dialog2:public CDialog //对话框2(信息框)
{
public:
DWORD info1,info2,info3,info4,info5,info6,
info7,info8,info9;
CString info10;
dialog2():CDialog(IDD_DIALOG2)
{
}
void DoDataExchange(CDataExchange *pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX,IDC_dwSize,info1);
DDX_Text(pDX,IDC_cntUsage,info2);
DDX_Text(pDX,IDC_th32ProcessID,info3);
DDX_Text(pDX,IDC_th32DefaultHeapID,info4);
DDX_Text(pDX,IDC_th32ModuleID,info5);
DDX_Text(pDX,IDC_cntThreads,info6);
DDX_Text(pDX,IDC_th32ParentProcessID,info7);
DDX_Text(pDX,IDC_pcPriClassBase,info8);
DDX_Text(pDX,IDC_dwFlags,info9);
DDX_Text(pDX,IDC_szExeFile,info10);
}
BOOL OnInitDialog()
{
CDialog::OnInitDialog();
GetDlgItem(IDC_STATIC1)->SetWindowText("Size");
GetDlgItem(IDC_STATIC2)->SetWindowText("Usage");
GetDlgItem(IDC_STATIC3)->SetWindowText("ProcessID");
GetDlgItem(IDC_STATIC4)->SetWindowText("DefaultHeapID");
GetDlgItem(IDC_STATIC5)->SetWindowText("ModuleID");
GetDlgItem(IDC_STATIC6)->SetWindowText("Threads");
GetDlgItem(IDC_STATIC7)->SetWindowText("ParentProcessID");
GetDlgItem(IDC_STATIC8)->SetWindowText("ClassBase");
GetDlgItem(IDC_STATIC9)->SetWindowText("Flags");
GetDlgItem(IDC_STATIC10)->SetWindowText("File");
return TRUE;
}
HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) ////设置控件颜色
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor==CTLCOLOR_STATIC)
{
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(160,180,220));
HBRUSH b=CreateSolidBrush(RGB(160,180,220));
return b;
}
else if(nCtlColor==CTLCOLOR_DLG)
{
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(160,180,220));
HBRUSH b=CreateSolidBrush(RGB(160,180,220));
return b;
}
return hbr;
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(dialog2,CDialog)
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
class dialog1:public CDialog//对话框1
{
public:
CListCtrl m_list;
CObArray m_lists;
COleDateTime m_time;
CImageList m_im;
CString name;
CString time;
CHyperLink m_url;
HICON m_hIcon;
dialog1():CDialog(IDD_DIALOG1)
{
m_hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);//添加图标
}
void DoDataExchange(CDataExchange *pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX,IDC_LIST1,m_list);
DDX_Control(pDX,IDC_URL,m_url);
DDX_DateTimeCtrl(pDX,IDC_DATETIMEPICKER1,m_time);
}
BOOL OnInitDialog()
{
CDialog::OnInitDialog();
//在列表控件中添加图标
m_im.Create(IDB_BITMAP1,16,1,RGB(255,0,255));
m_list.SetImageList(&m_im,LVSIL_SMALL);
//初始化列表控件
m_list.InsertColumn(0,"Image Name",LVCFMT_LEFT,120,0);
m_list.InsertColumn(1,"ProID",LVCFMT_CENTER,88,1);
m_list.InsertColumn(2,"Parent ProID",LVCFMT_CENTER,88,2);
m_list.InsertColumn(3,"Kill Time",LVCFMT_CENTER,98,3);
GetDlgItem(IDC_ADD)->SetWindowText("ADD");
GetDlgItem(IDC_DEL)->SetWindowText("DEL");
GetDlgItem(IDC_QUIT)->SetWindowText("EXIT");
GetDlgItem(IDC_RF)->SetWindowText("Refresh");
GetDlgItem(IDC_KP)->SetWindowText("Kill");
refresh();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
SetTimer(ID_PRO,20000,NULL);
SetTimer(ID_CHK,500,NULL);
m_url.SetURL(_T("mailto:landyu@163.com"));
m_url.SetUnderline(FALSE);
return TRUE;
}
void AddTime()//添加时间
{
UpdateData();
CString time;
int CurItem=m_list.GetNextItem(-1,LVIS_SELECTED);
if(CurItem>1)
{
time.Format("%0.2d:%0.2d:%0.2d",m_time.GetHour(),m_time.GetMinute(),m_time.GetSecond());
m_list.SetItemText(CurItem,3,time);
dialog1* pNewItem = new dialog1();
pNewItem->name=m_list.GetItemText(CurItem,0);
pNewItem->time=time;
m_lists.Add(pNewItem);
}
else
{
MessageBox("Please select the list item");
}
}
BOOL refresh()//刷新
{
//获取系统中的所有进程信息
m_list.DeleteAllItems();
HANDLE SnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(SnapShot==NULL)
{
MessageBox("检测当前进程失败!");
return FALSE;
}
SHFILEINFO shSmall;
int nIndex;
CString str;
PROCESSENTRY32 ProcessInfo;//声明进程信息变量
ProcessInfo.dwSize=sizeof(ProcessInfo);//设置ProcessInfo的大小
//返回系统中第一个进程的信息
BOOL Status=Process32First(SnapShot,&ProcessInfo);
int m_nProcess=0;
while(Status)
{
m_nProcess++;
ZeroMemory(&shSmall,sizeof(shSmall));
//获取进程文件信息
SHGetFileInfo(ProcessInfo.szExeFile,0,&shSmall,
sizeof(shSmall),SHGFI_ICON|SHGFI_SMALLICON);
//在列表控件中添加映像名称
nIndex=m_list.InsertItem(m_nProcess,ProcessInfo.szExeFile);
//在列表控件中添加进程
str.Format("%08x",ProcessInfo.th32ProcessID);
m_list.SetItemText(nIndex,1,str);
//在列表控制件中添加进程的父进程
str.Format("%08x",ProcessInfo.th32ParentProcessID);
m_list.SetItemText(nIndex,2,str);
//在列表控件中添加KILLTIME
for(int i=0;i<m_lists.GetSize();i++)
{
dialog1* pItem = (dialog1*)(m_lists.GetAt(i));
if(ProcessInfo.szExeFile==pItem->name)
{
m_list.SetItemText(nIndex,3,pItem->time);
}
}
//获取下一个进程的信息
Status=Process32Next(SnapShot,&ProcessInfo);
}
return TRUE;
}
void KillProcess()//关闭进程
{
UpdateData();
CString str,a;
int CurItem=m_list.GetNextItem(-1,LVIS_SELECTED);
a=m_list.GetItemText(CurItem,1);
HANDLE SnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
SHFILEINFO shSmall;
PROCESSENTRY32 ProcessInfo;//声明进程信息变量
ProcessInfo.dwSize=sizeof(ProcessInfo);//设置ProcessInfo的大小
//返回系统中第一个进程的信息
BOOL Status=Process32First(SnapShot,&ProcessInfo);
int m_nProcess=0;
while(Status)
{
m_nProcess++;
ZeroMemory(&shSmall,sizeof(shSmall));//获取进程文件信息
SHGetFileInfo(ProcessInfo.szExeFile,0,&shSmall,
sizeof(shSmall),SHGFI_ICON|SHGFI_SMALLICON);
str.Format("%08x",ProcessInfo.th32ProcessID);
if(str==a)
{
if((AfxMessageBox("Do you really want to kill the process?",MB_OKCANCEL|MB_DEFBUTTON2))==IDOK)
{
HANDLE hProcess= OpenProcess( PROCESS_ALL_ACCESS, FALSE,ProcessInfo.th32ProcessID);
if(::TerminateProcess(hProcess,1))
{
AfxMessageBox(" The process has been successfully killed",MB_OK);
refresh() ;
break;
}
}
break;
}
//获取下一个进程的信息
Status=Process32Next(SnapShot,&ProcessInfo);
}
}
void KillTime()//移除自动关闭时间
{
UpdateData();
int CurItem=m_list.GetNextItem(-1,LVIS_SELECTED);
if(CurItem>1)
{
m_list.SetItemText(CurItem,3,"");
///////////////////////////////////////////////////////
for(int i=0;i<m_lists.GetSize();i++)
{
dialog1* pItem = (dialog1*)(m_lists.GetAt(i));
if(m_list.GetItemText(CurItem,0)==pItem->name)
{
m_lists.RemoveAt(i);
delete pItem;
}
}
///////////////////////////////////////////////////////
}
else
{
MessageBox("Please select the list item");
}
}
void quit()//退出
{
OnOK();
}
void check()//自动关闭进程检查
{
UpdateData();
CString temp;
CString systime;
CTime t=CTime::GetCurrentTime();
systime.Format("%0.2d:%0.2d:%0.2d",t.GetHour(),t.GetMinute(),t.GetSecond());
int i=m_list.GetItemCount();
for(int j=0;j<i;j++)
{
if(m_list.GetItemText(j,3)!="")
{
if(systime==m_list.GetItemText(j,3))
{
HANDLE SnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
SHFILEINFO shSmall;
PROCESSENTRY32 Pro