VC + + Basic Class---Implement a simple task Manager

Source: Internet
Author: User

 Because the task manager involves enumeration of processes, combine the knowledge points of the two lessons together!  ①, design interface, and binding of list control variables; ②, List control style designation: M_tasklist.setextendedstyle (Lvs_ex_gridlines | Lvs_ex_fullrowselect); M_tasklist.insertcolumn (0, _t ("process name"), 0, [1], _t ("PID"), 0; m_ Tasklist.insertcolumn (2, _t ("File path"), 0, 380),  ③, process List acquisition: The specific method is also a variety of, specifically see: http://www.cctry.com/ thread-11857-1-1.html Here we use the first method to explain:  #include <Tlhelp32.h> #include <Psapi.h> #pragma comment ( LIB, "Psapi.lib") BOOL cmfctestdlg::getprocesslist () { bool bresult = false; handle Hsnap = CreateToolhelp32Snapshot (th32cs_snapprocess, 0);  if (Hsnap = = invalid_handle_value)   return FALSE; & Nbsp;int idx = 0; cstring Strid; handle hprocess = null; processentry32 Info = {0}; info.dwsize = size of (PROCESSENTRY32);   bool bRet = Process32First (Hsnap, &info);  while (bRet) {  idx = m_ Tasklist.insertitem (M_tasklist.getitemcount (), _t (""));  m_tasklist.setitemtext (idx, 0, Info.szexefile);  strid.empty ();  Strid.format (_t ("%d"), Info.th32processid);  m_tasklist.setitemtext (IDX, 1, Strid);   hprocess = openprocess (process_query_information | Process_vm_read, FALSE, Info.th32processid);  if (hprocess) {   tchar Szpath[max_path] = {0};   getmodulefilenameex (hprocess, NULL, szpath, MAX_PATH);   m_tasklist.setitemtext (IDX, 2, SzPath);  }  BRet = Process32Next (Hsnap, &info);  } return TRUE;}  ④, Debug privilege elevation function: BOOL adjustprivileges () { handle Htoken = null; token_privileges TP = {0}; token_ Privileges OLDTP = {0}; dword dwsize = sizeof (token_privileges);  luid LUID = {0};  if (! OpenProcessToken (GetCurrentProcess (), Token_adjust_privileges | Token_query, &htoken)) {  if (GetLastError () ==error_call_not_implemented)    return TRUE;  else   return false; } if (! Lookupprivilegevalue (NULL, Se_debug_name, &luid)) {&nbsp CloseHandle (htoken);  return FALSE;&NBSP;}&NBSP;&NBSP;TP. PRIVILEGECOUNT=1;&NBSP;TP. Privileges[0]. Luid = Luid; tp. Privileges[0]. Attributes = se_privilege_enabled;  /* Adjust Token privileges */ if (! AdjustTokenPrivileges (Htoken, FALSE, &AMP;TP, sizeof (token_privileges), &AMP;OLDTP, &dwsize)) {  CloseHandle (htoken);  return false; }  //close Handles closehandle (htoken);  return TRUE;}  ⑤, menu resource Additions and edits: Refresh list: Id_m_refresh_list end this process: Id_m_edn_task location file: Id_m_find_exe copy path: Id_m_copy_path ⑥, Popup for menu resources: void Cmfctestdlg::onnmrclicktasklist (NMHDR *pnmhdr, LRESULT *presult) { lpnmitemactivate Pnmitemactivate = reinterpret_cast<lpnmitemactivate> (PNMHDR)  if (Pnmitemactivate->iitem < 0) return;  cstring Strid = M_tasklist.getitemtext (Pnmitemactivate->iitem, 1);   CPoint pt;  getcursorpos (&pt);  cmenu Mmenu, *pmenu; mmenu.loadmenu (idr_menu);  pMenu = Mmenu.getsubmenu (0)  if (Strid = = _t ("0") | | strid = = _t ("4")) {  Pmenu->enablemenuitem (Id_m_edn_task, mf_bycommand| mf_grayed);  Pmenu->enablemenuitem (Id_m_find_exe, mf_bycommand| mf_grayed);  Pmenu->enablemenuitem (Id_m_copy_path, mf_bycommand| mf_grayed);  } pmenu->trackpopupmenu (Tpm_leftalign | Tpm_rightbutton, Pt.x, Pt.y, this);   *presult = 0;}  ⑦, menu response function implementation: void Cmfctestdlg::onmrefreshlist () { for (int idx = 0; idx < M_imglist.getimagecount (); idx++) {  m_imglist.remove (IDX);  }  m_tasklist.deleteallitems ();  getprocesslist ();}  void Cmfctestdlg::onmedntask () { int idx = M_tasklist.getselectionmark ();  cstring StrID = m_ Tasklist.getitemtext (idx, 1);  cstring strName = m_tasklist.getitemtext (idx, 0);   int iRet = MessageBox ( _t ("Are you sure you want to end") +strname+_t ("? "), _t (" friendly hint "), Mb_okcancel),  if (IRet = = IDCANCEL) return;  if (Strid = _t (" 0 ") | | StrName = = _t ("4")) {//actually the judgment of this position is superfluous   MessageBox (_t ("system process, cannot end!") "), _t (" hint ")); }else{  BOOL bRet = false;  HANDLE hprocess = OpenProcess (process_terminate| Process_query_information, FALSE, _tstoi (Strid));  if (hprocess! = NULL) {   bret = TerminateProcess (HPro Cess,-1);   closehandle (hprocess); }   if (!bret) {   messagebox (_T ("End process \" ") + S Trname + _t ("\" failed, please try again!) "), _t (" hint ")); }else{   m_tasklist.deleteitem (idx); } }} void cmfctestdlg::o Nmfindexe () { int idx = M_tasklist.getselectionmark ();  cstring strpath = M_tasklist.getitemtext (idx, 2);  if (strpath.getlength () > 0)   ShellExecute (M_hwnd, _t ("open"), _t ("Explorer.exe"), _t ("/select,") + strpath, NULL, sw_show);}  void Cmfctestdlg::onmcopypath () { int idx = M_tasklist.getselectionmark ();  cstring StrPath = m_ Tasklist.getitemtext (idx, 2);   if (! OpenClipboard ()) return; if (! EmptyClipboard ()) {  CloseClipboard ();  return; }  size_t cbstr = (strpath.getlength () +1) *sizeof (TCHAR);  hglobal hdata = GlobalAlloc (GMEM_ Moveable, cbstr);  memcpy_s (GlobalLock (Hdata), Cbstr, Strpath.lockbuffer (), cbstr);  globalunlock (HData);  strpath.unlockbuffer ();   uint UIFormat = (sizeof (TCHAR) = = sizeof (WCHAR))? Cf_unicodetext:cf_text; if (:: SetClipboardData (UIFormat, hdata) = = NULL) {  MessageBox (_t ("Set clipboard data failed!") "));  CloseClipboard ();  return; }  closeclipboard ();  ⑧, add program prefix icon for list control://H header file for image list definition: CImageList m_imglist; //OnInitDialog () function to create and set image list: m_ Imglist.create (Ilc_color24, M_tasklist.setimagelist, Lvsil_small),  ⑨ (&m_imglist); Modify the Getprocesslist () function as follows: BOOL cmfctestdlg::getprocesslist () { bool bresult = false; handle HSnap = CreateToolhelp32Snapshot (th32cs_snapprocess, 0);  if (Hsnap = = invalid_handle_value)   return FALSE; & Nbsp;int idx = 0, Imgidx = -1; cstring strid;&nbSp HANDLE hprocess = null; cstring strpath, Strsys (_t ("SystemRoot"));  processentry32 info = {0};  info.dwsize = sizeof (PROCESSENTRY32),   bool bRet = Process32First (Hsnap, &info);  while (BRet) {   hprocess = openprocess (process_query_information | Process_vm_read, FALSE, Info.th32processid);  if (hprocess) {   strpath.empty ();    Getmodulefilenameex (hprocess, NULL, Strpath.getbuffersetlength (MAX_PATH), MAX_PATH);    Strpath.releasebuffer ();   strpath.trimleft (_t ("\ \"));    if (Strpath.left (Strsys.getlength ()). CompareNoCase (strsys) = = 0) {    TCHAR Szwin[max_path] = {0};    GetWindowsDirectory (Szwin, MAX_PATH) ;    Strpath.replace (Strsys, Szwin);   }    shfileinfo shinfo = {0};    Shgetfileinfo (strpath, 0, &shinfo, sizeof (shfileinfo), shgfi_smallicon| Shgfi_icon);   imgidx = M_imglist.add (shinfo.hicon); }  //InInsert   IDX = M_tasklist.insertitem (M_tasklist.getitemcount (), _t (""), Imgidx);  M_tasklist.setitemtext ( IDX, 0, Info.szexefile);  strid.empty ();  Strid.format (_t ("%d"), Info.th32processid);  m_ Tasklist.setitemtext (IDX, 1, Strid);  m_tasklist.setitemtext (IDX, 2, strpath);  //Continue traversing the process list   BRet = Process32Next (Hsnap, &info);  }  return TRUE;}   1, using the EnumProcesses function for the enumeration of processes, 2, for the selection of multiple processes at the end of processing!  

VC + + Basic Class---Implement a simple task manager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.