15,471
社区成员
发帖
与我相关
我的任务
分享
BOOL CTaskMgrDlg::GetProcess(void)
{
HANDLE hProcess = INVALID_HANDLE_VALUE;
hProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //建立进程快照
if(INVALID_HANDLE_VALUE == hProcess)
{
//获取进程信息失败
m_listProcess.InsertItem(0, _T("获取进程信息失败!!!"));
return FALSE;
}
//成功获取进程信息
PROCESSENTRY32 pe32 = {0};
pe32.dwFlags = sizeof(PROCESSENTRY32); //必须填充结构体的大小值
CString pid; //进程ID
CString pname; //进程名字
CString ppath; //进程路径
int cnt = 0; //下标
BOOL moreProc = Process32First(hProcess, &pe32); //获取第一个进程的信息
if(moreProc == FALSE)
{
CString tmp;
tmp.Format(_T("%d"), GetLastError());
AfxMessageBox(tmp);
////////////////////////////////////////////////////////////////////////////////
//这里出错了……………………应该说是Process32First出错了
//这里返回24,也就是ERROR_NO_MORE_FILES
}
while(moreProc) //枚举所有进程
{
pid.Format(_T("%d"), pe32.th32ProcessID);
pname.Format(_T("%s"), pe32.szExeFile);
m_listProcess.InsertColumn(cnt, pid);
m_listProcess.SetItemText(cnt, 1, pname);
moreProc = Process32Next(hProcess, &pe32);
++cnt;
}
CloseHandle(hProcess); //释放进程快照
return TRUE;
}