Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4627 lines
126 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // TaskMan - NT TaskManager
  4. // Copyright (C) Microsoft
  5. //
  6. // File: procpage.cpp
  7. //
  8. // History: Nov-16-95 DavePl Created
  9. //
  10. //--------------------------------------------------------------------------
  11. #include "precomp.h"
  12. //
  13. // Project-scope globals
  14. //
  15. DWORD g_cProcesses = 0;
  16. extern WCHAR g_szTimeSep[];
  17. extern WCHAR g_szGroupThousSep[];
  18. extern ULONG g_ulGroupSep;
  19. //--------------------------------------------------------------------------
  20. // TERMINAL SERVICES
  21. //-- cache this state
  22. BOOL IsUserAdmin( )
  23. {
  24. // Note that local static initialization is not thread safe,
  25. // but this function is only called from the process page dialog
  26. // proc (i.e. single thread).
  27. static BOOL sbIsUserAdmin = SHTestTokenMembership(NULL, DOMAIN_ALIAS_RID_ADMINS);
  28. return sbIsUserAdmin;
  29. }
  30. // get/set current session id.
  31. // we use this session id to filter the processes for current session.
  32. DWORD gdwSessionId = static_cast<DWORD>(-1);
  33. inline DWORD GetCurrentSessionID( )
  34. {
  35. return gdwSessionId;
  36. }
  37. inline VOID SetCurrentSessionID( DWORD dwSessionId )
  38. {
  39. gdwSessionId = dwSessionId;
  40. }
  41. // END OF TERMINAL SERVICES DECLs
  42. //--------------------------------------------------------------------------
  43. //
  44. // File-scope globals
  45. //
  46. SYSTEM_BASIC_INFORMATION g_BasicInfo;
  47. //
  48. // Table of which resource IDs in the column selection dialog
  49. // correspond to which columns
  50. //
  51. const int g_aDlgColIDs[] =
  52. {
  53. IDC_IMAGENAME,
  54. IDC_PID,
  55. IDC_USERNAME,
  56. IDC_SESSIONID,
  57. IDC_CPU,
  58. IDC_CPUTIME,
  59. IDC_MEMUSAGE,
  60. IDC_MEMPEAK,
  61. IDC_MEMUSAGEDIFF,
  62. IDC_PAGEFAULTS,
  63. IDC_PAGEFAULTSDIFF,
  64. IDC_COMMITCHARGE,
  65. IDC_PAGEDPOOL,
  66. IDC_NONPAGEDPOOL,
  67. IDC_BASEPRIORITY,
  68. IDC_HANDLECOUNT,
  69. IDC_THREADCOUNT,
  70. IDC_USEROBJECTS,
  71. IDC_GDIOBJECTS,
  72. IDC_READOPERCOUNT,
  73. IDC_WRITEOPERCOUNT,
  74. IDC_OTHEROPERCOUNT,
  75. IDC_READXFERCOUNT,
  76. IDC_WRITEXFERCOUNT,
  77. IDC_OTHERXFERCOUNT
  78. };
  79. //
  80. // Column ID on which to sort in the listview, and for
  81. // compares in general
  82. //
  83. COLUMNID g_iProcSortColumnID = COL_PID;
  84. INT g_iProcSortDirection = 1; // 1 = asc, -1 = desc
  85. //
  86. // Column Default Info
  87. //
  88. struct
  89. {
  90. INT Format;
  91. INT Width;
  92. } ColumnDefaults[NUM_COLUMN] =
  93. {
  94. { LVCFMT_LEFT, 0x6B }, // COL_IMAGENAME
  95. { LVCFMT_RIGHT, 50 }, // COL_PID
  96. { LVCFMT_LEFT, 0x6B }, // COL_USERNAME
  97. { LVCFMT_RIGHT, 70 }, // COL_SESSIONID
  98. { LVCFMT_RIGHT, 35}, // COL_CPU
  99. { LVCFMT_RIGHT, 70 }, // COL_CPUTIME
  100. { LVCFMT_RIGHT, 70 }, // COL_MEMUSAGE
  101. { LVCFMT_RIGHT, 100 }, // COL_MEMPEAK
  102. { LVCFMT_RIGHT, 70 }, // COL_MEMUSAGEDIFF
  103. { LVCFMT_RIGHT, 70 }, // COL_PAGEFAULTS
  104. { LVCFMT_RIGHT, 70 }, // COL_PAGEFAULTSDIFF
  105. { LVCFMT_RIGHT, 70 }, // COL_COMMITCHARGE
  106. { LVCFMT_RIGHT, 70 }, // COL_PAGEDPOOL
  107. { LVCFMT_RIGHT, 70 }, // COL_NONPAGEDPOOL
  108. { LVCFMT_RIGHT, 60 }, // COL_BASEPRIORITY
  109. { LVCFMT_RIGHT, 60 }, // COL_HANDLECOUNT
  110. { LVCFMT_RIGHT, 60 }, // COL_THREADCOUNT
  111. { LVCFMT_RIGHT, 60 }, // COL_USEROBJECTS
  112. { LVCFMT_RIGHT, 60 }, // COL_GDIOBJECTS
  113. { LVCFMT_RIGHT, 70 }, // COL_READOPERCOUNT
  114. { LVCFMT_RIGHT, 70 }, // COL_WRITEOPERCOUNT
  115. { LVCFMT_RIGHT, 70 }, // COL_OTHEROPERCOUNT
  116. { LVCFMT_RIGHT, 70 }, // COL_READXFERCOUNT
  117. { LVCFMT_RIGHT, 70 }, // COL_WRITEXFERCOUNT
  118. { LVCFMT_RIGHT, 70 } // COL_OTHERXFERCOUNT
  119. };
  120. /*++ class CProcInfo
  121. Class Description:
  122. Represents the last known information about a running process
  123. Arguments:
  124. Return Value:
  125. Revision History:
  126. Nov-16-95 Davepl Created
  127. --*/
  128. class CProcInfo
  129. {
  130. public:
  131. LARGE_INTEGER m_uPassCount;
  132. DWORD m_UniqueProcessId;
  133. LPWSTR m_pszUserName;
  134. ULONG m_SessionId;
  135. BYTE m_CPU;
  136. BYTE m_DisplayCPU;
  137. LARGE_INTEGER m_CPUTime;
  138. LARGE_INTEGER m_DisplayCPUTime;
  139. SIZE_T m_MemUsage;
  140. SSIZE_T m_MemDiff;
  141. ULONG m_PageFaults;
  142. LONG m_PageFaultsDiff;
  143. ULONG_PTR m_CommitCharge;
  144. ULONG_PTR m_PagedPool;
  145. ULONG_PTR m_NonPagedPool;
  146. KPRIORITY m_PriClass;
  147. ULONG m_HandleCount;
  148. ULONG m_ThreadCount;
  149. ULONG m_GDIObjectCount;
  150. ULONG m_USERObjectCount;
  151. LONGLONG m_IoReadOperCount;
  152. LONGLONG m_IoWriteOperCount;
  153. LONGLONG m_IoOtherOperCount;
  154. LONGLONG m_IoReadXferCount;
  155. LONGLONG m_IoWriteXferCount;
  156. LONGLONG m_IoOtherXferCount;
  157. LPWSTR m_pszImageName;
  158. CProcInfo * m_pWowParentProcInfo; // non-NULL for WOW tasks
  159. WORD m_htaskWow; // non-zero for WOW tasks
  160. BOOL m_fWowProcess:1; // TRUE for real WOW process
  161. BOOL m_fWowProcessTested:1; // TRUE once fWowProcess is valid
  162. SIZE_T m_MemPeak;
  163. //
  164. // This is a union of who (which column) is dirty. You can look at
  165. // or set any particular column's bit, or just inspect m_fDirty
  166. // to see if anyone at all is dirty. Used to optimize listview
  167. // painting
  168. //
  169. union
  170. {
  171. DWORD m_fDirty;
  172. #pragma warning(disable:4201) // Nameless struct or union
  173. struct
  174. {
  175. DWORD m_fDirty_COL_CPU :1;
  176. DWORD m_fDirty_COL_CPUTIME :1;
  177. DWORD m_fDirty_COL_MEMUSAGE :1;
  178. DWORD m_fDirty_COL_MEMUSAGEDIFF :1;
  179. DWORD m_fDirty_COL_PAGEFAULTS :1;
  180. DWORD m_fDirty_COL_PAGEFAULTSDIFF :1;
  181. DWORD m_fDirty_COL_COMMITCHARGE :1;
  182. DWORD m_fDirty_COL_PAGEDPOOL :1;
  183. DWORD m_fDirty_COL_NONPAGEDPOOL :1;
  184. DWORD m_fDirty_COL_BASEPRIORITY :1;
  185. DWORD m_fDirty_COL_HANDLECOUNT :1;
  186. DWORD m_fDirty_COL_IMAGENAME :1;
  187. DWORD m_fDirty_COL_PID :1;
  188. DWORD m_fDirty_COL_SESSIONID :1;
  189. DWORD m_fDirty_COL_USERNAME :1;
  190. DWORD m_fDirty_COL_THREADCOUNT :1;
  191. DWORD m_fDirty_COL_GDIOBJECTS :1;
  192. DWORD m_fDirty_COL_USEROBJECTS :1;
  193. DWORD m_fDirty_COL_MEMPEAK :1;
  194. DWORD m_fDirty_COL_READOPERCOUNT :1;
  195. DWORD m_fDirty_COL_WRITEOPERCOUNT :1;
  196. DWORD m_fDirty_COL_OTHEROPERCOUNT :1;
  197. DWORD m_fDirty_COL_READXFERCOUNT :1;
  198. DWORD m_fDirty_COL_WRITEXFERCOUNT :1;
  199. DWORD m_fDirty_COL_OTHERXFERCOUNT :1;
  200. };
  201. #pragma warning(default:4201) // Nameless struct or union
  202. };
  203. HRESULT SetData(LARGE_INTEGER TotalTime,
  204. PSYSTEM_PROCESS_INFORMATION pInfo,
  205. LARGE_INTEGER uPassCount,
  206. CProcPage * pProcPage,
  207. BOOL fUpdateOnly);
  208. HRESULT SetProcessUsername(const FILETIME *CreationTime);
  209. HRESULT SetDataWowTask(LARGE_INTEGER TotalTime,
  210. DWORD dwThreadId,
  211. CHAR * pszFilePath,
  212. LARGE_INTEGER uPassCount,
  213. CProcInfo * pParentProcInfo,
  214. LARGE_INTEGER *pTimeLeft,
  215. WORD htask,
  216. BOOL fUpdateOnly);
  217. CProcInfo()
  218. {
  219. ZeroMemory(this, sizeof(*this));
  220. m_SessionId = 832;
  221. }
  222. ~CProcInfo()
  223. {
  224. if (m_pszImageName)
  225. {
  226. LocalFree( m_pszImageName );
  227. }
  228. if( m_pszUserName != NULL )
  229. {
  230. LocalFree( m_pszUserName );
  231. }
  232. }
  233. BOOL OkToShowThisProcess ()
  234. {
  235. // this function determines if the process should be listed in the view.
  236. return GetCurrentSessionID() == m_SessionId;
  237. }
  238. // Invalidate() marks this proc with a bogus pid so that it is removed
  239. // on the next cleanup pass
  240. void Invalidate()
  241. {
  242. m_UniqueProcessId = PtrToUlong(INVALID_HANDLE_VALUE);
  243. }
  244. LONGLONG GetCPUTime() const
  245. {
  246. return m_CPUTime.QuadPart;
  247. }
  248. INT Compare(CProcInfo * pOther);
  249. //
  250. // Is this a WOW task psuedo-process?
  251. //
  252. INT_PTR IsWowTask(void) const
  253. {
  254. return (INT_PTR) m_pWowParentProcInfo;
  255. }
  256. //
  257. // Get the Win32 PID for this task
  258. //
  259. DWORD GetRealPID(void) const
  260. {
  261. return m_pWowParentProcInfo
  262. ? m_pWowParentProcInfo->m_UniqueProcessId
  263. : m_UniqueProcessId;
  264. }
  265. void SetCPU(LARGE_INTEGER CPUTimeDelta,
  266. LARGE_INTEGER TotalTime,
  267. BOOL fDisplayOnly);
  268. };
  269. /*++ ColSelectDlgProc
  270. Function Description:
  271. Dialog Procedure for the column selection dialog
  272. Arguments:
  273. Standard wndproc stuff
  274. Revision History:
  275. Jan-05-96 Davepl Created
  276. --*/
  277. INT_PTR CALLBACK ColSelectDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  278. {
  279. static CProcPage * pPage = NULL;
  280. switch(uMsg)
  281. {
  282. case WM_INITDIALOG:
  283. {
  284. pPage = (CProcPage *) lParam;
  285. //
  286. // Start with none of the boxes checked
  287. //
  288. for (int i = 0; i < NUM_COLUMN; i++)
  289. {
  290. CheckDlgButton(hwndDlg, g_aDlgColIDs[i], BST_UNCHECKED);
  291. }
  292. //
  293. // HIDE the Username and SessionId if its not Terminal Server.
  294. //
  295. if( !g_fIsTSEnabled )
  296. {
  297. ShowWindow( GetDlgItem( hwndDlg , IDC_USERNAME ) , SW_HIDE );
  298. ShowWindow( GetDlgItem( hwndDlg , IDC_SESSIONID ) , SW_HIDE );
  299. }
  300. //
  301. // Then turn on the ones for the columns we have active
  302. //
  303. for (i = 0; i < NUM_COLUMN + 1; i++)
  304. {
  305. if (g_Options.m_ActiveProcCol[i] == -1)
  306. {
  307. break;
  308. }
  309. CheckDlgButton(hwndDlg, g_aDlgColIDs[g_Options.m_ActiveProcCol[i]], BST_CHECKED);
  310. }
  311. }
  312. return TRUE; // don't set focus
  313. case WM_COMMAND:
  314. //
  315. // If user clicked OK, add the columns to the array and reset the listview
  316. //
  317. if (LOWORD(wParam) == IDOK)
  318. {
  319. // First, make sure the column width array is up to date
  320. pPage->SaveColumnWidths();
  321. INT iCol = 1;
  322. g_Options.m_ActiveProcCol[0] = COL_IMAGENAME;
  323. for (int i = 1; i < NUM_COLUMN && g_aDlgColIDs[i] >= 0; i++)
  324. {
  325. if (BST_CHECKED == IsDlgButtonChecked(hwndDlg, g_aDlgColIDs[i]))
  326. {
  327. // It is checked
  328. if (g_Options.m_ActiveProcCol[iCol] != (COLUMNID) i)
  329. {
  330. // If the column wasn't already there, insert its column
  331. // width into the column width array
  332. ShiftArray(g_Options.m_ColumnWidths, iCol, SHIFT_UP);
  333. ShiftArray(g_Options.m_ActiveProcCol, iCol, SHIFT_UP);
  334. g_Options.m_ColumnWidths[iCol] = ColumnDefaults[ i ].Width;
  335. g_Options.m_ActiveProcCol[iCol] = (COLUMNID) i;
  336. }
  337. iCol++;
  338. }
  339. else
  340. {
  341. // Not checked, column not active. If it used to be active,
  342. // remove its column width from the column width array
  343. if (g_Options.m_ActiveProcCol[iCol] == (COLUMNID) i)
  344. {
  345. ShiftArray(g_Options.m_ColumnWidths, iCol, SHIFT_DOWN);
  346. ShiftArray(g_Options.m_ActiveProcCol, iCol, SHIFT_DOWN);
  347. }
  348. }
  349. }
  350. // Terminate the column list
  351. g_Options.m_ActiveProcCol[iCol] = (COLUMNID) -1;
  352. pPage->SetupColumns();
  353. pPage->TimerEvent();
  354. EndDialog(hwndDlg, IDOK);
  355. }
  356. else if (LOWORD(wParam) == IDCANCEL)
  357. {
  358. EndDialog(hwndDlg, IDCANCEL);
  359. }
  360. break;
  361. }
  362. return FALSE;
  363. }
  364. /*++ CProcPage::~CProcPage()
  365. - Destructor
  366. */
  367. CProcPage::~CProcPage()
  368. {
  369. Destroy( );
  370. }
  371. /*++ CProcPage::PickColumns()
  372. Function Description:
  373. Puts up UI that lets the user select what columns to display in the
  374. process page, and then resets the listview with the new column list
  375. Arguments:
  376. none
  377. Return Value:
  378. none
  379. Revision History:
  380. Jan-05-96 Davepl Created
  381. --*/
  382. void CProcPage::PickColumns()
  383. {
  384. DialogBoxParam(g_hInstance,
  385. MAKEINTRESOURCE(IDD_SELECTPROCCOLS),
  386. g_hMainWnd,
  387. ColSelectDlgProc,
  388. (LPARAM) this);
  389. }
  390. /*++ GetPriRanking
  391. Function Description:
  392. Since the priority class defines aren't in order, this helper
  393. exists to make comparisons between pri classes easier. It returns
  394. a larger number for "higher" priority classes
  395. Arguments:
  396. Return Value:
  397. rank of priority (0 to 5)
  398. Revision History:
  399. Nov-27-95 Davepl Created
  400. --*/
  401. DWORD GetPriRanking(DWORD dwClass)
  402. {
  403. switch(dwClass)
  404. {
  405. case REALTIME_PRIORITY_CLASS:
  406. return 5;
  407. case HIGH_PRIORITY_CLASS:
  408. return 4;
  409. case ABOVE_NORMAL_PRIORITY_CLASS:
  410. return 3;
  411. case NORMAL_PRIORITY_CLASS:
  412. return 2;
  413. case BELOW_NORMAL_PRIORITY_CLASS:
  414. return 1;
  415. default:
  416. return 0;
  417. }
  418. }
  419. /*++ QuickConfirm
  420. Function Description:
  421. Gets a confirmation for things like terminating/debugging processes
  422. Arguments:
  423. idtitle - string ID of title for message box
  424. idmsg - string ID of message body
  425. Return Value:
  426. IDNO/IDYES, whatever comes back from MessageBox
  427. Revision History:
  428. Nov-28-95 Davepl Created
  429. --*/
  430. UINT CProcPage::QuickConfirm(UINT idTitle, UINT idBody)
  431. {
  432. //
  433. // Get confirmation before we dust the process, or something similar
  434. //
  435. WCHAR szTitle[MAX_PATH];
  436. WCHAR szBody[MAX_PATH];
  437. if (0 == LoadString(g_hInstance, idTitle, szTitle, ARRAYSIZE(szTitle)) ||
  438. 0 == LoadString(g_hInstance, idBody, szBody, ARRAYSIZE(szBody)))
  439. {
  440. return IDNO;
  441. }
  442. if (IDYES == MessageBox(m_hPage, szBody, szTitle, MB_ICONEXCLAMATION | MB_YESNO))
  443. {
  444. return IDYES;
  445. }
  446. return IDNO;
  447. }
  448. /*++ class CProcPage::SetupColumns
  449. Class Description:
  450. Removes any existing columns from the process listview and
  451. adds all of the columns listed in the g_Options.m_ActiveProcCol array.
  452. Arguments:
  453. Return Value:
  454. HRESULT
  455. Revision History:
  456. Nov-16-95 Davepl Created
  457. --*/
  458. static const _aIDColNames[NUM_COLUMN] =
  459. {
  460. IDS_COL_IMAGENAME,
  461. IDS_COL_PID,
  462. IDS_COL_USERNAME,
  463. IDS_COL_SESSIONID,
  464. IDS_COL_CPU,
  465. IDS_COL_CPUTIME,
  466. IDS_COL_MEMUSAGE,
  467. IDS_COL_MEMPEAK,
  468. IDS_COL_MEMUSAGEDIFF,
  469. IDS_COL_PAGEFAULTS,
  470. IDS_COL_PAGEFAULTSDIFF,
  471. IDS_COL_COMMITCHARGE,
  472. IDS_COL_PAGEDPOOL,
  473. IDS_COL_NONPAGEDPOOL,
  474. IDS_COL_BASEPRIORITY,
  475. IDS_COL_HANDLECOUNT,
  476. IDS_COL_THREADCOUNT,
  477. IDS_COL_USEROBJECTS,
  478. IDS_COL_GDIOBJECTS,
  479. IDS_COL_READOPERCOUNT,
  480. IDS_COL_WRITEOPERCOUNT,
  481. IDS_COL_OTHEROPERCOUNT,
  482. IDS_COL_READXFERCOUNT,
  483. IDS_COL_WRITEXFERCOUNT,
  484. IDS_COL_OTHERXFERCOUNT
  485. };
  486. HRESULT CProcPage::SetupColumns()
  487. {
  488. HWND hwndList = GetDlgItem(m_hPage, IDC_PROCLIST);
  489. if (NULL == hwndList)
  490. {
  491. return E_UNEXPECTED;
  492. }
  493. ListView_DeleteAllItems(hwndList);
  494. // Remove all existing columns
  495. LV_COLUMN lvcolumn;
  496. while(ListView_DeleteColumn(hwndList, 0))
  497. {
  498. NULL;
  499. }
  500. // Add all of the new columns
  501. INT iColumn = 0;
  502. while (g_Options.m_ActiveProcCol[iColumn] >= 0)
  503. {
  504. INT idColumn = g_Options.m_ActiveProcCol[iColumn];
  505. // idc_username or IDC_SESSIONID are available only for terminalserver.
  506. ASSERT((idColumn != COL_USERNAME && idColumn != COL_SESSIONID) || g_fIsTSEnabled);
  507. WCHAR szTitle[MAX_PATH];
  508. LoadString(g_hInstance, _aIDColNames[idColumn], szTitle, ARRAYSIZE(szTitle));
  509. lvcolumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_TEXT | LVCF_WIDTH;
  510. lvcolumn.fmt = ColumnDefaults[ idColumn ].Format;
  511. // If no width preference has been recorded for this column, use the
  512. // default
  513. if (-1 == g_Options.m_ColumnWidths[iColumn])
  514. {
  515. lvcolumn.cx = ColumnDefaults[ idColumn ].Width;
  516. }
  517. else
  518. {
  519. lvcolumn.cx = g_Options.m_ColumnWidths[iColumn];
  520. }
  521. lvcolumn.pszText = szTitle;
  522. lvcolumn.iSubItem = iColumn;
  523. if (-1 == ListView_InsertColumn(hwndList, iColumn, &lvcolumn))
  524. {
  525. return E_FAIL;
  526. }
  527. iColumn++;
  528. }
  529. return S_OK;
  530. }
  531. //
  532. // Take two unsigned 64-bit values and compare them in a manner
  533. // that CProcInfo::Compare likes.
  534. //
  535. int Compare64(unsigned __int64 First, unsigned __int64 Second)
  536. {
  537. if (First < Second)
  538. {
  539. return -1;
  540. }
  541. else if (First > Second)
  542. {
  543. return 1;
  544. }
  545. else
  546. {
  547. return 0;
  548. }
  549. }
  550. /*++ class CProcInfo::Compare
  551. Class Description:
  552. Compares this CProcInfo object to another, and returns its ranking
  553. based on the g_iProcSortColumnID field.
  554. Note that if the objects are equal based on the current sort column,
  555. the PID is used as a secondary sort key to prevent items from
  556. jumping around in the listview
  557. WOW psuedo-processes always sort directly after their parent
  558. ntvdm.exe process. So really the sort order is:
  559. 1. WOW task psuedo-processes under parent in alpha order
  560. 2. User's selected order.
  561. 3. PID
  562. Arguments:
  563. pOther - the CProcInfo object to compare this to
  564. Return Value:
  565. < 0 - This CProcInfo is "less" than the other
  566. 0 - Equal (Can't happen, since PID is used to sort)
  567. > 0 - This CProcInfo is "greater" than the other
  568. Revision History:
  569. Nov-20-95 Davepl Created
  570. --*/
  571. INT CProcInfo::Compare(CProcInfo * pOther)
  572. {
  573. CProcInfo * pMyThis;
  574. CProcInfo * pMyOther;
  575. INT iRet = 0;
  576. //
  577. // Wow psuedo-processes don't have any performance information,
  578. // so use the parent "real" ntvdm.exe CProcInfo for sorting.
  579. //
  580. ASSERT(this != pOther);
  581. pMyThis = this->IsWowTask()
  582. ? this->m_pWowParentProcInfo
  583. : this;
  584. pMyOther = pOther->IsWowTask()
  585. ? pOther->m_pWowParentProcInfo
  586. : pOther;
  587. if (pMyThis == pMyOther) {
  588. //
  589. // This implies one or the other or both this and pOther
  590. // are WOW tasks, and they're in the same WOW VDM. Sort
  591. // the "real" process entry first, followed by its associated
  592. // WOW task entries alphabetical.
  593. //
  594. if (this->IsWowTask()) {
  595. if (pOther->IsWowTask()) {
  596. //
  597. // They are siblings and we sort by
  598. // image name.
  599. //
  600. ASSERT(this->m_pWowParentProcInfo == pOther->m_pWowParentProcInfo);
  601. iRet = lstrcmpi(this->m_pszImageName, pOther->m_pszImageName);
  602. } else {
  603. //
  604. // pOther is not a Wow task, it must be ntvdm.exe
  605. // the parent of this. this sorts after pOther.
  606. //
  607. ASSERT(pOther == this->m_pWowParentProcInfo);
  608. iRet = 1;
  609. }
  610. } else {
  611. //
  612. // this is not a Wow task, pOther must be and
  613. // this must be pOther's parent.
  614. //
  615. ASSERT(pOther->IsWowTask());
  616. iRet = -1;
  617. }
  618. }
  619. if (0 == iRet)
  620. {
  621. switch (g_iProcSortColumnID)
  622. {
  623. case COL_CPU:
  624. iRet = Compare64(pMyThis->m_CPU, pMyOther->m_CPU);
  625. break;
  626. case COL_CPUTIME:
  627. iRet = Compare64(pMyThis->m_CPUTime.QuadPart, pMyOther->m_CPUTime.QuadPart);
  628. break;
  629. case COL_MEMUSAGE:
  630. iRet = Compare64(pMyThis->m_MemUsage, pMyOther->m_MemUsage);
  631. break;
  632. case COL_MEMUSAGEDIFF:
  633. iRet = Compare64(pMyThis->m_MemDiff, pMyOther->m_MemDiff);
  634. break;
  635. case COL_MEMPEAK:
  636. iRet = Compare64(pMyThis->m_MemPeak, pMyOther->m_MemPeak);
  637. break;
  638. case COL_PAGEFAULTS:
  639. iRet = Compare64(pMyThis->m_PageFaults, pMyOther->m_PageFaults);
  640. break;
  641. case COL_PAGEFAULTSDIFF:
  642. iRet = Compare64(pMyThis->m_PageFaultsDiff, pMyOther->m_PageFaultsDiff);
  643. break;
  644. case COL_COMMITCHARGE:
  645. iRet = Compare64(pMyThis->m_CommitCharge, pMyOther->m_CommitCharge);
  646. break;
  647. case COL_PAGEDPOOL:
  648. iRet = Compare64(pMyThis->m_PagedPool, pMyOther->m_PagedPool);
  649. break;
  650. case COL_NONPAGEDPOOL:
  651. iRet = Compare64(pMyThis->m_NonPagedPool, pMyOther->m_NonPagedPool);
  652. break;
  653. case COL_BASEPRIORITY:
  654. iRet = Compare64(GetPriRanking(pMyThis->m_PriClass), GetPriRanking(pMyOther->m_PriClass));
  655. break;
  656. case COL_HANDLECOUNT:
  657. iRet = Compare64(pMyThis->m_HandleCount, pMyOther->m_HandleCount);
  658. break;
  659. case COL_THREADCOUNT:
  660. iRet = Compare64(pMyThis->m_ThreadCount, pMyOther->m_ThreadCount);
  661. break;
  662. case COL_PID:
  663. iRet = Compare64(pMyThis->m_UniqueProcessId, pMyOther->m_UniqueProcessId);
  664. break;
  665. case COL_SESSIONID:
  666. iRet = Compare64(pMyThis->m_SessionId, pMyOther->m_SessionId);
  667. break;
  668. case COL_USERNAME:
  669. iRet = lstrcmpi( pMyThis->m_pszUserName , pMyOther->m_pszUserName );
  670. break;
  671. case COL_IMAGENAME:
  672. iRet = lstrcmpi(pMyThis->m_pszImageName, pMyOther->m_pszImageName);
  673. break;
  674. case COL_USEROBJECTS:
  675. iRet = Compare64(pMyThis->m_USERObjectCount, pMyOther->m_USERObjectCount);
  676. break;
  677. case COL_GDIOBJECTS:
  678. iRet = Compare64(pMyThis->m_GDIObjectCount, pMyOther->m_GDIObjectCount);
  679. break;
  680. case COL_READOPERCOUNT:
  681. iRet = Compare64(pMyThis->m_IoReadOperCount, pMyOther->m_IoReadOperCount);
  682. break;
  683. case COL_WRITEOPERCOUNT:
  684. iRet = Compare64(pMyThis->m_IoWriteOperCount, pMyOther->m_IoWriteOperCount);
  685. break;
  686. case COL_OTHEROPERCOUNT:
  687. iRet = Compare64(pMyThis->m_IoOtherOperCount, pMyOther->m_IoOtherOperCount);
  688. break;
  689. case COL_READXFERCOUNT:
  690. iRet = Compare64(pMyThis->m_IoReadXferCount, pMyOther->m_IoReadXferCount);
  691. break;
  692. case COL_WRITEXFERCOUNT:
  693. iRet = Compare64(pMyThis->m_IoWriteXferCount, pMyOther->m_IoWriteXferCount);
  694. break;
  695. case COL_OTHERXFERCOUNT:
  696. iRet = Compare64(pMyThis->m_IoOtherXferCount, pMyOther->m_IoOtherXferCount);
  697. break;
  698. default:
  699. ASSERT(FALSE);
  700. iRet = 0;
  701. break;
  702. }
  703. iRet *= g_iProcSortDirection;
  704. }
  705. // If objects look equal, compare on PID as secondary sort column
  706. // so that items don't jump around in the listview
  707. if (0 == iRet)
  708. {
  709. iRet = Compare64(pMyThis->m_UniqueProcessId, pMyOther->m_UniqueProcessId) * g_iProcSortDirection;
  710. }
  711. return iRet;
  712. }
  713. /*++ class CProcInfo::SetCPU
  714. Method Description:
  715. Sets the CPU percentage.
  716. Arguments:
  717. CPUTime - Time for this process
  718. TotalTime - Total elapsed time, used as the denominator in calculations
  719. Return Value:
  720. Revision History:
  721. 19-Feb-96 DaveHart Created
  722. --*/
  723. void CProcInfo::SetCPU(LARGE_INTEGER CPUTimeDelta,
  724. LARGE_INTEGER TotalTime,
  725. BOOL fDisplayOnly)
  726. {
  727. // Calc CPU time based on this process's ratio of the total process time used
  728. INT cpu = (BYTE) (((CPUTimeDelta.QuadPart / ((TotalTime.QuadPart / 1000) ?
  729. (TotalTime.QuadPart / 1000) : 1)) + 5)
  730. / 10);
  731. if (cpu > 99)
  732. {
  733. cpu = 99;
  734. }
  735. if (m_DisplayCPU != cpu)
  736. {
  737. m_fDirty_COL_CPU = TRUE;
  738. m_DisplayCPU = (BYTE) cpu;
  739. if ( ! fDisplayOnly )
  740. {
  741. m_CPU = (BYTE) cpu;
  742. }
  743. }
  744. }
  745. /*++ CProcPage::GetProcessInfo
  746. Class Description:
  747. Reads the process info table into a virtual alloc'd buffer, resizing
  748. the buffer if needed
  749. Arguments:
  750. Return Value:
  751. Revision History:
  752. Nov-16-95 Davepl Created
  753. --*/
  754. static const int PROCBUF_GROWSIZE = 4096;
  755. HRESULT CProcPage::GetProcessInfo()
  756. {
  757. HRESULT hr = S_OK;
  758. NTSTATUS status;
  759. while(hr == S_OK)
  760. {
  761. if (m_pvBuffer)
  762. {
  763. status = NtQuerySystemInformation(SystemProcessInformation,
  764. m_pvBuffer,
  765. static_cast<ULONG>(m_cbBuffer),
  766. NULL);
  767. //
  768. // If we succeeded, great, get outta here. If not, any error other
  769. // than "buffer too small" is fatal, in which case we bail
  770. //
  771. if (NT_SUCCESS(status))
  772. {
  773. break;
  774. }
  775. if (status != STATUS_INFO_LENGTH_MISMATCH)
  776. {
  777. hr = E_FAIL;
  778. break;
  779. }
  780. }
  781. //
  782. // Buffer wasn't large enough to hold the process info table, so resize it
  783. // to be larger, then retry.
  784. //
  785. if (m_pvBuffer)
  786. {
  787. HeapFree( GetProcessHeap( ), 0, m_pvBuffer );
  788. m_pvBuffer = NULL;
  789. }
  790. m_cbBuffer += PROCBUF_GROWSIZE;
  791. m_pvBuffer = HeapAlloc( GetProcessHeap( ), 0, m_cbBuffer );
  792. if (m_pvBuffer == NULL)
  793. {
  794. hr = E_OUTOFMEMORY;
  795. break;
  796. }
  797. }
  798. return hr;
  799. }
  800. //
  801. //
  802. //
  803. void
  804. CProcPage::Int64ToCommaSepString(
  805. LONGLONG n,
  806. LPTSTR pszOut,
  807. int cchOut
  808. )
  809. {
  810. NUMBERFMT nfmt = { 0 };
  811. WCHAR szText[32];
  812. //
  813. // Convert the 64-bit int to a text string.
  814. //
  815. _i64tow( n, szText, 10 );
  816. //
  817. // Format the number with commas according to locale conventions.
  818. //
  819. nfmt.Grouping = UINT(g_ulGroupSep);
  820. nfmt.lpDecimalSep = nfmt.lpThousandSep = g_szGroupThousSep;
  821. GetNumberFormat(LOCALE_USER_DEFAULT, 0, szText, &nfmt, pszOut, cchOut);
  822. }
  823. /*++ CProcPage::Int64ToCommaSepKString
  824. Class Description:
  825. Convert a 64-bit integer to a string with commas appended
  826. with the "K" units designator.
  827. (2^64)-1 = "18,446,744,073,709,600,000 K" (29 chars).
  828. Arguments:
  829. n - 64-bit integer.
  830. pszOut - Destination character buffer.
  831. Return Value:
  832. None.
  833. Revision History:
  834. Jan-11-99 BrianAu Created
  835. --*/
  836. void
  837. CProcPage::Int64ToCommaSepKString(
  838. LONGLONG n,
  839. LPTSTR pszOut,
  840. int cchOut
  841. )
  842. {
  843. //
  844. // Destined for UI - don't care if it truncates.
  845. //
  846. Int64ToCommaSepString(n, pszOut, cchOut);
  847. StringCchCat( pszOut, cchOut, L" " );
  848. StringCchCat( pszOut, cchOut, g_szK );
  849. }
  850. /*++ CProcPage::RestoreColumnOrder
  851. Routine Description:
  852. Sets the column order from the per-user preference data stored
  853. in the global COptions object.
  854. Arguments:
  855. hwndList - Listview window handle.
  856. Return Value:
  857. Revision History:
  858. Jan-11/99 BrianAu Created
  859. --*/
  860. void
  861. CProcPage::RestoreColumnOrder(
  862. HWND hwndList
  863. )
  864. {
  865. INT rgOrder[ARRAYSIZE(g_Options.m_ColumnPositions)];
  866. INT cOrder = 0;
  867. INT iOrder = 0;
  868. for (int i = 0; i < ARRAYSIZE(g_Options.m_ColumnPositions); i++)
  869. {
  870. iOrder = g_Options.m_ColumnPositions[i];
  871. if (-1 == iOrder)
  872. break;
  873. rgOrder[cOrder++] = iOrder;
  874. }
  875. if (0 < cOrder)
  876. {
  877. const HWND hwndHeader = ListView_GetHeader(hwndList);
  878. ASSERT(Header_GetItemCount(hwndHeader) == cOrder);
  879. Header_SetOrderArray(hwndHeader, Header_GetItemCount(hwndHeader), rgOrder);
  880. }
  881. }
  882. /*++ CProcPage::RememberColumnOrder
  883. Routine Description:
  884. Saves the current column order to the global COptions object
  885. which is later saved to the registry for per-user preferences.
  886. Arguments:
  887. hwndList - Listview window handle.
  888. Return Value:
  889. Revision History:
  890. Jan-11/99 BrianAu Created
  891. --*/
  892. void
  893. CProcPage::RememberColumnOrder(
  894. HWND hwndList
  895. )
  896. {
  897. const HWND hwndHeader = ListView_GetHeader(hwndList);
  898. ASSERT(Header_GetItemCount(hwndHeader) <= ARRAYSIZE(g_Options.m_ColumnPositions));
  899. FillMemory(&g_Options.m_ColumnPositions, sizeof(g_Options.m_ColumnPositions), 0xFF);
  900. Header_GetOrderArray(hwndHeader,
  901. Header_GetItemCount(hwndHeader),
  902. g_Options.m_ColumnPositions);
  903. }
  904. /*++ FindProcInArrayByPID
  905. Class Description:
  906. Walks the ptrarray given and looks for the CProcInfo object
  907. that has the PID supplied. If not found, returns NULL
  908. Arguments:
  909. pArray - The CPtrArray where the CProcInfos could live
  910. pid - The pid to search for
  911. Return Value:
  912. CProcInfo * in the array, if found, or NULL if not
  913. Revision History:
  914. Nov-20-95 Davepl Created
  915. --*/
  916. CProcInfo * FindProcInArrayByPID(CPtrArray * pArray, DWORD pid)
  917. {
  918. for (int i = 0; i < pArray->GetSize(); i++)
  919. {
  920. CProcInfo * pTmp = (CProcInfo *) (pArray->GetAt(i));
  921. if (pTmp->m_UniqueProcessId == pid)
  922. {
  923. // Found it
  924. return pTmp;
  925. }
  926. }
  927. // Not found
  928. return NULL;
  929. }
  930. /*++ InsertIntoSortedArray
  931. Class Description:
  932. Sticks a CProcInfo ptr into the ptrarray supplied at the
  933. appropriate location based on the current sort column (which
  934. is used by the Compare member function)
  935. Arguments:
  936. pArray - The CPtrArray to add to
  937. pProc - The CProcInfo object to add to the array
  938. Return Value:
  939. TRUE if successful, FALSE if fails
  940. Revision History:
  941. Nov-20-95 Davepl Created
  942. --*/
  943. BOOL InsertIntoSortedArray(CPtrArray * pArray, CProcInfo * pProc)
  944. {
  945. INT cItems = pArray->GetSize();
  946. for (INT iIndex = 0; iIndex < cItems; iIndex++)
  947. {
  948. CProcInfo * pTmp = (CProcInfo *) pArray->GetAt(iIndex);
  949. if (pProc->Compare(pTmp) > 0)
  950. {
  951. return pArray->InsertAt(iIndex, pProc);
  952. }
  953. }
  954. return pArray->Add(pProc);
  955. }
  956. /*++ ResortArray
  957. Function Description:
  958. Creates a new ptr array sorted in the current sort order based
  959. on the old array, and then replaces the old with the new
  960. Arguments:
  961. ppArray - The CPtrArray to resort
  962. Return Value:
  963. TRUE if successful, FALSE if fails
  964. Revision History:
  965. Nov-21-95 Davepl Created
  966. --*/
  967. BOOL ResortArray(CPtrArray ** ppArray)
  968. {
  969. // Create a new array which will be sorted in the new
  970. // order and used to replace the existing array
  971. CPtrArray * pNew = new CPtrArray(GetProcessHeap());
  972. if (NULL == pNew)
  973. {
  974. return FALSE;
  975. }
  976. // Insert each of the existing items in the old array into
  977. // the new array in the correct spot
  978. INT cItems = (*ppArray)->GetSize();
  979. for (int i = 0; i < cItems; i++)
  980. {
  981. CProcInfo * pItem = (CProcInfo *) (*ppArray)->GetAt(i);
  982. if (FALSE == InsertIntoSortedArray(pNew, pItem))
  983. {
  984. delete pNew;
  985. return FALSE;
  986. }
  987. }
  988. // Kill off the old array, replace it with the new
  989. delete (*ppArray);
  990. (*ppArray) = pNew;
  991. return TRUE;
  992. }
  993. //
  994. //
  995. //
  996. typedef struct
  997. {
  998. LARGE_INTEGER uPassCount;
  999. CProcPage * pProcPage;
  1000. CProcInfo * pParentProcInfo;
  1001. LARGE_INTEGER TotalTime;
  1002. LARGE_INTEGER TimeLeft;
  1003. } WOWTASKCALLBACKPARMS, *PWOWTASKCALLBACKPARMS;
  1004. //
  1005. //
  1006. //
  1007. BOOL WINAPI WowTaskCallback(
  1008. DWORD dwThreadId,
  1009. WORD hMod16,
  1010. WORD hTask16,
  1011. CHAR *pszModName,
  1012. CHAR *pszFileName,
  1013. LPARAM lparam
  1014. )
  1015. {
  1016. PWOWTASKCALLBACKPARMS pParms = (PWOWTASKCALLBACKPARMS)lparam;
  1017. HRESULT hr;
  1018. hMod16; // unrefernced
  1019. pszModName; // unreferenced
  1020. //
  1021. // See if this task is already in the list.
  1022. //
  1023. CProcInfo * pOldProcInfo;
  1024. pOldProcInfo = FindProcInArrayByPID(
  1025. pParms->pProcPage->m_pProcArray,
  1026. dwThreadId);
  1027. if (NULL == pOldProcInfo)
  1028. {
  1029. //
  1030. // We don't already have this process in our array, so create a new one
  1031. // and add it to the array
  1032. //
  1033. CProcInfo * pNewProcInfo = new CProcInfo;
  1034. if (NULL == pNewProcInfo)
  1035. {
  1036. goto done;
  1037. }
  1038. hr = pNewProcInfo->SetDataWowTask(pParms->TotalTime,
  1039. dwThreadId,
  1040. pszFileName,
  1041. pParms->uPassCount,
  1042. pParms->pParentProcInfo,
  1043. &pParms->TimeLeft,
  1044. hTask16,
  1045. FALSE);
  1046. if (FAILED(hr) ||
  1047. FALSE == pParms->pProcPage->m_pProcArray->Add(pNewProcInfo))
  1048. {
  1049. delete pNewProcInfo;
  1050. goto done;
  1051. }
  1052. }
  1053. else
  1054. {
  1055. //
  1056. // This process already existed in our array, so update its info
  1057. //
  1058. pOldProcInfo->SetDataWowTask(pParms->TotalTime,
  1059. dwThreadId,
  1060. pszFileName,
  1061. pParms->uPassCount,
  1062. pParms->pParentProcInfo,
  1063. &pParms->TimeLeft,
  1064. hTask16,
  1065. TRUE);
  1066. }
  1067. done:
  1068. return FALSE; // continue enumeration
  1069. }
  1070. /*++ class CProcInfo::SetDataWowTask
  1071. Method Description:
  1072. Sets up a single CProcInfo object based on the parameters.
  1073. This is a WOW task pseudo-process entry.
  1074. Arguments:
  1075. dwThreadId
  1076. pszFilePath Fully-qualified path from VDMEnumTaskWOWEx.
  1077. Return Value:
  1078. Revision History:
  1079. 18-Feb-96 DaveHart created
  1080. --*/
  1081. HRESULT CProcInfo::SetDataWowTask(LARGE_INTEGER TotalTime,
  1082. DWORD dwThreadId,
  1083. CHAR * pszFilePath,
  1084. LARGE_INTEGER uPassCount,
  1085. CProcInfo * pParentProcInfo,
  1086. LARGE_INTEGER *pTimeLeft,
  1087. WORD htask,
  1088. BOOL fUpdateOnly)
  1089. {
  1090. CHAR *pchExe;
  1091. //
  1092. // Touch this CProcInfo to indicate the process is still alive
  1093. //
  1094. m_uPassCount.QuadPart = uPassCount.QuadPart;
  1095. //
  1096. // Update the thread's execution times.
  1097. //
  1098. HANDLE hThread;
  1099. NTSTATUS Status;
  1100. OBJECT_ATTRIBUTES obja;
  1101. CLIENT_ID cid;
  1102. ULONGLONG ullCreation;
  1103. InitializeObjectAttributes(
  1104. &obja,
  1105. NULL,
  1106. 0,
  1107. NULL,
  1108. 0 );
  1109. cid.UniqueProcess = 0; // 0 means any process
  1110. cid.UniqueThread = IntToPtr(dwThreadId);
  1111. Status = NtOpenThread(
  1112. &hThread,
  1113. THREAD_QUERY_INFORMATION,
  1114. &obja,
  1115. &cid );
  1116. if ( NT_SUCCESS(Status) )
  1117. {
  1118. ULONGLONG ullExit, ullKernel, ullUser;
  1119. if (GetThreadTimes(
  1120. hThread,
  1121. (LPFILETIME) &ullCreation,
  1122. (LPFILETIME) &ullExit,
  1123. (LPFILETIME) &ullKernel,
  1124. (LPFILETIME) &ullUser
  1125. ) )
  1126. {
  1127. LARGE_INTEGER TimeDelta, Time;
  1128. Time.QuadPart = (LONGLONG)(ullUser + ullKernel);
  1129. TimeDelta.QuadPart = Time.QuadPart - m_CPUTime.QuadPart;
  1130. if (TimeDelta.QuadPart < 0)
  1131. {
  1132. ASSERT(0 && "WOW tasks's cpu total usage went DOWN since last refresh - Bug 247473, Shaunp");
  1133. Invalidate();
  1134. return E_FAIL;
  1135. }
  1136. if (TimeDelta.QuadPart)
  1137. {
  1138. m_fDirty_COL_CPUTIME = TRUE;
  1139. m_CPUTime.QuadPart = Time.QuadPart;
  1140. }
  1141. //
  1142. // Don't allow sum of WOW child task times to
  1143. // exceed ntvdm.exe total. We call GetThreadTimes
  1144. // substantially after we get process times, so
  1145. // this can happen.
  1146. //
  1147. if (TimeDelta.QuadPart > pTimeLeft->QuadPart)
  1148. {
  1149. TimeDelta.QuadPart = pTimeLeft->QuadPart;
  1150. pTimeLeft->QuadPart = 0;
  1151. }
  1152. else
  1153. {
  1154. pTimeLeft->QuadPart -= TimeDelta.QuadPart;
  1155. }
  1156. SetCPU( TimeDelta, TotalTime, FALSE );
  1157. //
  1158. // When WOW tasks are being displayed, the line for ntvdm.exe
  1159. // should show times only for overhead or historic threads,
  1160. // not including any active task threads.
  1161. //
  1162. if (pParentProcInfo->m_DisplayCPUTime.QuadPart > m_CPUTime.QuadPart)
  1163. {
  1164. pParentProcInfo->m_DisplayCPUTime.QuadPart -= m_CPUTime.QuadPart;
  1165. }
  1166. else
  1167. {
  1168. pParentProcInfo->m_DisplayCPUTime.QuadPart = 0;
  1169. }
  1170. m_DisplayCPUTime.QuadPart = m_CPUTime.QuadPart;
  1171. }
  1172. NtClose(hThread);
  1173. }
  1174. if (m_PriClass != pParentProcInfo->m_PriClass) {
  1175. m_fDirty_COL_BASEPRIORITY = TRUE;
  1176. m_PriClass = pParentProcInfo->m_PriClass;
  1177. }
  1178. if( m_SessionId != pParentProcInfo->m_SessionId )
  1179. {
  1180. m_fDirty_COL_SESSIONID = TRUE;
  1181. m_SessionId = pParentProcInfo->m_SessionId;
  1182. }
  1183. if (FALSE == fUpdateOnly)
  1184. {
  1185. DWORD cchLen;
  1186. //
  1187. // Set the task's image name, thread ID, thread count,
  1188. // htask, and parent CProcInfo which do not change over
  1189. // time.
  1190. //
  1191. m_htaskWow = htask;
  1192. m_fDirty_COL_PID = TRUE;
  1193. m_fDirty_COL_IMAGENAME = TRUE;
  1194. m_fDirty_COL_THREADCOUNT = TRUE;
  1195. m_fDirty_COL_USERNAME = TRUE;
  1196. m_fDirty_COL_SESSIONID = TRUE;
  1197. m_UniqueProcessId = dwThreadId;
  1198. m_ThreadCount = 1;
  1199. //
  1200. // We're only interested in the filename of the EXE
  1201. // with the path stripped.
  1202. //
  1203. pchExe = strrchr(pszFilePath, '\\');
  1204. if (NULL == pchExe)
  1205. {
  1206. pchExe = pszFilePath;
  1207. }
  1208. else
  1209. {
  1210. // skip backslash
  1211. pchExe++;
  1212. }
  1213. cchLen = lstrlenA(pchExe);
  1214. //
  1215. // Indent the EXE name by two spaces
  1216. // so WOW tasks look subordinate to
  1217. // their ntvdm.exe
  1218. //
  1219. m_pszImageName = (LPWSTR) LocalAlloc( LPTR, sizeof(*m_pszImageName) * ( cchLen + 3 ) );
  1220. if (NULL == m_pszImageName)
  1221. {
  1222. return E_OUTOFMEMORY;
  1223. }
  1224. m_pszImageName[0] = m_pszImageName[1] = TEXT(' ');
  1225. MultiByteToWideChar(
  1226. CP_ACP,
  1227. 0,
  1228. pchExe,
  1229. cchLen,
  1230. &m_pszImageName[2],
  1231. cchLen
  1232. );
  1233. m_pszImageName[cchLen + 2] = 0; // make sure it is terminated
  1234. //
  1235. // WOW EXE filenames are always uppercase, so lowercase it.
  1236. //
  1237. CharLowerBuff( &m_pszImageName[2], cchLen );
  1238. m_pWowParentProcInfo = pParentProcInfo;
  1239. if( g_fIsTSEnabled )
  1240. {
  1241. SetProcessUsername( LPFILETIME( &ullCreation ) );
  1242. }
  1243. }
  1244. return S_OK;
  1245. }
  1246. /*++ class CProcInfo::SetData
  1247. Class Description:
  1248. Sets up a single CProcInfo object based on the data contained in a
  1249. SYSTEM_PROCESS_INFORMATION block.
  1250. If fUpdate is set, the imagename and icon fields are not processed,
  1251. since they do not change throughout the lifetime of the process
  1252. Arguments:
  1253. TotalTime - Total elapsed time, used as the denominator in calculations
  1254. for the process' CPU usage, etc
  1255. pInfo - The SYSTEM_PROCESS_INFORMATION block for this process
  1256. uPassCount- Current passcount, used to timestamp the last update of
  1257. this objectg
  1258. fUpdate - See synopsis
  1259. Return Value:
  1260. Revision History:
  1261. Nov-16-95 Davepl Created
  1262. --*/
  1263. HRESULT CProcInfo::SetData(LARGE_INTEGER TotalTime,
  1264. PSYSTEM_PROCESS_INFORMATION pInfo,
  1265. LARGE_INTEGER uPassCount,
  1266. CProcPage * pProcPage,
  1267. BOOL fUpdateOnly)
  1268. {
  1269. HRESULT hr = S_OK;
  1270. DWORD dwTemp;
  1271. HANDLE hProcess;
  1272. // Touch this CProcInfo to indicate the process is still alive
  1273. m_uPassCount.QuadPart = uPassCount.QuadPart;
  1274. // Calc this process's total time as the sum of its user and kernel time
  1275. LARGE_INTEGER TimeDelta;
  1276. LARGE_INTEGER Time;
  1277. if (pInfo->UserTime.QuadPart + pInfo->KernelTime.QuadPart < m_CPUTime.QuadPart)
  1278. {
  1279. // ASSERT(0 && "Proc's cpu total usage went DOWN since last refresh. - Davepl x69731, 425-836-1939 (res)");
  1280. Invalidate();
  1281. return hr = E_FAIL;
  1282. }
  1283. Time.QuadPart = pInfo->UserTime.QuadPart +
  1284. pInfo->KernelTime.QuadPart;
  1285. TimeDelta.QuadPart = Time.QuadPart - m_CPUTime.QuadPart;
  1286. if (TimeDelta.QuadPart)
  1287. {
  1288. m_CPUTime.QuadPart = m_DisplayCPUTime.QuadPart = Time.QuadPart;
  1289. m_fDirty_COL_CPUTIME = TRUE;
  1290. }
  1291. SetCPU( TimeDelta, TotalTime, FALSE );
  1292. //
  1293. // For each of the fields, we check to see if anything has changed, and if
  1294. // so, we mark that particular column as having changed, and update the value.
  1295. // This allows me to opimize which fields of the listview to repaint, since
  1296. // repainting an entire listview column causes flicker and looks bad in
  1297. // general
  1298. //
  1299. // Miscellaneous fields
  1300. if (m_UniqueProcessId != PtrToUlong(pInfo->UniqueProcessId))
  1301. {
  1302. m_fDirty_COL_PID = TRUE;
  1303. m_UniqueProcessId = PtrToUlong(pInfo->UniqueProcessId);
  1304. }
  1305. if( m_SessionId != pInfo->SessionId )
  1306. {
  1307. m_fDirty_COL_SESSIONID = TRUE;
  1308. m_SessionId = pInfo->SessionId;
  1309. }
  1310. if (m_MemDiff != ((SSIZE_T)pInfo->WorkingSetSize / 1024) - (SSIZE_T)m_MemUsage )
  1311. {
  1312. m_fDirty_COL_MEMUSAGEDIFF = TRUE;
  1313. m_MemDiff = ((SSIZE_T)pInfo->WorkingSetSize / 1024) - (SSIZE_T)m_MemUsage;
  1314. }
  1315. if (m_MemPeak != (pInfo->PeakWorkingSetSize / 1024))
  1316. {
  1317. m_fDirty_COL_MEMPEAK = TRUE;
  1318. m_MemPeak = (pInfo->PeakWorkingSetSize / 1024);
  1319. }
  1320. if (m_MemUsage != pInfo->WorkingSetSize / 1024)
  1321. {
  1322. m_fDirty_COL_MEMUSAGE = TRUE;
  1323. m_MemUsage = (pInfo->WorkingSetSize / 1024);
  1324. }
  1325. if (m_PageFaultsDiff != ((LONG)(pInfo->PageFaultCount) - (LONG)m_PageFaults))
  1326. {
  1327. m_fDirty_COL_PAGEFAULTSDIFF = TRUE;
  1328. m_PageFaultsDiff = ((LONG)(pInfo->PageFaultCount) - (LONG)m_PageFaults);
  1329. }
  1330. if (m_PageFaults != (pInfo->PageFaultCount))
  1331. {
  1332. m_fDirty_COL_PAGEFAULTS = TRUE;
  1333. m_PageFaults = (pInfo->PageFaultCount);
  1334. }
  1335. if (m_CommitCharge != pInfo->PrivatePageCount / 1024)
  1336. {
  1337. m_fDirty_COL_COMMITCHARGE = TRUE;
  1338. m_CommitCharge = pInfo->PrivatePageCount / 1024;
  1339. }
  1340. if (m_PagedPool != pInfo->QuotaPagedPoolUsage / 1024)
  1341. {
  1342. m_fDirty_COL_PAGEDPOOL = TRUE;
  1343. m_PagedPool = pInfo->QuotaPagedPoolUsage / 1024;
  1344. }
  1345. if (m_NonPagedPool != pInfo->QuotaNonPagedPoolUsage / 1024)
  1346. {
  1347. m_fDirty_COL_NONPAGEDPOOL = TRUE;
  1348. m_NonPagedPool = pInfo->QuotaNonPagedPoolUsage / 1024;
  1349. }
  1350. if (m_PriClass != pInfo->BasePriority)
  1351. {
  1352. m_fDirty_COL_BASEPRIORITY = TRUE;
  1353. m_PriClass = pInfo->BasePriority;
  1354. }
  1355. if (m_HandleCount != pInfo->HandleCount)
  1356. {
  1357. m_fDirty_COL_HANDLECOUNT = TRUE;
  1358. m_HandleCount = pInfo->HandleCount;
  1359. }
  1360. if (m_ThreadCount != pInfo->NumberOfThreads)
  1361. {
  1362. m_fDirty_COL_HANDLECOUNT = TRUE;
  1363. m_ThreadCount = pInfo->NumberOfThreads;
  1364. }
  1365. if (m_IoReadOperCount != pInfo->ReadOperationCount.QuadPart)
  1366. {
  1367. m_fDirty_COL_READOPERCOUNT = TRUE;
  1368. m_IoReadOperCount = pInfo->ReadOperationCount.QuadPart;
  1369. }
  1370. if (m_IoWriteOperCount != pInfo->WriteOperationCount.QuadPart)
  1371. {
  1372. m_fDirty_COL_WRITEOPERCOUNT = TRUE;
  1373. m_IoWriteOperCount = pInfo->WriteOperationCount.QuadPart;
  1374. }
  1375. if (m_IoOtherOperCount != pInfo->OtherOperationCount.QuadPart)
  1376. {
  1377. m_fDirty_COL_OTHEROPERCOUNT = TRUE;
  1378. m_IoOtherOperCount = pInfo->OtherOperationCount.QuadPart;
  1379. }
  1380. if (m_IoReadXferCount != pInfo->ReadTransferCount.QuadPart)
  1381. {
  1382. m_fDirty_COL_READXFERCOUNT = TRUE;
  1383. m_IoReadXferCount = pInfo->ReadTransferCount.QuadPart;
  1384. }
  1385. if (m_IoWriteXferCount != pInfo->WriteTransferCount.QuadPart)
  1386. {
  1387. m_fDirty_COL_WRITEXFERCOUNT = TRUE;
  1388. m_IoWriteXferCount = pInfo->WriteTransferCount.QuadPart;
  1389. }
  1390. if (m_IoOtherXferCount != pInfo->OtherTransferCount.QuadPart)
  1391. {
  1392. m_fDirty_COL_OTHERXFERCOUNT = TRUE;
  1393. m_IoOtherXferCount = pInfo->OtherTransferCount.QuadPart;
  1394. }
  1395. hProcess = OpenProcess( PROCESS_QUERY_INFORMATION , FALSE, m_UniqueProcessId);
  1396. if ( NULL != hProcess )
  1397. {
  1398. dwTemp = GetGuiResources(hProcess, GR_USEROBJECTS);
  1399. if ( m_USERObjectCount != dwTemp )
  1400. {
  1401. m_fDirty_COL_USEROBJECTS = TRUE;
  1402. m_USERObjectCount = dwTemp;
  1403. }
  1404. dwTemp = GetGuiResources(hProcess, GR_GDIOBJECTS);
  1405. if ( m_GDIObjectCount != dwTemp )
  1406. {
  1407. m_fDirty_COL_GDIOBJECTS = TRUE;
  1408. m_GDIObjectCount = dwTemp;
  1409. }
  1410. CloseHandle(hProcess);
  1411. }
  1412. if (FALSE == fUpdateOnly)
  1413. {
  1414. //
  1415. // Set the process' image name. If its NULL it could be the "Idle Process" or simply
  1416. // a process whose image name is unknown. In both cases we load a string resource
  1417. // with an appropriate replacement name.
  1418. //
  1419. m_fDirty_COL_IMAGENAME = TRUE;
  1420. if (pInfo->ImageName.Buffer == NULL)
  1421. {
  1422. // No image name, so replace it with "Unknown"
  1423. WCHAR szTmp[MAX_PATH];
  1424. szTmp[0] = TEXT('\0');
  1425. UINT cchLen = LoadString(g_hInstance, IDS_SYSPROC, szTmp, MAX_PATH);
  1426. cchLen ++; // add one for NULL char.
  1427. m_pszImageName = (LPWSTR) LocalAlloc( LPTR, sizeof(*m_pszImageName) * cchLen );
  1428. if (NULL == m_pszImageName)
  1429. {
  1430. return hr = E_OUTOFMEMORY;
  1431. }
  1432. StringCchCopy( m_pszImageName, cchLen, szTmp); // should never be truncated
  1433. }
  1434. else
  1435. {
  1436. //
  1437. // We have a valid image name, so allocate enough space and then
  1438. // make a copy of it
  1439. //
  1440. DWORD cchLen = pInfo->ImageName.Length / sizeof(WCHAR) + 1;
  1441. m_pszImageName = (LPWSTR) LocalAlloc( LPTR, sizeof(*m_pszImageName) * cchLen );
  1442. if (NULL == m_pszImageName)
  1443. {
  1444. return hr = E_OUTOFMEMORY;
  1445. }
  1446. StringCchCopy( m_pszImageName, cchLen, pInfo->ImageName.Buffer ); // should never be truncated
  1447. }
  1448. if( g_fIsTSEnabled )
  1449. {
  1450. SetProcessUsername(LPFILETIME(&(pInfo->CreateTime)));
  1451. }
  1452. }
  1453. //
  1454. // Check if this process is a WOW process. There is some latency
  1455. // between the time a WOW process is created and the time
  1456. // the shared memory used by VDMEnumTaskWOWEx reflects the new
  1457. // process and tasks. However, once a process becomes a WOW
  1458. // process, it is always a WOW process until it dies.
  1459. //
  1460. if (g_Options.m_fShow16Bit)
  1461. {
  1462. if ( m_fWowProcess ||
  1463. ! m_fWowProcessTested)
  1464. {
  1465. #if !defined (_WIN64)
  1466. if ( ( m_pszImageName != NULL ) && ( ! _wcsicmp(m_pszImageName, TEXT("ntvdm.exe")) ) )
  1467. {
  1468. WOWTASKCALLBACKPARMS WowTaskCallbackParms;
  1469. WowTaskCallbackParms.uPassCount = uPassCount;
  1470. WowTaskCallbackParms.pProcPage = pProcPage;
  1471. WowTaskCallbackParms.pParentProcInfo = this;
  1472. WowTaskCallbackParms.TotalTime.QuadPart = TotalTime.QuadPart;
  1473. WowTaskCallbackParms.TimeLeft.QuadPart = TimeDelta.QuadPart;
  1474. if (VDMEnumTaskWOWEx(m_UniqueProcessId,
  1475. WowTaskCallback,
  1476. (LPARAM) &WowTaskCallbackParms))
  1477. {
  1478. if ( ! m_fWowProcess )
  1479. {
  1480. m_fWowProcessTested =
  1481. m_fWowProcess = TRUE;
  1482. }
  1483. SetCPU( WowTaskCallbackParms.TimeLeft, TotalTime, TRUE );
  1484. }
  1485. else
  1486. {
  1487. //
  1488. // We avoid calling VDMEnumTaskWOWEx if the process has an
  1489. // execution time of more than 10 seconds and has not so
  1490. // far been seen as a WOW process.
  1491. //
  1492. if (GetCPUTime() > (10 * 10 * 1000 * 1000))
  1493. {
  1494. m_fWowProcessTested = TRUE;
  1495. }
  1496. }
  1497. }
  1498. else
  1499. {
  1500. m_fWowProcessTested = TRUE;
  1501. }
  1502. #else
  1503. pProcPage; // unreferenced
  1504. m_fWowProcessTested = TRUE;
  1505. #endif
  1506. }
  1507. }
  1508. return S_OK;
  1509. }
  1510. //----------------------------------------------------------------
  1511. //
  1512. // No creation info
  1513. //
  1514. // Reviewed by alhen 9 - 3 - 98
  1515. //
  1516. HRESULT CProcInfo::SetProcessUsername(const FILETIME *pCreateTime)
  1517. {
  1518. DWORD dwError = NO_ERROR;
  1519. // in case of wow tasks assign username same as its parent process's
  1520. if( IsWowTask( ) )
  1521. {
  1522. if( m_pWowParentProcInfo->m_pszUserName == NULL )
  1523. {
  1524. return E_FAIL;
  1525. }
  1526. DWORD cchLen = lstrlen( m_pWowParentProcInfo->m_pszUserName ) + 1;
  1527. m_pszUserName = (LPWSTR) LocalAlloc( LPTR, sizeof(*m_pszUserName) * cchLen );
  1528. if( NULL == m_pszUserName )
  1529. {
  1530. return E_OUTOFMEMORY;
  1531. }
  1532. StringCchCopy( m_pszUserName, cchLen, m_pWowParentProcInfo->m_pszUserName ); // should never truncate
  1533. return S_OK;
  1534. }
  1535. if( m_UniqueProcessId == 0 ) // this is a system idle process.
  1536. {
  1537. const WCHAR szIdleProcessOwner[] = L"SYSTEM";
  1538. m_pszUserName = (LPWSTR) LocalAlloc( LPTR, sizeof(szIdleProcessOwner) );
  1539. if( NULL == m_pszUserName )
  1540. {
  1541. return E_OUTOFMEMORY;
  1542. }
  1543. StringCbCopy( m_pszUserName, sizeof(szIdleProcessOwner), szIdleProcessOwner ); // should never truncate
  1544. }
  1545. else
  1546. {
  1547. PSID pUserSid = NULL;
  1548. DWORD dwSize = 0;
  1549. if( !WinStationGetProcessSid( NULL , GetRealPID( ) , *pCreateTime, ( PBYTE )pUserSid , &dwSize ) )
  1550. {
  1551. pUserSid = (PSID) LocalAlloc( LPTR, dwSize );
  1552. if( pUserSid != NULL )
  1553. {
  1554. if( WinStationGetProcessSid( NULL , GetRealPID( ) , *pCreateTime, ( PBYTE )pUserSid , &dwSize ) )
  1555. {
  1556. if( IsValidSid( pUserSid ) )
  1557. {
  1558. WCHAR szTmpName[MAX_PATH];
  1559. DWORD dwTmpNameSize = MAX_PATH;
  1560. CachedGetUserFromSid( pUserSid , szTmpName , &dwTmpNameSize );
  1561. m_pszUserName = (LPWSTR) LocalAlloc( LPTR, sizeof(*m_pszUserName) * ( dwTmpNameSize + 1 ) );
  1562. if( m_pszUserName != NULL )
  1563. {
  1564. StringCchCopy( m_pszUserName, dwTmpNameSize + 1, szTmpName); // don't care if it truncates - used in UI only
  1565. }
  1566. }
  1567. }
  1568. LocalFree( pUserSid );
  1569. }
  1570. else
  1571. {
  1572. dwError = GetLastError();
  1573. }
  1574. } // this would mean that a sid of size zero was returned
  1575. }
  1576. return HRESULT_FROM_WIN32(dwError);
  1577. }
  1578. /*++ CProcPage::UpdateProcListview
  1579. Class Description:
  1580. Walks the listview and checks to see if each line in the
  1581. listview matches the corresponding entry in our process
  1582. array. Those which differe by PID are replaced, and those
  1583. that need updating are updated.
  1584. Items are also added and removed to/from the tail of the
  1585. listview as required.
  1586. Arguments:
  1587. Return Value:
  1588. HRESULT
  1589. Revision History:
  1590. Nov-20-95 Davepl Created
  1591. --*/
  1592. HRESULT CProcPage::UpdateProcListview ()
  1593. {
  1594. HWND hListView = GetDlgItem(m_hPage, IDC_PROCLIST);
  1595. //
  1596. // Stop repaints while we party on the listview
  1597. //
  1598. SendMessage(hListView, WM_SETREDRAW, FALSE, 0);
  1599. INT cListViewItems = ListView_GetItemCount(hListView);
  1600. INT cProcArrayItems = m_pProcArray->GetSize();
  1601. //
  1602. // Walk the existing lines in the listview and replace/update
  1603. // them as needed
  1604. //
  1605. CProcInfo * pSelected = GetSelectedProcess();
  1606. for (int iCurrent = 0, iCurrListViewItem = 0;
  1607. iCurrListViewItem < cListViewItems && iCurrent < cProcArrayItems;
  1608. iCurrent++) // for each process
  1609. {
  1610. CProcInfo * pProc = (CProcInfo *) m_pProcArray->GetAt(iCurrent);
  1611. //get only processes we need to show
  1612. if(g_fIsTSEnabled && !g_Options.m_bShowAllProcess && !pProc->OkToShowThisProcess() ) {
  1613. continue;
  1614. }
  1615. LV_ITEM lvitem = { 0 };
  1616. lvitem.mask = LVIF_PARAM | LVIF_TEXT | LVIF_STATE;
  1617. lvitem.iItem = iCurrListViewItem;
  1618. if (FALSE == ListView_GetItem(hListView, &lvitem))
  1619. {
  1620. SendMessage(hListView, WM_SETREDRAW, TRUE, 0);
  1621. return E_FAIL;
  1622. }
  1623. CProcInfo * pTmp = (CProcInfo *) lvitem.lParam;
  1624. if (pTmp != pProc)
  1625. {
  1626. // If the objects aren't the same, we need to replace this line
  1627. lvitem.pszText = pProc->m_pszImageName;
  1628. lvitem.lParam = (LPARAM) pProc;
  1629. if (pProc == pSelected)
  1630. {
  1631. lvitem.state |= LVIS_SELECTED | LVIS_FOCUSED;
  1632. }
  1633. else
  1634. {
  1635. lvitem.state &= ~(LVIS_SELECTED | LVIS_FOCUSED);
  1636. }
  1637. lvitem.stateMask |= LVIS_SELECTED | LVIS_FOCUSED;
  1638. ListView_SetItem(hListView, &lvitem);
  1639. ListView_RedrawItems(hListView, iCurrListViewItem, iCurrListViewItem);
  1640. }
  1641. else if (pProc->m_fDirty)
  1642. {
  1643. // Same PID, but item needs updating
  1644. ListView_RedrawItems(hListView, iCurrListViewItem, iCurrListViewItem);
  1645. pProc->m_fDirty = 0;
  1646. }
  1647. iCurrListViewItem++;
  1648. }
  1649. //
  1650. // We've either run out of listview items or run out of proc array
  1651. // entries, so remove/add to the listview as appropriate
  1652. //
  1653. while (iCurrListViewItem < cListViewItems)
  1654. {
  1655. // Extra items in the listview (processes gone away), so remove them
  1656. ListView_DeleteItem(hListView, iCurrListViewItem);
  1657. cListViewItems--;
  1658. }
  1659. while (iCurrent < cProcArrayItems)
  1660. {
  1661. // Need to add new items to the listview (new processes appeared)
  1662. CProcInfo * pProc = (CProcInfo *)m_pProcArray->GetAt(iCurrent++);
  1663. //get only processes we need to show
  1664. if(g_fIsTSEnabled && !g_Options.m_bShowAllProcess && !pProc->OkToShowThisProcess() ) {
  1665. continue;
  1666. }
  1667. LV_ITEM lvitem = { 0 };
  1668. lvitem.mask = LVIF_PARAM | LVIF_TEXT;
  1669. lvitem.iItem = iCurrListViewItem;
  1670. lvitem.pszText = pProc->m_pszImageName;
  1671. lvitem.lParam = (LPARAM) pProc;
  1672. // The first item added (actually, every 0 to 1 count transition) gets
  1673. // selected and focused
  1674. if (iCurrListViewItem == 0)
  1675. {
  1676. lvitem.state = LVIS_SELECTED | LVIS_FOCUSED;
  1677. lvitem.stateMask = lvitem.state;
  1678. lvitem.mask |= LVIF_STATE;
  1679. }
  1680. ListView_InsertItem(hListView, &lvitem);
  1681. iCurrListViewItem++;
  1682. }
  1683. ASSERT(iCurrListViewItem == ListView_GetItemCount(hListView));
  1684. ASSERT(iCurrent == cProcArrayItems);
  1685. // Let the listview paint again
  1686. SendMessage(hListView, WM_SETREDRAW, TRUE, 0);
  1687. return S_OK;
  1688. }
  1689. /*++ class CProcPage::UpdateProcInfoArray
  1690. Class Description:
  1691. Retrieves the list of process info blocks from the system,
  1692. and runs through our array of CProcInfo items. Items which
  1693. already exist are updated, and those that do not are added.
  1694. At the end, any process which has not been touched by this
  1695. itteration of the function are considered to have completed
  1696. and are removed from the array.
  1697. Arguments:
  1698. Return Value:
  1699. Revision History:
  1700. Nov-16-95 Davepl Created
  1701. --*/
  1702. // See comments near the usage of this table below for info on why it exists
  1703. static struct
  1704. {
  1705. size_t cbOffset;
  1706. UINT idString;
  1707. }
  1708. g_OffsetMap[] =
  1709. {
  1710. { FIELD_OFFSET(CSysInfo, m_cHandles), IDC_TOTAL_HANDLES },
  1711. { FIELD_OFFSET(CSysInfo, m_cThreads), IDC_TOTAL_THREADS },
  1712. { FIELD_OFFSET(CSysInfo, m_cProcesses), IDC_TOTAL_PROCESSES },
  1713. { FIELD_OFFSET(CSysInfo, m_dwPhysicalMemory), IDC_TOTAL_PHYSICAL },
  1714. { FIELD_OFFSET(CSysInfo, m_dwPhysAvail), IDC_AVAIL_PHYSICAL },
  1715. { FIELD_OFFSET(CSysInfo, m_dwFileCache), IDC_FILE_CACHE },
  1716. { FIELD_OFFSET(CSysInfo, m_dwCommitTotal), IDC_COMMIT_TOTAL },
  1717. { FIELD_OFFSET(CSysInfo, m_dwCommitLimit), IDC_COMMIT_LIMIT },
  1718. { FIELD_OFFSET(CSysInfo, m_dwCommitPeak), IDC_COMMIT_PEAK },
  1719. { FIELD_OFFSET(CSysInfo, m_dwKernelPaged), IDC_KERNEL_PAGED },
  1720. { FIELD_OFFSET(CSysInfo, m_dwKernelNP), IDC_KERNEL_NONPAGED },
  1721. { FIELD_OFFSET(CSysInfo, m_dwKernelTotal), IDC_KERNEL_TOTAL },
  1722. };
  1723. //
  1724. //
  1725. //
  1726. HRESULT CProcPage::UpdateProcInfoArray()
  1727. {
  1728. HRESULT hr;
  1729. INT i;
  1730. INT iField;
  1731. ULONG cbOffset = 0;
  1732. CSysInfo SysInfoTemp;
  1733. NTSTATUS Status;
  1734. SYSTEM_BASIC_INFORMATION BasicInfo;
  1735. PSYSTEM_PROCESS_INFORMATION pCurrent;
  1736. SYSTEM_PERFORMANCE_INFORMATION PerfInfo;
  1737. SYSTEM_FILECACHE_INFORMATION FileCache;
  1738. LARGE_INTEGER TotalTime = {0,0};
  1739. LARGE_INTEGER LastTotalTime = {0,0};
  1740. //
  1741. // Pass-count for this function. It ain't thread-safe, of course, but I
  1742. // can't imagine a scenario where we'll have mode than one thread running
  1743. // through this (the app is currently single threaded anyway). If we
  1744. // overflow LARGE_INTEGER updates, I'll already be long gone, so don't bug me.
  1745. //
  1746. static LARGE_INTEGER uPassCount = {0,0};
  1747. //
  1748. // Get some non-process specific info, like memory status
  1749. //
  1750. Status = NtQuerySystemInformation(
  1751. SystemBasicInformation,
  1752. &BasicInfo,
  1753. sizeof(BasicInfo),
  1754. NULL
  1755. );
  1756. if (!NT_SUCCESS(Status))
  1757. {
  1758. return E_FAIL;
  1759. }
  1760. SysInfoTemp.m_dwPhysicalMemory = (ULONG)(BasicInfo.NumberOfPhysicalPages *
  1761. (BasicInfo.PageSize / 1024));
  1762. Status = NtQuerySystemInformation(
  1763. SystemPerformanceInformation,
  1764. &PerfInfo,
  1765. sizeof(PerfInfo),
  1766. NULL
  1767. );
  1768. if (!NT_SUCCESS(Status))
  1769. {
  1770. return E_FAIL;
  1771. }
  1772. SysInfoTemp.m_dwPhysAvail = PerfInfo.AvailablePages * (g_BasicInfo.PageSize / 1024);
  1773. SysInfoTemp.m_dwCommitTotal = (DWORD)(PerfInfo.CommittedPages * (g_BasicInfo.PageSize / 1024));
  1774. SysInfoTemp.m_dwCommitLimit = (DWORD)(PerfInfo.CommitLimit * (g_BasicInfo.PageSize / 1024));
  1775. SysInfoTemp.m_dwCommitPeak = (DWORD)(PerfInfo.PeakCommitment * (g_BasicInfo.PageSize / 1024));
  1776. SysInfoTemp.m_dwKernelPaged = PerfInfo.PagedPoolPages * (g_BasicInfo.PageSize / 1024);
  1777. SysInfoTemp.m_dwKernelNP = PerfInfo.NonPagedPoolPages * (g_BasicInfo.PageSize / 1024);
  1778. SysInfoTemp.m_dwKernelTotal = SysInfoTemp.m_dwKernelNP + SysInfoTemp.m_dwKernelPaged;
  1779. g_MEMMax = SysInfoTemp.m_dwCommitLimit;
  1780. Status = NtQuerySystemInformation(
  1781. SystemFileCacheInformation,
  1782. &FileCache,
  1783. sizeof(FileCache),
  1784. NULL
  1785. );
  1786. if (!NT_SUCCESS(Status))
  1787. {
  1788. return E_FAIL;
  1789. }
  1790. //
  1791. // The DWORD cast below must be fixed as this value can be greater than
  1792. // 32 bits.
  1793. //
  1794. SysInfoTemp.m_dwFileCache = (DWORD)(FileCache.CurrentSizeIncludingTransitionInPages * (g_BasicInfo.PageSize / 1024));
  1795. //
  1796. // Read the process info structures into the flat buffer
  1797. //
  1798. hr = GetProcessInfo();
  1799. if (FAILED(hr))
  1800. {
  1801. goto done;
  1802. }
  1803. //
  1804. // First walk all of the process info blocks and sum their times, so that we can
  1805. // calculate a CPU usage ratio (%) for each individual process
  1806. //
  1807. cbOffset = 0;
  1808. do
  1809. {
  1810. CProcInfo * pOldProcInfo;
  1811. pCurrent = (PSYSTEM_PROCESS_INFORMATION)&((LPBYTE)m_pvBuffer)[cbOffset];
  1812. if (pCurrent->UniqueProcessId == NULL && pCurrent->NumberOfThreads == 0)
  1813. {
  1814. // Zombie process, just skip it
  1815. goto next;
  1816. }
  1817. pOldProcInfo = FindProcInArrayByPID(m_pProcArray, PtrToUlong(pCurrent->UniqueProcessId));
  1818. if (pOldProcInfo)
  1819. {
  1820. if (pOldProcInfo->GetCPUTime() > pCurrent->KernelTime.QuadPart + pCurrent->UserTime.QuadPart)
  1821. {
  1822. // If CPU has gone DOWN, its because the PID has been reused, so invalidate this
  1823. // CProcInfo such that it is removed and the new one added
  1824. pOldProcInfo->Invalidate();
  1825. goto next;
  1826. }
  1827. else if (pCurrent->UniqueProcessId == 0 &&
  1828. pCurrent->KernelTime.QuadPart == 0 &&
  1829. pCurrent->UserTime.QuadPart == 0)
  1830. {
  1831. pOldProcInfo->Invalidate();
  1832. goto next;
  1833. }
  1834. else
  1835. {
  1836. LastTotalTime.QuadPart += pOldProcInfo->GetCPUTime();
  1837. }
  1838. }
  1839. TotalTime.QuadPart += pCurrent->KernelTime.QuadPart + pCurrent->UserTime.QuadPart;
  1840. SysInfoTemp.m_cHandles += pCurrent->HandleCount;
  1841. SysInfoTemp.m_cThreads += pCurrent->NumberOfThreads;
  1842. SysInfoTemp.m_cProcesses++;
  1843. next:
  1844. cbOffset += pCurrent->NextEntryOffset;
  1845. // if current session id is not set yet, set it now
  1846. //
  1847. // REVIEWER: Previous dev didnot document this, but taskmgr session id
  1848. // is cached so that when the user deselects "show all the processes", only
  1849. // processes with session id's equal to taskmgr session id are listed
  1850. // --alhen
  1851. if( ( GetCurrentSessionID() == -1 ) && ( PtrToUlong(pCurrent->UniqueProcessId) == GetCurrentProcessId( ) ) )
  1852. {
  1853. SetCurrentSessionID( ( DWORD )pCurrent->SessionId );
  1854. }
  1855. } while (pCurrent->NextEntryOffset);
  1856. LARGE_INTEGER TimeDelta;
  1857. TimeDelta.QuadPart = TotalTime.QuadPart - LastTotalTime.QuadPart;
  1858. ASSERT(TimeDelta.QuadPart >= 0);
  1859. // Update the global count (visible to the status bar)
  1860. g_cProcesses = SysInfoTemp.m_cProcesses;
  1861. //
  1862. // We have a number of text fields in the dialog that are based on counts we accumulate
  1863. // here. Rather than painting all of the time, we only change the ones whose values have
  1864. // really changed. We have a table up above of the offsets into the CSysInfo object
  1865. // where these values live (the same offset in the real g_SysInfo object and the temp
  1866. // working copy, of course), and what control ID they correspond to. We then loop through
  1867. // and compare each real one to the temp working copy, updating as needed. Hard to
  1868. // read, but smaller than a dozen if() statements.
  1869. //
  1870. extern CPage * g_pPages[];
  1871. if (g_pPages[PERF_PAGE])
  1872. {
  1873. for (iField = 0; iField < ARRAYSIZE(g_OffsetMap); iField++)
  1874. {
  1875. DWORD * pdwRealCopy = (DWORD *)(((LPBYTE)&m_SysInfo) + g_OffsetMap[iField].cbOffset);
  1876. DWORD * pdwTempCopy = (DWORD *)(((LPBYTE)&SysInfoTemp) + g_OffsetMap[iField].cbOffset);
  1877. *pdwRealCopy = *pdwTempCopy;
  1878. WCHAR szText[32];
  1879. StringCchPrintf( szText, ARRAYSIZE(szText), L"%d", *pdwRealCopy); // don't care if it truncates - UI only
  1880. HWND hPage = g_pPages[PERF_PAGE]->GetPageWindow();
  1881. // Updates can come through before page is created, so verify
  1882. // that it exists before we party on its children
  1883. if (hPage)
  1884. {
  1885. SetWindowText(GetDlgItem(hPage, g_OffsetMap[iField].idString), szText);
  1886. }
  1887. }
  1888. }
  1889. //
  1890. // Now walk the process info blocks again and refresh the CProcInfo array for each
  1891. // individual process
  1892. //
  1893. cbOffset = 0;
  1894. do
  1895. {
  1896. //
  1897. // Grab a PROCESS_INFORMATION struct from the buffer
  1898. //
  1899. pCurrent = (PSYSTEM_PROCESS_INFORMATION)&((LPBYTE)m_pvBuffer)[cbOffset];
  1900. if (pCurrent->UniqueProcessId == NULL && pCurrent->NumberOfThreads == 0)
  1901. {
  1902. // Zombie process, just skip it
  1903. goto nextprocinfo;
  1904. }
  1905. //
  1906. // This is really ugly, but... NtQuerySystemInfo has too much latency, and if you
  1907. // change a process' priority, you don't see it reflected right away. And, if you
  1908. // don't have autoupdate on, you never do. So, we use GetPriorityClass() to get
  1909. // the value instead. This means BasePriority is now the pri class, not the pri value.
  1910. //
  1911. if (pCurrent->UniqueProcessId)
  1912. {
  1913. HANDLE hProcess;
  1914. hProcess = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, PtrToUlong(pCurrent->UniqueProcessId) );
  1915. DWORD dwPriClass;
  1916. dwPriClass = 0;
  1917. if (hProcess)
  1918. {
  1919. dwPriClass = GetPriorityClass(hProcess);
  1920. if (dwPriClass)
  1921. {
  1922. pCurrent->BasePriority = dwPriClass;
  1923. }
  1924. CloseHandle( hProcess );
  1925. }
  1926. if (NULL == hProcess || dwPriClass == 0)
  1927. {
  1928. // We're not allowed to open this process, so convert what NtQuerySystemInfo
  1929. // gave us into a priority class... its the next best thing
  1930. if (pCurrent->BasePriority <= 4)
  1931. {
  1932. pCurrent->BasePriority = IDLE_PRIORITY_CLASS;
  1933. }
  1934. else if (pCurrent->BasePriority <= 6)
  1935. {
  1936. pCurrent->BasePriority = BELOW_NORMAL_PRIORITY_CLASS;
  1937. }
  1938. else if (pCurrent->BasePriority <= 8)
  1939. {
  1940. pCurrent->BasePriority = NORMAL_PRIORITY_CLASS;
  1941. }
  1942. else if (pCurrent->BasePriority <= 10)
  1943. {
  1944. pCurrent->BasePriority = ABOVE_NORMAL_PRIORITY_CLASS;
  1945. }
  1946. else if (pCurrent->BasePriority <= 13)
  1947. {
  1948. pCurrent->BasePriority = HIGH_PRIORITY_CLASS;
  1949. }
  1950. else
  1951. {
  1952. pCurrent->BasePriority = REALTIME_PRIORITY_CLASS;
  1953. }
  1954. }
  1955. }
  1956. //
  1957. // Try to find an existing CProcInfo instance which corresponds to this process
  1958. //
  1959. CProcInfo * pProcInfo;
  1960. pProcInfo = FindProcInArrayByPID(m_pProcArray, PtrToUlong(pCurrent->UniqueProcessId));
  1961. if (NULL == pProcInfo)
  1962. {
  1963. //
  1964. // We don't already have this process in our array, so create a new one
  1965. // and add it to the array
  1966. //
  1967. pProcInfo = new CProcInfo;
  1968. if (NULL == pProcInfo)
  1969. {
  1970. hr = E_OUTOFMEMORY;
  1971. goto done;
  1972. }
  1973. hr = pProcInfo->SetData(TimeDelta,
  1974. pCurrent,
  1975. uPassCount,
  1976. this,
  1977. FALSE);
  1978. if (FAILED(hr) || FALSE == m_pProcArray->Add(pProcInfo))
  1979. {
  1980. delete pProcInfo;
  1981. goto done;
  1982. }
  1983. }
  1984. else
  1985. {
  1986. //
  1987. // This process already existed in our array, so update its info
  1988. //
  1989. hr = pProcInfo->SetData(TimeDelta,
  1990. pCurrent,
  1991. uPassCount,
  1992. this,
  1993. TRUE);
  1994. if (FAILED(hr))
  1995. {
  1996. goto done;
  1997. }
  1998. }
  1999. nextprocinfo:
  2000. cbOffset += pCurrent->NextEntryOffset;
  2001. } while (pCurrent->NextEntryOffset);
  2002. //
  2003. // Run through the CProcInfo array and remove anyone that hasn't been touched
  2004. // by this pass through this function (which indicates the process is no
  2005. // longer alive)
  2006. //
  2007. i = 0;
  2008. while (i < m_pProcArray->GetSize())
  2009. {
  2010. CProcInfo * pProcInfo = (CProcInfo *)(m_pProcArray->GetAt(i));
  2011. ASSERT(pProcInfo);
  2012. //
  2013. // If passcount doesn't match, delete the CProcInfo instance and remove
  2014. // its pointer from the array. Note that we _don't_ increment the index
  2015. // if we remove an element, since the next element would now live at
  2016. // the current index after the deletion
  2017. //
  2018. if (pProcInfo->m_uPassCount.QuadPart != uPassCount.QuadPart)
  2019. {
  2020. delete pProcInfo;
  2021. m_pProcArray->RemoveAt(i, 1);
  2022. }
  2023. else
  2024. {
  2025. i++;
  2026. }
  2027. }
  2028. done:
  2029. ResortArray(&m_pProcArray);
  2030. uPassCount.QuadPart++;
  2031. return hr;
  2032. }
  2033. /*++ CPerfPage::SizeProcPage
  2034. Routine Description:
  2035. Sizes its children based on the size of the
  2036. tab control on which it appears.
  2037. Arguments:
  2038. Return Value:
  2039. Revision History:
  2040. Nov-16-95 Davepl Created
  2041. --*/
  2042. void CProcPage::SizeProcPage()
  2043. {
  2044. // Get the coords of the outer dialog
  2045. RECT rcParent;
  2046. GetClientRect(m_hPage, &rcParent);
  2047. HDWP hdwp = BeginDeferWindowPos(3);
  2048. if (!hdwp)
  2049. return;
  2050. // Calc the deltas in the x and y positions that we need to
  2051. // move each of the child controls
  2052. RECT rcTerminate;
  2053. HWND hwndTerminate = GetDlgItem(m_hPage, IDC_TERMINATE);
  2054. GetWindowRect(hwndTerminate, &rcTerminate);
  2055. MapWindowPoints(HWND_DESKTOP, m_hPage, (LPPOINT) &rcTerminate, 2);
  2056. INT dx = ((rcParent.right - g_DefSpacing * 2) - rcTerminate.right);
  2057. INT dy = ((rcParent.bottom - g_DefSpacing * 2) - rcTerminate.bottom);
  2058. // Move the EndProcess button
  2059. DeferWindowPos(hdwp, hwndTerminate, NULL,
  2060. rcTerminate.left + dx,
  2061. rcTerminate.top + dy,
  2062. 0, 0,
  2063. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  2064. HWND hwndShowall = GetDlgItem(m_hPage, IDC_SHOWALL);
  2065. if( IsWindow( hwndShowall ) )
  2066. {
  2067. if( g_fIsTSEnabled )
  2068. {
  2069. RECT rcShowall;
  2070. GetWindowRect(hwndShowall, &rcShowall);
  2071. MapWindowPoints(HWND_DESKTOP, m_hPage, (LPPOINT) &rcShowall, 2);
  2072. DeferWindowPos(hdwp, hwndShowall, NULL, rcShowall.left, rcShowall.top + dy, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  2073. }
  2074. else
  2075. {
  2076. // this window must be hidden.
  2077. ShowWindow(hwndShowall, SW_HIDE);
  2078. }
  2079. }
  2080. //
  2081. // Size the listbox
  2082. //
  2083. HWND hwndListbox = GetDlgItem(m_hPage, IDC_PROCLIST);
  2084. RECT rcListbox;
  2085. GetWindowRect(hwndListbox, &rcListbox);
  2086. MapWindowPoints(HWND_DESKTOP, m_hPage, (LPPOINT) &rcListbox, 2);
  2087. DeferWindowPos(hdwp, hwndListbox, NULL,
  2088. 0, 0,
  2089. rcTerminate.right - rcListbox.left + dx,
  2090. rcTerminate.top - rcListbox.top + dy - g_DefSpacing,
  2091. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  2092. EndDeferWindowPos(hdwp);
  2093. }
  2094. /*++ CProcPage::HandleTaskManNotify
  2095. Routine Description:
  2096. Processes WM_NOTIFY messages received by the procpage dialog
  2097. Arguments:
  2098. hWnd - Control that generated the WM_NOTIFY
  2099. pnmhdr - Ptr to the NMHDR notification stucture
  2100. Return Value:
  2101. BOOL "did we handle it" code
  2102. Revision History:
  2103. Nov-20-95 Davepl Created
  2104. --*/
  2105. INT CProcPage::HandleProcPageNotify(LPNMHDR pnmhdr)
  2106. {
  2107. switch(pnmhdr->code)
  2108. {
  2109. case LVN_COLUMNCLICK:
  2110. {
  2111. // User clicked a header control, so set the sort column. If its the
  2112. // same as the current sort column, just invert the sort direction in
  2113. // the column. Then resort the task array
  2114. const NM_LISTVIEW * pnmv = (const NM_LISTVIEW *) pnmhdr;
  2115. if (g_iProcSortColumnID == g_Options.m_ActiveProcCol[pnmv->iSubItem])
  2116. {
  2117. g_iProcSortDirection *= -1;
  2118. }
  2119. else
  2120. {
  2121. g_iProcSortColumnID = g_Options.m_ActiveProcCol[pnmv->iSubItem];
  2122. g_iProcSortDirection = -1;
  2123. }
  2124. ResortArray(&m_pProcArray);
  2125. TimerEvent();
  2126. }
  2127. break;
  2128. case LVN_ITEMCHANGED:
  2129. {
  2130. const NM_LISTVIEW * pnmv = (const NM_LISTVIEW *) pnmhdr;
  2131. if (pnmv->uChanged & LVIF_STATE)
  2132. {
  2133. UINT cSelected = ListView_GetSelectedCount(GetDlgItem(m_hPage, IDC_PROCLIST));
  2134. EnableWindow(GetDlgItem(m_hPage, IDC_TERMINATE), cSelected ? TRUE : FALSE);
  2135. }
  2136. }
  2137. break;
  2138. case LVN_GETDISPINFO:
  2139. {
  2140. LV_ITEM * plvitem = &(((LV_DISPINFO *) pnmhdr)->item);
  2141. // Listview needs a text string
  2142. if (plvitem->mask & LVIF_TEXT)
  2143. {
  2144. COLUMNID columnid = (COLUMNID) g_Options.m_ActiveProcCol[plvitem->iSubItem];
  2145. const CProcInfo * pProcInfo = (const CProcInfo *) plvitem->lParam;
  2146. //
  2147. // Most columns are blank for WOW tasks.
  2148. //
  2149. if (pProcInfo->IsWowTask() &&
  2150. columnid != COL_IMAGENAME &&
  2151. columnid != COL_BASEPRIORITY &&
  2152. columnid != COL_THREADCOUNT &&
  2153. columnid != COL_CPUTIME &&
  2154. columnid != COL_USERNAME &&
  2155. columnid != COL_SESSIONID &&
  2156. columnid != COL_CPU) {
  2157. plvitem->pszText[0] = L'\0';
  2158. goto done;
  2159. }
  2160. switch(columnid)
  2161. {
  2162. case COL_PID:
  2163. // don't care if it truncates - UI only
  2164. StringCchPrintf(plvitem->pszText, plvitem->cchTextMax, L"%d", (ULONG) pProcInfo->m_UniqueProcessId );
  2165. break;
  2166. case COL_USERNAME:
  2167. if( pProcInfo->m_pszUserName )
  2168. {
  2169. // don't care if it truncates - UI only
  2170. StringCchCopy( plvitem->pszText, plvitem->cchTextMax, pProcInfo->m_pszUserName );
  2171. }
  2172. break;
  2173. case COL_SESSIONID:
  2174. // don't care if it truncates - UI only
  2175. StringCchPrintf( plvitem->pszText, plvitem->cchTextMax, L"%d", pProcInfo->m_SessionId );
  2176. break;
  2177. case COL_CPU:
  2178. // don't care if it truncates - UI only
  2179. StringCchPrintf(plvitem->pszText, plvitem->cchTextMax, L"%02d %", pProcInfo->m_DisplayCPU );
  2180. break;
  2181. case COL_IMAGENAME:
  2182. // don't care if it truncates - UI only
  2183. StringCchCopy(plvitem->pszText, plvitem->cchTextMax, pProcInfo->m_pszImageName );
  2184. break;
  2185. case COL_CPUTIME:
  2186. {
  2187. TIME_FIELDS TimeOut;
  2188. RtlTimeToElapsedTimeFields ( (LARGE_INTEGER *)&(pProcInfo->m_DisplayCPUTime), &TimeOut);
  2189. TimeOut.Hour = static_cast<CSHORT>(TimeOut.Hour + static_cast<SHORT>(TimeOut.Day * 24));
  2190. // don't care if it truncates - UI only
  2191. StringCchPrintf( plvitem->pszText
  2192. , plvitem->cchTextMax
  2193. , L"%2d%s%02d%s%02d"
  2194. , TimeOut.Hour
  2195. , g_szTimeSep
  2196. , TimeOut.Minute
  2197. , g_szTimeSep
  2198. , TimeOut.Second
  2199. );
  2200. break;
  2201. }
  2202. case COL_MEMUSAGE:
  2203. Int64ToCommaSepKString(LONGLONG(pProcInfo->m_MemUsage), plvitem->pszText, plvitem->cchTextMax);
  2204. break;
  2205. case COL_MEMUSAGEDIFF:
  2206. Int64ToCommaSepKString(LONGLONG(pProcInfo->m_MemDiff), plvitem->pszText, plvitem->cchTextMax);
  2207. break;
  2208. case COL_MEMPEAK:
  2209. Int64ToCommaSepKString(LONGLONG(pProcInfo->m_MemPeak), plvitem->pszText, plvitem->cchTextMax);
  2210. break;
  2211. case COL_PAGEFAULTS:
  2212. Int64ToCommaSepString(LONGLONG(pProcInfo->m_PageFaults), plvitem->pszText, plvitem->cchTextMax);
  2213. break;
  2214. case COL_PAGEFAULTSDIFF:
  2215. Int64ToCommaSepString(LONGLONG(pProcInfo->m_PageFaultsDiff), plvitem->pszText, plvitem->cchTextMax);
  2216. break;
  2217. case COL_COMMITCHARGE:
  2218. Int64ToCommaSepKString(LONGLONG(pProcInfo->m_CommitCharge), plvitem->pszText, plvitem->cchTextMax);
  2219. break;
  2220. case COL_PAGEDPOOL:
  2221. Int64ToCommaSepKString(LONGLONG(pProcInfo->m_PagedPool), plvitem->pszText, plvitem->cchTextMax);
  2222. break;
  2223. case COL_NONPAGEDPOOL:
  2224. Int64ToCommaSepKString(LONGLONG(pProcInfo->m_NonPagedPool), plvitem->pszText, plvitem->cchTextMax);
  2225. break;
  2226. case COL_BASEPRIORITY:
  2227. {
  2228. LPCTSTR pszClass = NULL;
  2229. switch(pProcInfo->m_PriClass)
  2230. {
  2231. case REALTIME_PRIORITY_CLASS:
  2232. pszClass = g_szRealtime;
  2233. break;
  2234. case HIGH_PRIORITY_CLASS:
  2235. pszClass = g_szHigh;
  2236. break;
  2237. case ABOVE_NORMAL_PRIORITY_CLASS:
  2238. pszClass = g_szAboveNormal;
  2239. break;
  2240. case NORMAL_PRIORITY_CLASS:
  2241. pszClass = g_szNormal;
  2242. break;
  2243. case BELOW_NORMAL_PRIORITY_CLASS:
  2244. pszClass = g_szBelowNormal;
  2245. break;
  2246. case IDLE_PRIORITY_CLASS:
  2247. pszClass = g_szLow;
  2248. break;
  2249. default:
  2250. pszClass = g_szUnknown;
  2251. break;
  2252. }
  2253. // don't care if it truncates - UI only
  2254. StringCchCopy(plvitem->pszText, plvitem->cchTextMax, pszClass );
  2255. break;
  2256. }
  2257. case COL_HANDLECOUNT:
  2258. Int64ToCommaSepString(LONGLONG(pProcInfo->m_HandleCount), plvitem->pszText, plvitem->cchTextMax);
  2259. break;
  2260. case COL_THREADCOUNT:
  2261. Int64ToCommaSepString(LONGLONG(pProcInfo->m_ThreadCount), plvitem->pszText, plvitem->cchTextMax);
  2262. break;
  2263. case COL_USEROBJECTS:
  2264. Int64ToCommaSepString(LONGLONG(pProcInfo->m_USERObjectCount), plvitem->pszText, plvitem->cchTextMax);
  2265. break;
  2266. case COL_GDIOBJECTS:
  2267. Int64ToCommaSepString(LONGLONG(pProcInfo->m_GDIObjectCount), plvitem->pszText, plvitem->cchTextMax);
  2268. break;
  2269. case COL_READOPERCOUNT:
  2270. Int64ToCommaSepString(pProcInfo->m_IoReadOperCount, plvitem->pszText, plvitem->cchTextMax);
  2271. break;
  2272. case COL_WRITEOPERCOUNT:
  2273. Int64ToCommaSepString(pProcInfo->m_IoWriteOperCount, plvitem->pszText, plvitem->cchTextMax);
  2274. break;
  2275. case COL_OTHEROPERCOUNT:
  2276. Int64ToCommaSepString(pProcInfo->m_IoOtherOperCount, plvitem->pszText, plvitem->cchTextMax);
  2277. break;
  2278. case COL_READXFERCOUNT:
  2279. Int64ToCommaSepString(pProcInfo->m_IoReadXferCount, plvitem->pszText, plvitem->cchTextMax);
  2280. break;
  2281. case COL_WRITEXFERCOUNT:
  2282. Int64ToCommaSepString(pProcInfo->m_IoWriteXferCount, plvitem->pszText, plvitem->cchTextMax);
  2283. break;
  2284. case COL_OTHERXFERCOUNT:
  2285. Int64ToCommaSepString(pProcInfo->m_IoOtherXferCount, plvitem->pszText, plvitem->cchTextMax);
  2286. break;
  2287. default:
  2288. Assert( 0 && "Unknown listview subitem" );
  2289. break;
  2290. } // end switch(columnid)
  2291. } // end LVIF_TEXT case
  2292. } // end LVN_GETDISPINFO case
  2293. break;
  2294. } // end switch(pnmhdr->code)
  2295. done:
  2296. return 1;
  2297. }
  2298. /*++ CProcPage::TimerEvent
  2299. Routine Description:
  2300. Called by main app when the update time fires
  2301. Arguments:
  2302. Return Value:
  2303. Revision History:
  2304. Nov-20-95 Davepl Created
  2305. --*/
  2306. void CProcPage::TimerEvent()
  2307. {
  2308. if (FALSE == m_fPaused)
  2309. {
  2310. // We only process updates when the display is not paused, ie:
  2311. // not during trackpopupmenu loop
  2312. UpdateProcInfoArray();
  2313. UpdateProcListview();
  2314. }
  2315. }
  2316. /*++ CProcPage::HandleProcListContextMenu
  2317. Routine Description:
  2318. Handles right-clicks (context menu) in the proc list
  2319. Arguments:
  2320. xPos, yPos - coords of where the click occurred
  2321. Return Value:
  2322. Revision History:
  2323. Nov-22-95 Davepl Created
  2324. --*/
  2325. void CProcPage::HandleProcListContextMenu(INT xPos, INT yPos)
  2326. {
  2327. HWND hTaskList = GetDlgItem(m_hPage, IDC_PROCLIST);
  2328. INT iItem = ListView_GetNextItem(hTaskList, -1, LVNI_SELECTED);
  2329. if (-1 != iItem)
  2330. {
  2331. if (0xFFFF == LOWORD(xPos) && 0xFFFF == LOWORD(yPos))
  2332. {
  2333. RECT rcItem;
  2334. ListView_GetItemRect(hTaskList, iItem, &rcItem, LVIR_ICON);
  2335. MapWindowRect(hTaskList, NULL, &rcItem);
  2336. xPos = rcItem.right;
  2337. yPos = rcItem.bottom;
  2338. }
  2339. HMENU hPopup = LoadPopupMenu(g_hInstance, IDR_PROC_CONTEXT);
  2340. if (hPopup)
  2341. {
  2342. if (hPopup && SHRestricted(REST_NORUN))
  2343. {
  2344. DeleteMenu(hPopup, IDM_RUN, MF_BYCOMMAND);
  2345. }
  2346. CProcInfo * pProc = GetSelectedProcess();
  2347. if (NULL == pProc)
  2348. {
  2349. return;
  2350. }
  2351. //
  2352. // If no debugger is installed or it's a 16-bit app
  2353. // ghost the debug menu item
  2354. //
  2355. if (NULL == m_pszDebugger || pProc->IsWowTask())
  2356. {
  2357. EnableMenuItem(hPopup, IDM_PROC_DEBUG, MF_GRAYED | MF_DISABLED | MF_BYCOMMAND);
  2358. }
  2359. //
  2360. // If it's a 16-bit task grey the priority choices
  2361. //
  2362. if (pProc->IsWowTask())
  2363. {
  2364. EnableMenuItem(hPopup, IDM_PROC_REALTIME, MF_GRAYED | MF_DISABLED | MF_BYCOMMAND);
  2365. EnableMenuItem(hPopup, IDM_PROC_ABOVENORMAL,MF_GRAYED | MF_DISABLED | MF_BYCOMMAND);
  2366. EnableMenuItem(hPopup, IDM_PROC_NORMAL, MF_GRAYED | MF_DISABLED | MF_BYCOMMAND);
  2367. EnableMenuItem(hPopup, IDM_PROC_BELOWNORMAL,MF_GRAYED | MF_DISABLED | MF_BYCOMMAND);
  2368. EnableMenuItem(hPopup, IDM_PROC_HIGH, MF_GRAYED | MF_DISABLED | MF_BYCOMMAND);
  2369. EnableMenuItem(hPopup, IDM_PROC_LOW, MF_GRAYED | MF_DISABLED | MF_BYCOMMAND);
  2370. }
  2371. //
  2372. // If not an MP machine, remove the affinity option
  2373. //
  2374. if (1 == g_cProcessors || pProc->IsWowTask())
  2375. {
  2376. DeleteMenu(hPopup, IDM_AFFINITY, MF_BYCOMMAND);
  2377. }
  2378. DWORD dwPri = pProc->m_PriClass;
  2379. INT idCheck = 0;
  2380. //
  2381. // These constants are listed in the SDK
  2382. //
  2383. if (dwPri == IDLE_PRIORITY_CLASS)
  2384. {
  2385. idCheck = IDM_PROC_LOW;
  2386. }
  2387. else if (dwPri == BELOW_NORMAL_PRIORITY_CLASS)
  2388. {
  2389. idCheck = IDM_PROC_BELOWNORMAL;
  2390. }
  2391. else if (dwPri == NORMAL_PRIORITY_CLASS)
  2392. {
  2393. idCheck = IDM_PROC_NORMAL;
  2394. }
  2395. else if (dwPri == ABOVE_NORMAL_PRIORITY_CLASS)
  2396. {
  2397. idCheck = IDM_PROC_ABOVENORMAL;
  2398. }
  2399. else if (dwPri == HIGH_PRIORITY_CLASS)
  2400. {
  2401. idCheck = IDM_PROC_HIGH;
  2402. }
  2403. else
  2404. {
  2405. Assert(dwPri == REALTIME_PRIORITY_CLASS);
  2406. idCheck = IDM_PROC_REALTIME;
  2407. }
  2408. // Check the appropriate radio menu for this process' priority class
  2409. CheckMenuRadioItem(hPopup, IDM_PROC_REALTIME, IDM_PROC_LOW, idCheck, MF_BYCOMMAND);
  2410. m_fPaused = TRUE;
  2411. g_fInPopup = TRUE;
  2412. TrackPopupMenuEx(hPopup, 0, xPos, yPos, m_hPage, NULL);
  2413. g_fInPopup = FALSE;
  2414. m_fPaused = FALSE;
  2415. //
  2416. // If one of the context menu actions (ie: Kill) requires that the display
  2417. // get updated, do it now
  2418. //
  2419. DestroyMenu(hPopup);
  2420. }
  2421. }
  2422. }
  2423. /*++ AffinityDlgProc
  2424. Routine Description:
  2425. Dialog procedure for the affinity mask dialog. Basically just tracks 32 check
  2426. boxes that represent the processors
  2427. Arguments:
  2428. standard dlgproc fare 0 - initial lParam is pointer to affinity mask
  2429. Revision History:
  2430. Jan-17-96 Davepl Created
  2431. --*/
  2432. INT_PTR CALLBACK AffinityDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  2433. {
  2434. static DWORD_PTR * pdwAffinity = NULL; // One of the joys of single threadedness
  2435. switch (uMsg)
  2436. {
  2437. case WM_INITDIALOG:
  2438. {
  2439. pdwAffinity = (DWORD_PTR *) lParam;
  2440. WCHAR szName[ 64 ];
  2441. WCHAR szFormatString[ 64 ];
  2442. RECT rcCPU0;
  2443. HFONT hFont;
  2444. HWND hwndCPU0 = GetDlgItem( hwndDlg, IDC_CPU0 );
  2445. GetWindowRect( hwndCPU0, &rcCPU0 );
  2446. MapWindowPoints(HWND_DESKTOP, hwndDlg, (LPPOINT) &rcCPU0, 2);
  2447. GetWindowText( hwndCPU0, szFormatString, ARRAYSIZE(szFormatString) );
  2448. hFont = (HFONT) SendMessage( hwndCPU0, WM_GETFONT, 0, 0 );
  2449. StringCchPrintf( szName, ARRAYSIZE(szName), szFormatString, 0 );
  2450. SetWindowText( hwndCPU0, szName );
  2451. CheckDlgButton(hwndDlg, IDC_CPU0, ((*pdwAffinity & 1 ) != 0));
  2452. int width = rcCPU0.right - rcCPU0.left + g_ControlWidthSpacing;
  2453. int height = rcCPU0.bottom - rcCPU0.top + g_ControlHeightSpacing;
  2454. int cProcessors = (int) ( g_cProcessors > sizeof(*pdwAffinity) * 8 ? sizeof(*pdwAffinity) * 8 : g_cProcessors );
  2455. for ( int i = 1; i < cProcessors; i ++ )
  2456. {
  2457. StringCchPrintf( szName, ARRAYSIZE(szName), szFormatString, i );
  2458. HWND hwnd = CreateWindow( L"BUTTON"
  2459. , szName
  2460. , BS_AUTOCHECKBOX | WS_TABSTOP | WS_VISIBLE | WS_CHILD
  2461. , rcCPU0.left + width * ( i % 4 )
  2462. , rcCPU0.top + height * ( i / 4)
  2463. , rcCPU0.right - rcCPU0.left
  2464. , rcCPU0.bottom - rcCPU0.top
  2465. , hwndDlg
  2466. , (HMENU) ((ULONGLONG) IDC_CPU0 + i)
  2467. , NULL // ignored
  2468. , NULL
  2469. );
  2470. if ( NULL != hwnd )
  2471. {
  2472. SendMessage( hwnd, WM_SETFONT, (WPARAM) hFont, TRUE );
  2473. if ( *pdwAffinity & (1ULL << i) )
  2474. {
  2475. CheckDlgButton(hwndDlg, IDC_CPU0 + i, TRUE);
  2476. }
  2477. }
  2478. }
  2479. if ( cProcessors > 4 )
  2480. {
  2481. //
  2482. // Need to make the dialog bigger and move some stuff around.
  2483. //
  2484. int delta = height * (( cProcessors / 4 ) + ( cProcessors % 4 == 0 ? 0 : 1 ) );
  2485. RECT rc;
  2486. HWND hwnd;
  2487. hwnd = GetDlgItem( hwndDlg, IDOK );
  2488. GetWindowRect( hwnd, &rc );
  2489. MapWindowPoints(HWND_DESKTOP, hwndDlg, (LPPOINT) &rc, 2);
  2490. SetWindowPos( hwnd, NULL, rc.left, rcCPU0.top + delta, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
  2491. hwnd = GetDlgItem( hwndDlg, IDCANCEL );
  2492. GetWindowRect( hwnd, &rc );
  2493. MapWindowPoints(HWND_DESKTOP, hwndDlg, (LPPOINT) &rc, 2);
  2494. SetWindowPos( hwnd, NULL, rc.left, rcCPU0.top + delta, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
  2495. GetClientRect( hwndDlg, &rc );
  2496. SetWindowPos( hwndDlg, NULL, 0, 0, rc.right, rc.bottom + delta + g_ControlHeightSpacing, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
  2497. }
  2498. SetFocus( hwndCPU0 );
  2499. }
  2500. return FALSE; // do not set the default focus.
  2501. case WM_COMMAND:
  2502. switch (LOWORD(wParam))
  2503. {
  2504. case IDCANCEL:
  2505. EndDialog(hwndDlg, IDCANCEL);
  2506. break;
  2507. case IDOK:
  2508. *pdwAffinity = 0;
  2509. for (int i = 0; i < g_cProcessors; i++)
  2510. {
  2511. if (IsDlgButtonChecked(hwndDlg, IDC_CPU0 + i))
  2512. {
  2513. *pdwAffinity |= 1ULL << i;
  2514. }
  2515. }
  2516. if (*pdwAffinity == 0)
  2517. {
  2518. // Can't set affinity to "none"
  2519. WCHAR szTitle[MAX_PATH];
  2520. WCHAR szBody[MAX_PATH];
  2521. if (0 == LoadString(g_hInstance, IDS_INVALIDOPTION, szTitle, ARRAYSIZE(szTitle)) ||
  2522. 0 == LoadString(g_hInstance, IDS_NOAFFINITYMASK, szBody, ARRAYSIZE(szBody)))
  2523. {
  2524. break;
  2525. }
  2526. MessageBox(hwndDlg, szBody, szTitle, MB_ICONERROR);
  2527. break;
  2528. }
  2529. EndDialog(hwndDlg, IDOK);
  2530. break;
  2531. }
  2532. break;
  2533. }
  2534. return FALSE;
  2535. }
  2536. /*++ SetAffinity
  2537. Routine Description:
  2538. Puts up a dialog that lets the user adjust the processor affinity
  2539. for a process
  2540. Arguments:
  2541. pid - process Id of process to modify
  2542. Return Value:
  2543. boolean success
  2544. Revision History:
  2545. Jan-17-96 Davepl Created
  2546. --*/
  2547. BOOL CProcPage::SetAffinity(DWORD pid)
  2548. {
  2549. BOOL fSuccess = FALSE;
  2550. HANDLE hProcess = OpenProcess( PROCESS_SET_INFORMATION | PROCESS_QUERY_INFORMATION, FALSE, pid );
  2551. if (hProcess)
  2552. {
  2553. DWORD_PTR dwAffinity;
  2554. DWORD_PTR dwUnusedSysAfin;
  2555. if (GetProcessAffinityMask(hProcess, &dwAffinity, &dwUnusedSysAfin))
  2556. {
  2557. if (IDOK == DialogBoxParam(g_hInstance,
  2558. MAKEINTRESOURCE(IDD_AFFINITY),
  2559. m_hPage,
  2560. AffinityDlgProc,
  2561. (LPARAM) &dwAffinity))
  2562. {
  2563. if (SetProcessAffinityMask(hProcess, dwAffinity))
  2564. {
  2565. fSuccess = TRUE;
  2566. }
  2567. }
  2568. else
  2569. {
  2570. fSuccess = TRUE; // Cancel, so no failure
  2571. }
  2572. }
  2573. CloseHandle(hProcess);
  2574. }
  2575. if (!fSuccess)
  2576. {
  2577. DWORD dwError = GetLastError();
  2578. DisplayFailureMsg(m_hPage, IDS_CANTSETAFFINITY, dwError);
  2579. }
  2580. return fSuccess;
  2581. }
  2582. //
  2583. //
  2584. //
  2585. BOOL CProcPage::IsSystemProcess(DWORD pid, CProcInfo * pProcInfo)
  2586. {
  2587. // We don't allow the following set of critical system processes to be terminated,
  2588. // since the system would bugcheck immediately, no matter who you are.
  2589. static const LPCTSTR apszCantKill[] =
  2590. {
  2591. TEXT("csrss.exe"), TEXT("winlogon.exe"), TEXT("smss.exe"), TEXT("services.exe"), TEXT("lsass.exe")
  2592. };
  2593. // if they pass in a pProcInfo we'll use it, otherwise find it ourselves
  2594. if (!pProcInfo)
  2595. {
  2596. pProcInfo = FindProcInArrayByPID(m_pProcArray, pid);
  2597. }
  2598. if (!pProcInfo)
  2599. {
  2600. return FALSE;
  2601. }
  2602. for (int i = 0; i < ARRAYSIZE(apszCantKill); ++i)
  2603. {
  2604. if (0 == lstrcmpi(pProcInfo->m_pszImageName, apszCantKill[i]))
  2605. {
  2606. WCHAR szTitle[MAX_PATH];
  2607. WCHAR szBody[MAX_PATH];
  2608. if (0 != LoadString(g_hInstance, IDS_CANTKILL, szTitle, ARRAYSIZE(szTitle)) &&
  2609. 0 != LoadString(g_hInstance, IDS_KILLSYS, szBody, ARRAYSIZE(szBody)))
  2610. {
  2611. MessageBox(m_hPage, szBody, szTitle, MB_ICONEXCLAMATION | MB_OK);
  2612. }
  2613. return TRUE;
  2614. }
  2615. }
  2616. return FALSE;
  2617. }
  2618. /*++ KillProcess
  2619. Routine Description:
  2620. Kills a process
  2621. Arguments:
  2622. pid - process Id of process to kill
  2623. Return Value:
  2624. Revision History:
  2625. Nov-22-95 Davepl Created
  2626. --*/
  2627. BOOL CProcPage::KillProcess(DWORD pid, BOOL bBatchKill)
  2628. {
  2629. DWORD dwError = ERROR_SUCCESS;
  2630. //
  2631. // Special-case killing WOW tasks
  2632. //
  2633. CProcInfo * pProcInfo;
  2634. pProcInfo = FindProcInArrayByPID(m_pProcArray, pid);
  2635. if (NULL == pProcInfo)
  2636. return FALSE;
  2637. if (IsSystemProcess(pid, pProcInfo))
  2638. return FALSE;
  2639. // Grab info from pProcInfo (because once we call QuickConfirm(), the
  2640. // pProcInfo pointer may be invalid)
  2641. INT_PTR fWowTask = pProcInfo->IsWowTask();
  2642. #if defined (_WIN64)
  2643. #else
  2644. DWORD dwRealPID = pProcInfo->GetRealPID();
  2645. WORD hTaskWow = pProcInfo->m_htaskWow;
  2646. #endif
  2647. // OK so far, now confirm that the user really wants to do this.
  2648. if (!bBatchKill && (IDYES != QuickConfirm(IDS_WARNING, IDS_KILL)))
  2649. {
  2650. return FALSE;
  2651. }
  2652. // We can't use this pointer after QuickConfirm() is called.
  2653. // NULL it out to prevent subtle bugs.
  2654. pProcInfo = NULL;
  2655. if (fWowTask) {
  2656. #if defined (_WIN64)
  2657. return FALSE;
  2658. #else
  2659. return VDMTerminateTaskWOW(dwRealPID, hTaskWow);
  2660. #endif
  2661. }
  2662. //
  2663. // If possible, enable the Debug privilege. This allows us to kill
  2664. // processes not owned by the current user, including processes
  2665. // running in other TS sessions.
  2666. //
  2667. // Alternatively, we could first open the process for WRITE_DAC,
  2668. // grant ourselves PROCESS_TERMINATE access, and then reopen the
  2669. // process to kill it.
  2670. //
  2671. CPrivilegeEnable privilege(SE_DEBUG_NAME);
  2672. HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, FALSE, pid );
  2673. if (hProcess)
  2674. {
  2675. if (FALSE == TerminateProcess( hProcess, 1 ))
  2676. {
  2677. dwError = GetLastError();
  2678. }
  2679. else
  2680. {
  2681. TimerEvent();
  2682. }
  2683. CloseHandle( hProcess );
  2684. }
  2685. else
  2686. {
  2687. dwError = GetLastError();
  2688. }
  2689. if (ERROR_SUCCESS != dwError)
  2690. {
  2691. DisplayFailureMsg(m_hPage, IDS_CANTKILL, dwError);
  2692. return FALSE;
  2693. }
  2694. else
  2695. {
  2696. return TRUE;
  2697. }
  2698. }
  2699. /*++ AttachDebugger
  2700. Routine Description:
  2701. Attaches the debugger listed in the AeDebug reg key to the specified
  2702. running process
  2703. Arguments:
  2704. pid - process Id of process to debug
  2705. Return Value:
  2706. Revision History:
  2707. Nov-27-95 Davepl Created
  2708. --*/
  2709. BOOL CProcPage::AttachDebugger(DWORD pid)
  2710. {
  2711. if (IDYES != QuickConfirm(IDS_WARNING, IDS_DEBUG))
  2712. {
  2713. return FALSE;
  2714. }
  2715. WCHAR szCmdline[MAX_PATH * 2];
  2716. //
  2717. // Don't construct an incomplete string.
  2718. //
  2719. HRESULT hr = StringCchPrintf( szCmdline, ARRAYSIZE(szCmdline), L"%s -p %ld", m_pszDebugger, pid );
  2720. if ( S_OK == hr )
  2721. {
  2722. STARTUPINFO sinfo =
  2723. {
  2724. sizeof(STARTUPINFO),
  2725. };
  2726. PROCESS_INFORMATION pinfo;
  2727. if (FALSE == CreateProcess(NULL,
  2728. szCmdline,
  2729. NULL,
  2730. NULL,
  2731. FALSE,
  2732. CREATE_NEW_CONSOLE,
  2733. NULL,
  2734. NULL,
  2735. &sinfo,
  2736. &pinfo))
  2737. {
  2738. hr = GetLastHRESULT();
  2739. }
  2740. else
  2741. {
  2742. CloseHandle(pinfo.hThread);
  2743. CloseHandle(pinfo.hProcess);
  2744. }
  2745. }
  2746. if (S_OK != hr)
  2747. {
  2748. DisplayFailureMsg(m_hPage, IDS_CANTDEBUG, hr);
  2749. return FALSE;
  2750. }
  2751. else
  2752. {
  2753. return TRUE;
  2754. }
  2755. }
  2756. /*++ SetPriority
  2757. Routine Description:
  2758. Sets a process' priority class
  2759. Arguments:
  2760. pid - process Id of process to change
  2761. pri - ID_CMD_XXXXXX menu choice of priority
  2762. Return Value:
  2763. Revision History:
  2764. Nov-27-95 Davepl Created
  2765. --*/
  2766. BOOL CProcPage::SetPriority(CProcInfo * pProc, DWORD idCmd)
  2767. {
  2768. DWORD dwError = ERROR_SUCCESS;
  2769. DWORD oldPri;
  2770. DWORD pri;
  2771. // Determine which priority class we need to use based
  2772. // on the menu selection
  2773. switch (idCmd)
  2774. {
  2775. case IDM_PROC_LOW:
  2776. pri = IDLE_PRIORITY_CLASS;
  2777. break;
  2778. case IDM_PROC_BELOWNORMAL:
  2779. pri = BELOW_NORMAL_PRIORITY_CLASS;
  2780. break;
  2781. case IDM_PROC_ABOVENORMAL:
  2782. pri = ABOVE_NORMAL_PRIORITY_CLASS;
  2783. break;
  2784. case IDM_PROC_HIGH:
  2785. pri = HIGH_PRIORITY_CLASS;
  2786. break;
  2787. case IDM_PROC_REALTIME:
  2788. pri = REALTIME_PRIORITY_CLASS;
  2789. break;
  2790. default:
  2791. Assert(idCmd == IDM_PROC_NORMAL);
  2792. pri = NORMAL_PRIORITY_CLASS;
  2793. break;
  2794. }
  2795. oldPri = (DWORD) pProc->m_PriClass;
  2796. if ( oldPri == pri )
  2797. {
  2798. return FALSE; // nothing to do.
  2799. }
  2800. //
  2801. // Get confirmation before we change the priority
  2802. //
  2803. if (IDYES != QuickConfirm(IDS_WARNING, IDS_PRICHANGE))
  2804. {
  2805. return FALSE;
  2806. }
  2807. HANDLE hProcess = OpenProcess( PROCESS_SET_INFORMATION, FALSE, pProc->m_UniqueProcessId);
  2808. if (hProcess)
  2809. {
  2810. if (FALSE == SetPriorityClass( hProcess, pri ))
  2811. {
  2812. dwError = GetLastError();
  2813. }
  2814. else
  2815. {
  2816. TimerEvent();
  2817. }
  2818. CloseHandle( hProcess );
  2819. }
  2820. else
  2821. {
  2822. dwError = GetLastError();
  2823. }
  2824. if (ERROR_SUCCESS != dwError)
  2825. {
  2826. DisplayFailureMsg(m_hPage, IDS_CANTCHANGEPRI, dwError);
  2827. return FALSE;
  2828. }
  2829. else
  2830. {
  2831. return TRUE;
  2832. }
  2833. }
  2834. /*++ CProcPage::GetSelectedProcess
  2835. Routine Description:
  2836. Returns the CProcInfo * of the currently selected process
  2837. Arguments:
  2838. Return Value:
  2839. CProcInfo * on success, NULL on error or nothing selected
  2840. Revision History:
  2841. Nov-22-95 Davepl Created
  2842. --*/
  2843. CProcInfo * CProcPage::GetSelectedProcess()
  2844. {
  2845. HWND hTaskList = GetDlgItem(m_hPage, IDC_PROCLIST);
  2846. INT iItem = ListView_GetNextItem(hTaskList, -1, LVNI_SELECTED);
  2847. CProcInfo * pProc;
  2848. if (-1 != iItem)
  2849. {
  2850. LV_ITEM lvitem = { LVIF_PARAM };
  2851. lvitem.iItem = iItem;
  2852. if (ListView_GetItem(hTaskList, &lvitem))
  2853. {
  2854. pProc = (CProcInfo *) (lvitem.lParam);
  2855. }
  2856. else
  2857. {
  2858. pProc = NULL;
  2859. }
  2860. }
  2861. else
  2862. {
  2863. pProc = NULL;
  2864. }
  2865. return pProc;
  2866. }
  2867. /*++ CProcPage::HandleWMCOMMAND
  2868. Routine Description:
  2869. Handles WM_COMMANDS received at the main page dialog
  2870. Arguments:
  2871. id - Command id of command received
  2872. Return Value:
  2873. Revision History:
  2874. Nov-22-95 Davepl Created
  2875. --*/
  2876. void CProcPage::HandleWMCOMMAND( WORD id , HWND hCtrl )
  2877. {
  2878. CProcInfo * pProc = GetSelectedProcess();
  2879. switch(id)
  2880. {
  2881. case IDC_DEBUG:
  2882. case IDM_PROC_DEBUG:
  2883. if (pProc && m_pszDebugger)
  2884. {
  2885. AttachDebugger( pProc->m_UniqueProcessId);
  2886. }
  2887. break;
  2888. case IDC_ENDTASK:
  2889. case IDC_TERMINATE:
  2890. case IDM_PROC_TERMINATE:
  2891. if (pProc)
  2892. {
  2893. KillProcess( pProc->m_UniqueProcessId);
  2894. }
  2895. break;
  2896. case IDM_ENDTREE:
  2897. if (pProc)
  2898. {
  2899. RecursiveKill( pProc->m_UniqueProcessId);
  2900. }
  2901. break;
  2902. case IDM_AFFINITY:
  2903. if (pProc)
  2904. {
  2905. SetAffinity( pProc->m_UniqueProcessId);
  2906. }
  2907. break;
  2908. case IDM_PROC_REALTIME:
  2909. case IDM_PROC_HIGH:
  2910. case IDM_PROC_ABOVENORMAL:
  2911. case IDM_PROC_NORMAL:
  2912. case IDM_PROC_BELOWNORMAL:
  2913. case IDM_PROC_LOW:
  2914. if (pProc)
  2915. {
  2916. SetPriority( pProc, id);
  2917. }
  2918. break;
  2919. case IDC_SHOWALL:
  2920. g_Options.m_bShowAllProcess = SendMessage( hCtrl , BM_GETCHECK , 0 , 0 ) == BST_CHECKED;
  2921. break;
  2922. }
  2923. }
  2924. /*++ ProcPageProc
  2925. Routine Description:
  2926. Dialogproc for the process page.
  2927. Arguments:
  2928. hwnd - handle to dialog box
  2929. uMsg - message
  2930. wParam - first message parameter
  2931. lParam - second message parameter
  2932. Return Value:
  2933. For WM_INITDIALOG, TRUE == user32 sets focus, FALSE == we set focus
  2934. For others, TRUE == this proc handles the message
  2935. Revision History:
  2936. Nov-16-95 Davepl Created
  2937. --*/
  2938. INT_PTR CALLBACK ProcPageProc(
  2939. HWND hwnd, // handle to dialog box
  2940. UINT uMsg, // message
  2941. WPARAM wParam, // first message parameter
  2942. LPARAM lParam // second message parameter
  2943. )
  2944. {
  2945. CProcPage * thispage = (CProcPage *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
  2946. // See if the parent wants this message
  2947. if (TRUE == CheckParentDeferrals(uMsg, wParam, lParam))
  2948. {
  2949. return TRUE;
  2950. }
  2951. switch(uMsg)
  2952. {
  2953. case WM_INITDIALOG:
  2954. {
  2955. SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam);
  2956. CProcPage * thispage = (CProcPage *) lParam;
  2957. thispage->m_hPage = hwnd;
  2958. // Turn on SHOWSELALWAYS so that the selection is still highlighted even
  2959. // when focus is lost to one of the buttons (for example)
  2960. HWND hTaskList = GetDlgItem(hwnd, IDC_PROCLIST);
  2961. SetWindowLong(hTaskList, GWL_STYLE, GetWindowLong(hTaskList, GWL_STYLE) | LVS_SHOWSELALWAYS);
  2962. ListView_SetExtendedListViewStyle(hTaskList, LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_DOUBLEBUFFER);
  2963. //
  2964. // This was removed from CProcPage::Activate
  2965. //
  2966. HWND hchk = GetDlgItem( hwnd , IDC_SHOWALL );
  2967. if( hchk != NULL )
  2968. {
  2969. if( g_fIsTSEnabled )
  2970. {
  2971. // Disable the IDC_SHOWALL checkbox for non-admin. YufengZ 03/23/98
  2972. ShowWindow(hchk, TRUE);
  2973. if( !IsUserAdmin( ) )
  2974. {
  2975. EnableWindow( hchk, FALSE );
  2976. }
  2977. else
  2978. {
  2979. WPARAM wp = g_Options.m_bShowAllProcess ? BST_CHECKED : BST_UNCHECKED;
  2980. SendMessage( hchk , BM_SETCHECK , wp , 0 );
  2981. //Button_SetCheck( hchk , BST_CHECKED );
  2982. }
  2983. }
  2984. else
  2985. {
  2986. // hide the IDC_SHOWALL checkbox if its not terminal server.
  2987. ShowWindow( hchk , SW_HIDE );
  2988. }
  2989. }
  2990. }
  2991. // We handle focus during Activate(). Return FALSE here so the
  2992. // dialog manager doesn't try to set focus.
  2993. return FALSE;
  2994. case WM_DESTROY:
  2995. thispage->RememberColumnOrder(GetDlgItem(hwnd, IDC_PROCLIST));
  2996. break;
  2997. case WM_LBUTTONUP:
  2998. case WM_LBUTTONDOWN:
  2999. // We need to fake client mouse clicks in this child to appear as nonclient
  3000. // (caption) clicks in the parent so that the user can drag the entire app
  3001. // when the title bar is hidden by dragging the client area of this child
  3002. if (g_Options.m_fNoTitle)
  3003. {
  3004. SendMessage(g_hMainWnd,
  3005. uMsg == WM_LBUTTONUP ? WM_NCLBUTTONUP : WM_NCLBUTTONDOWN,
  3006. HTCAPTION,
  3007. lParam);
  3008. }
  3009. break;
  3010. case WM_NCLBUTTONDBLCLK:
  3011. case WM_LBUTTONDBLCLK:
  3012. SendMessage(g_hMainWnd, uMsg, wParam, lParam);
  3013. break;
  3014. // We have been asked to find and select a process
  3015. case WM_FINDPROC:
  3016. {
  3017. DWORD cProcs = thispage->m_pProcArray->GetSize();
  3018. DWORD dwProcessId;
  3019. for (INT iPass = 0; iPass < 2; iPass++)
  3020. {
  3021. //
  3022. // On the first pass we try to find a WOW
  3023. // task with a thread ID which matches the
  3024. // one given in wParam. If we don't find
  3025. // such a task, we look for a process which
  3026. // matches the PID in lParam.
  3027. //
  3028. for (UINT i = 0; i < cProcs; i++)
  3029. {
  3030. CProcInfo *pProc = (CProcInfo *)thispage->m_pProcArray->GetAt(i);
  3031. dwProcessId = pProc->m_UniqueProcessId;
  3032. if ((!iPass && wParam == (WPARAM) dwProcessId) ||
  3033. ( iPass && lParam == (LPARAM) dwProcessId))
  3034. {
  3035. // TS filters items out of the view so cannot assume
  3036. // that m_pProcArray is in sync with the listview.
  3037. HWND hwndLV = GetDlgItem(hwnd, IDC_PROCLIST);
  3038. LVFINDINFO fi;
  3039. fi.flags = LVFI_PARAM;
  3040. fi.lParam = (LPARAM)pProc;
  3041. int iItem = ListView_FindItem(hwndLV, -1, &fi);
  3042. if (iItem >= 0)
  3043. {
  3044. ListView_SetItemState (hwndLV,
  3045. iItem,
  3046. LVIS_FOCUSED | LVIS_SELECTED,
  3047. 0x000F);
  3048. ListView_EnsureVisible(hwndLV, iItem, FALSE);
  3049. }
  3050. else
  3051. {
  3052. // We found the process but the user isn't allowed
  3053. // to see it; remove the selection
  3054. ListView_SetItemState (hwndLV,
  3055. -1,
  3056. 0,
  3057. LVIS_FOCUSED | LVIS_SELECTED);
  3058. }
  3059. goto FoundProc;
  3060. }
  3061. }
  3062. }
  3063. }
  3064. FoundProc:
  3065. break;
  3066. case WM_COMMAND:
  3067. thispage->HandleWMCOMMAND( LOWORD(wParam) , ( HWND )lParam );
  3068. break;
  3069. case WM_CONTEXTMENU:
  3070. {
  3071. CProcInfo * pProc = thispage->GetSelectedProcess();
  3072. if (pProc && pProc->m_UniqueProcessId)
  3073. {
  3074. if ((HWND) wParam == GetDlgItem(hwnd, IDC_PROCLIST))
  3075. {
  3076. thispage->HandleProcListContextMenu(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
  3077. return TRUE;
  3078. }
  3079. }
  3080. }
  3081. break;
  3082. case WM_NOTIFY:
  3083. return thispage->HandleProcPageNotify((LPNMHDR) lParam);
  3084. case WM_SIZE:
  3085. //
  3086. // Size our kids
  3087. //
  3088. thispage->SizeProcPage();
  3089. return TRUE;
  3090. case WM_SYSCOLORCHANGE:
  3091. SendMessage(GetDlgItem(hwnd, IDC_PROCLIST), uMsg, wParam, lParam);
  3092. return TRUE;
  3093. }
  3094. return FALSE;
  3095. }
  3096. /*++ CProcPage::GetTitle
  3097. Routine Description:
  3098. Copies the title of this page to the caller-supplied buffer
  3099. Arguments:
  3100. pszText - the buffer to copy to
  3101. bufsize - size of buffer, in characters
  3102. Return Value:
  3103. Revision History:
  3104. Nov-16-95 Davepl Created
  3105. --*/
  3106. void CProcPage::GetTitle(LPTSTR pszText, size_t bufsize)
  3107. {
  3108. LoadString(g_hInstance, IDS_PROCPAGETITLE, pszText, static_cast<int>(bufsize));
  3109. }
  3110. /*++ CProcPage::Activate
  3111. Routine Description:
  3112. Brings this page to the front, sets its initial position,
  3113. and shows it
  3114. Arguments:
  3115. Return Value:
  3116. HRESULT (S_OK on success)
  3117. Revision History:
  3118. Nov-16-95 Davepl Created
  3119. --*/
  3120. HRESULT CProcPage::Activate()
  3121. {
  3122. //
  3123. // Make this page visible
  3124. //
  3125. ShowWindow(m_hPage, SW_SHOW);
  3126. SetWindowPos(m_hPage,
  3127. HWND_TOP,
  3128. 0, 0, 0, 0,
  3129. SWP_NOMOVE | SWP_NOSIZE);
  3130. //
  3131. // Change the menu bar to be the menu for this page
  3132. //
  3133. HMENU hMenuOld = GetMenu(g_hMainWnd);
  3134. HMENU hMenuNew = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_MAINMENU_PROC));
  3135. AdjustMenuBar(hMenuNew);
  3136. if (hMenuNew && SHRestricted(REST_NORUN))
  3137. {
  3138. DeleteMenu(hMenuNew, IDM_RUN, MF_BYCOMMAND);
  3139. }
  3140. g_hMenu = hMenuNew;
  3141. if (g_Options.m_fNoTitle == FALSE)
  3142. {
  3143. SetMenu(g_hMainWnd, hMenuNew);
  3144. }
  3145. if (hMenuOld)
  3146. {
  3147. DestroyMenu(hMenuOld);
  3148. }
  3149. // If the tab control has focus, leave it there. Otherwise, set focus
  3150. // to the listview. If we don't set focus, it may stay on the previous
  3151. // page, now hidden, which can confuse the dialog manager and may cause
  3152. // us to hang.
  3153. if (GetFocus() != m_hwndTabs)
  3154. {
  3155. SetFocus(GetDlgItem(m_hPage, IDC_PROCLIST));
  3156. }
  3157. return S_OK;
  3158. }
  3159. /*++ CProcPage::Initialize
  3160. Routine Description:
  3161. Initializes the process page
  3162. Arguments:
  3163. hwndParent - Parent on which to base sizing on: not used for creation,
  3164. since the main app window is always used as the parent in
  3165. order to keep tab order correct
  3166. Return Value:
  3167. Revision History:
  3168. Nov-16-95 Davepl Created
  3169. --*/
  3170. HRESULT CProcPage::Initialize(HWND hwndParent)
  3171. {
  3172. //
  3173. // Find out what debbuger is configured on this system
  3174. //
  3175. HKEY hkDebug;
  3176. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  3177. TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug"),
  3178. 0, KEY_READ, &hkDebug))
  3179. {
  3180. WCHAR szDebugger[MAX_PATH * 2];
  3181. DWORD cbString = sizeof(szDebugger);
  3182. LRESULT lr = RegQueryValueEx(hkDebug, TEXT("Debugger"), NULL, NULL, (LPBYTE) szDebugger, &cbString);
  3183. RegCloseKey(hkDebug); // always close the key.
  3184. if ( ERROR_SUCCESS == lr )
  3185. {
  3186. // Find the first token (which is the debugger exe name/path)
  3187. szDebugger[ ARRAYSIZE(szDebugger) - 1 ] = L'\0'; // make sure it is terminated
  3188. LPTSTR pszCmdLine = szDebugger;
  3189. if ( *pszCmdLine == TEXT('\"') )
  3190. {
  3191. //
  3192. // Scan, and skip over, subsequent characters until
  3193. // another double-quote or a null is encountered.
  3194. //
  3195. while ( *++pszCmdLine && (*pszCmdLine != TEXT('\"')) )
  3196. {
  3197. NULL;
  3198. }
  3199. //
  3200. // If we stopped on a double-quote (usual case), skip
  3201. // over it.
  3202. //
  3203. if ( *pszCmdLine == TEXT('\"') )
  3204. {
  3205. pszCmdLine++;
  3206. }
  3207. }
  3208. else
  3209. {
  3210. while (*pszCmdLine > TEXT(' '))
  3211. {
  3212. pszCmdLine++;
  3213. }
  3214. }
  3215. *pszCmdLine = TEXT('\0'); // Don't need the rest of the args, etc
  3216. // If the doctor is in, we don't allow the Debug action
  3217. if (lstrlen(szDebugger) && lstrcmpi(szDebugger, TEXT("drwtsn32")) && lstrcmpi(szDebugger, TEXT("drwtsn32.exe")))
  3218. {
  3219. DWORD cchLen = lstrlen(szDebugger) + 1;
  3220. m_pszDebugger = (LPWSTR) LocalAlloc( LPTR, sizeof(*m_pszDebugger) * cchLen );
  3221. if (NULL == m_pszDebugger)
  3222. {
  3223. return E_OUTOFMEMORY;
  3224. }
  3225. // Bail if the string copy fails.
  3226. HRESULT hr = StringCchCopy( m_pszDebugger, cchLen, szDebugger );
  3227. if(FAILED( hr ))
  3228. {
  3229. return hr;
  3230. }
  3231. }
  3232. }
  3233. }
  3234. //
  3235. // Get basic info like page size, etc.
  3236. //
  3237. NTSTATUS Status = NtQuerySystemInformation(
  3238. SystemBasicInformation,
  3239. &g_BasicInfo,
  3240. sizeof(g_BasicInfo),
  3241. NULL
  3242. );
  3243. if (!NT_SUCCESS(Status))
  3244. {
  3245. return E_FAIL;
  3246. }
  3247. //
  3248. // Create the ptr array used to hold the info on running processes
  3249. //
  3250. m_pProcArray = new CPtrArray(GetProcessHeap());
  3251. if (NULL == m_pProcArray)
  3252. {
  3253. return E_OUTOFMEMORY;
  3254. }
  3255. // Our pseudo-parent is the tab contrl, and is what we base our
  3256. // sizing on. However, in order to keep tab order right among
  3257. // the controls, we actually create ourselves with the main
  3258. // window as the parent
  3259. m_hwndTabs = hwndParent;
  3260. //
  3261. // Create the dialog which represents the body of this page
  3262. //
  3263. m_hPage = CreateDialogParam(
  3264. g_hInstance, // handle to application instance
  3265. MAKEINTRESOURCE(IDD_PROCPAGE), // identifies dialog box template name
  3266. g_hMainWnd, // handle to owner window
  3267. ProcPageProc, // pointer to dialog box procedure
  3268. (LPARAM) this ); // User data (our this pointer)
  3269. if (NULL == m_hPage)
  3270. {
  3271. return GetLastHRESULT();
  3272. }
  3273. // Set up the columns in the listview
  3274. if (FAILED(SetupColumns()))
  3275. {
  3276. DestroyWindow(m_hPage);
  3277. m_hPage = NULL;
  3278. return E_FAIL;
  3279. }
  3280. // Restore the column positions.
  3281. RestoreColumnOrder(GetDlgItem(m_hPage, IDC_PROCLIST));
  3282. // Do one initial calculation
  3283. TimerEvent();
  3284. return S_OK;
  3285. }
  3286. /*++ CProcPage::Destroy
  3287. Routine Description:
  3288. Frees whatever has been allocated by the Initialize call
  3289. Arguments:
  3290. Return Value:
  3291. Revision History:
  3292. Nov-16-95 Davepl Created
  3293. --*/
  3294. HRESULT CProcPage::Destroy()
  3295. {
  3296. if (m_pProcArray)
  3297. {
  3298. INT c = m_pProcArray->GetSize();
  3299. while (c)
  3300. {
  3301. delete (CProcInfo *) (m_pProcArray->GetAt(c - 1));
  3302. c--;
  3303. }
  3304. delete m_pProcArray;
  3305. m_pProcArray = NULL;
  3306. }
  3307. if ( m_pvBuffer != NULL )
  3308. {
  3309. HeapFree( GetProcessHeap( ), 0, m_pvBuffer );
  3310. m_pvBuffer = NULL;
  3311. }
  3312. if (m_hPage != NULL)
  3313. {
  3314. DestroyWindow(m_hPage);
  3315. m_hPage = NULL;
  3316. }
  3317. if (m_pszDebugger != NULL)
  3318. {
  3319. LocalFree(m_pszDebugger);
  3320. m_pszDebugger = NULL;
  3321. }
  3322. return S_OK;
  3323. }
  3324. /*++ CProcPage::SaveColumnWidths
  3325. Routine Description:
  3326. Saves the widths of all of the columns in the global options structure
  3327. Revision History:
  3328. Jan-26-95 Davepl Created
  3329. --*/
  3330. void CProcPage::SaveColumnWidths()
  3331. {
  3332. UINT i = 0;
  3333. LV_COLUMN col = { 0 };
  3334. while (g_Options.m_ActiveProcCol[i] != (COLUMNID) -1)
  3335. {
  3336. col.mask = LVCF_WIDTH;
  3337. if (ListView_GetColumn(GetDlgItem(m_hPage, IDC_PROCLIST), i, &col) )
  3338. {
  3339. g_Options.m_ColumnWidths[i] = col.cx;
  3340. }
  3341. else
  3342. {
  3343. ASSERT(0 && "Couldn't get the column width");
  3344. }
  3345. i++;
  3346. }
  3347. }
  3348. /*++ CProcPage::Deactivate
  3349. Routine Description:
  3350. Called when this page is losing its place up front
  3351. Arguments:
  3352. Return Value:
  3353. Revision History:
  3354. Nov-16-95 Davepl Created
  3355. --*/
  3356. void CProcPage::Deactivate()
  3357. {
  3358. SaveColumnWidths();
  3359. if (m_hPage)
  3360. {
  3361. ShowWindow(m_hPage, SW_HIDE);
  3362. }
  3363. }
  3364. /*++ CProcPage::KillAllChildren
  3365. Routine Description:
  3366. Given a pid, recursively kills it and all of its descendants
  3367. Arguments:
  3368. Return Value:
  3369. Revision History:
  3370. 2-26-01 Bretan Created
  3371. --*/
  3372. BOOL CProcPage::KillAllChildren(
  3373. DWORD dwTaskPid,
  3374. DWORD pid,
  3375. BYTE* pbBuffer,
  3376. LARGE_INTEGER CreateTime
  3377. )
  3378. {
  3379. ASSERT(pbBuffer);
  3380. BOOL rval = TRUE;
  3381. PSYSTEM_PROCESS_INFORMATION ProcessInfo = (PSYSTEM_PROCESS_INFORMATION) pbBuffer;
  3382. ULONG TotalOffset = 0;
  3383. for ( ;; ) // ever
  3384. {
  3385. // If we are a child of pid and not pid itself
  3386. // and if we have been created after pid (we can't be a child if we were created first)
  3387. if (PtrToUlong(ProcessInfo->InheritedFromUniqueProcessId) == pid &&
  3388. PtrToUlong(ProcessInfo->UniqueProcessId) != pid &&
  3389. CreateTime.QuadPart < ProcessInfo->CreateTime.QuadPart)
  3390. {
  3391. DWORD newpid = PtrToUlong(ProcessInfo->UniqueProcessId);
  3392. //
  3393. // Recurse down to the next level
  3394. //
  3395. rval = KillAllChildren(dwTaskPid, newpid, pbBuffer, ProcessInfo->CreateTime);
  3396. // Kill it if it is not task manager
  3397. if (newpid != dwTaskPid)
  3398. {
  3399. BOOL tval = KillProcess(newpid, TRUE);
  3400. //
  3401. // If it has failed earlier in the recursion
  3402. // we want to keep that failure (not overwrite it)
  3403. //
  3404. if (rval == TRUE)
  3405. {
  3406. rval = tval;
  3407. }
  3408. }
  3409. }
  3410. if (ProcessInfo->NextEntryOffset == 0)
  3411. {
  3412. break;
  3413. }
  3414. TotalOffset += ProcessInfo->NextEntryOffset;
  3415. ProcessInfo = (PSYSTEM_PROCESS_INFORMATION) &pbBuffer[ TotalOffset ];
  3416. }
  3417. return rval;
  3418. }
  3419. /*++ CProcPage::RecursiveKill
  3420. Routine Description:
  3421. Given a pid, starts the recursive function that kills all the pid's descendents
  3422. Arguments:
  3423. Return Value:
  3424. Revision History:
  3425. 8-4-98 Davepl Created
  3426. 2-26-01 Bretan Modified
  3427. --*/
  3428. #define MAX_TASKS 4096
  3429. BOOL CProcPage::RecursiveKill(DWORD pid)
  3430. {
  3431. BYTE* pbBuffer = NULL;
  3432. BOOL rval = TRUE;
  3433. DWORD dwTaskPid = GetCurrentProcessId();
  3434. if (IsSystemProcess(pid, NULL))
  3435. {
  3436. return FALSE;
  3437. }
  3438. if (IDYES != QuickConfirm(IDS_WARNING, IDS_KILLTREE))
  3439. {
  3440. return FALSE;
  3441. }
  3442. //
  3443. // get the task list for the system
  3444. //
  3445. pbBuffer = GetTaskListEx();
  3446. if (pbBuffer)
  3447. {
  3448. PSYSTEM_PROCESS_INFORMATION ProcessInfo = (PSYSTEM_PROCESS_INFORMATION) pbBuffer;
  3449. ULONG TotalOffset = 0;
  3450. for ( ;; ) // ever
  3451. {
  3452. if (PtrToUlong(ProcessInfo->UniqueProcessId) == pid)
  3453. {
  3454. rval = KillAllChildren(dwTaskPid, pid, pbBuffer, ProcessInfo->CreateTime);
  3455. //
  3456. // Kill the parent process if it is not task manager
  3457. //
  3458. if (pid != dwTaskPid)
  3459. {
  3460. KillProcess(pid, TRUE);
  3461. }
  3462. // We will not run into this pid again (since its unique)
  3463. // so we might as well break outta this for loop
  3464. break;
  3465. }
  3466. //
  3467. // Advance to next task
  3468. //
  3469. if (ProcessInfo->NextEntryOffset == 0)
  3470. {
  3471. break;
  3472. }
  3473. TotalOffset += ProcessInfo->NextEntryOffset;
  3474. ProcessInfo = (PSYSTEM_PROCESS_INFORMATION) &pbBuffer[ TotalOffset ];
  3475. }
  3476. }
  3477. else
  3478. {
  3479. rval = FALSE;
  3480. }
  3481. if (rval != TRUE)
  3482. {
  3483. // We failed to kill at least one of the processes
  3484. WCHAR szTitle[MAX_PATH];
  3485. WCHAR szBody[MAX_PATH];
  3486. if (0 != LoadString(g_hInstance, IDS_KILLTREEFAIL, szTitle, ARRAYSIZE(szTitle)) &&
  3487. 0 != LoadString(g_hInstance, IDS_KILLTREEFAILBODY, szBody, ARRAYSIZE(szBody)))
  3488. {
  3489. MessageBox(m_hPage, szBody, szTitle, MB_ICONERROR);
  3490. }
  3491. }
  3492. //
  3493. // Buffer allocated in call to GetTaskListEx
  3494. //
  3495. HeapFree( GetProcessHeap( ), 0, pbBuffer );
  3496. return rval;
  3497. }
  3498. /*++ CProcPage::GetTaskListEx
  3499. Routine Description:
  3500. Provides an API for getting a list of tasks running at the time of the
  3501. API call. This function uses internal NT apis and data structures. This
  3502. api is MUCH faster that the non-internal version that uses the registry.
  3503. Arguments:
  3504. dwNumTasks - maximum number of tasks that the pTask array can hold
  3505. Return Value:
  3506. Number of tasks placed into the pTask array.
  3507. --*/
  3508. BYTE* CProcPage::GetTaskListEx()
  3509. {
  3510. BYTE* pbBuffer = NULL;
  3511. NTSTATUS status;
  3512. DWORD dwBufferSize = sizeof(SYSTEM_PROCESS_INFORMATION) * 100; // start with ~100 processes
  3513. retry:
  3514. ASSERT( NULL == pbBuffer );
  3515. pbBuffer = (BYTE *) HeapAlloc( GetProcessHeap( ), 0, dwBufferSize );
  3516. if (pbBuffer == NULL)
  3517. {
  3518. return FALSE;
  3519. }
  3520. status = NtQuerySystemInformation( SystemProcessInformation
  3521. , pbBuffer
  3522. , dwBufferSize
  3523. , NULL
  3524. );
  3525. if ( status != ERROR_SUCCESS )
  3526. {
  3527. HeapFree( GetProcessHeap( ), 0, pbBuffer );
  3528. pbBuffer = NULL;
  3529. }
  3530. if (status == STATUS_INFO_LENGTH_MISMATCH) {
  3531. dwBufferSize += 8192;
  3532. goto retry;
  3533. }
  3534. if (!NT_SUCCESS(status))
  3535. {
  3536. return FALSE;
  3537. }
  3538. return pbBuffer;
  3539. }