对于进程,线程,模块以及PID 的获取,这方面的技术,以前以为挺神秘的,不过,现在看来,其实也就是这么回事吧。。。
一下是实现的界面:
代码如下:
int i = 1;
PROCESSENTRY32 pe32 = {0}; //这个是获取进程信息的一个结构,里边有很丰富的资源,通过它,我们就可以搞定这一切
BOOL bRet;
char buffer[500]; //设置缓冲区,方便我们得到信息
memset(buffer, 0, sizeof(buffer));
m_plist = (CListBox*)GetDlgItem(LST_PROCESS); //通过指针的操作,将控件与CLISTBOX 关联,可见指针的强大
m_plist->ResetContent(); //清空列表框,方便以后动态的填入信息,也不会重复
//在制定的位置插入字符串,当然也可以用 AddString, 但是它只能将字符串插入在最后
m_plist->InsertString(0, "映像名称 PID");
//获取进程快照的句柄,开始我们的操作了:
HANDLE handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == handle)
{
goto Exit0;
}
//填入我们的结构大小
pe32.dwSize = sizeof(PROCESSENTRY32);
//拿到第一个进程信息,然后就是遍历了:
bRet = Process32First(handle, &pe32);
if (!bRet)
{
goto Exit0;
}
do
{
// if (0 == strcmp(pe32.szExeFile,"WINWORD.EXE")) 其实这里改一下就可以用来查询进程是否存在
// {
wsprintf(buffer, "%-10s %-20d", pe32.szExeFile, pe32.th32ProcessID);
m_plist->InsertString(i++, buffer);
// }
} while (Process32Next(handle, &pe32));
wsprintf(buffer, "%d", m_plist->GetCount()); //得到进程总数
SetDlgItemText(STC_NUMS, buffer);
// MessageBox("Ok", "OK", NULL);
Exit0:
if (handle)
{
CloseHandle(handle);
}
UpdateData(FALSE);
return;
当然,如果要得到动态的任务信息,也是很简单的,就是将加入到计时器中就可以了。。。
计时器的频率不要太低,不然程序的其他功能就不能运行了。。。
下边来说一下,计时器的设置吧:
其实就是,让的你控件相应 WM_TIME 这个消息,将它加入到控件的类中。。。
这里,我同时获取了时间:
//获取时间
void CSystemToolDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
SYSTEMTIME time;
char TimeBuffer[20];
memset(TimeBuffer, 0, sizeof(TimeBuffer));
GetLocalTime(&time);
wsprintf(TimeBuffer, "%d/%d/%d %d:%d:%d",
time.wYear, time.wMonth,
time.wDay, time.wHour,
time.wMinute, time.wSecond);
SetDlgItemText(EDT_TIME, TimeBuffer);
GetCurrentProcess(); //这是我加入的得到任务信息的函数,就达到了动态更新的目的。
CDialog::OnTimer(nIDEvent);
}
计时器在初始化的函数中设置,其实,你也可以根据需要在其他地方设置,关键是以什么样的方式去触发它:
下边我是在初始化设置的:
BOOL CSystemToolDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
// TODO: Add extra initialization here
SetTimer(1, 1000, NULL); //设置定时器 第二个参数为 回调的周期,不要太小
return TRUE; // return TRUE unless you set the focus to a control
}