home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / MSGTRACE.ZIP / MyProjects / MsgTrace / MsgTracer / AttachListCtrl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-30  |  10.1 KB  |  377 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File        : AttachListCtrl.cpp
  4. // Project     : KARLOS
  5. // Component   : MsgTracer
  6. //---------------------------------------------------------------------------
  7. // Description : 
  8. //
  9. /////////////////////////////////////////////////////////////////////////////
  10. //
  11. // SourceSafe Strings. Do not change.
  12. //---------------------------------------------------------------------------
  13. // $Author: jeskes $
  14. // $Date: $
  15. // $Revision: $
  16. //
  17. /////////////////////////////////////////////////////////////////////////////
  18.  
  19. #include "stdafx.h"
  20. #include "resource.h"
  21. #include "psapi.h"
  22.  
  23. #include "MsgTracerApp.h"
  24. #include "MsgTracerProcess.h"
  25.  
  26. #include "AttachListCtrl.h"
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29.  
  30. #ifdef _DEBUG
  31. #define new DEBUG_NEW
  32. #undef THIS_FILE
  33. static char THIS_FILE[] = __FILE__;
  34. #endif
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CAttachListCtrlItem
  38. /////////////////////////////////////////////////////////////////////////////
  39.  
  40. CAttachListCtrlItem::CAttachListCtrlItem( DWORD idProcess ) : 
  41.     m_idProcess( idProcess ), m_sBaseName( "unknown" )
  42. {
  43.     HANDLE hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
  44.                                     FALSE, 
  45.                                     m_idProcess );
  46.     if( NULL != hProcess )
  47.     {
  48.         HMODULE hMod;
  49.         DWORD cbNeeded;
  50.         
  51.         if( EnumProcessModules( hProcess, &hMod, sizeof( hMod ), &cbNeeded ) )
  52.         {
  53.             GetModuleBaseName( hProcess, hMod, m_sBaseName.GetBuffer( MAX_PATH ), MAX_PATH );
  54.             m_sBaseName.ReleaseBuffer();
  55.         }
  56.     
  57.         CloseHandle( hProcess );
  58.     }
  59. }
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CAttachListCtrl
  63. /////////////////////////////////////////////////////////////////////////////
  64.  
  65. BEGIN_MESSAGE_MAP(CAttachListCtrl, CListCtrl)
  66.     //{{AFX_MSG_MAP(CAttachListCtrl)
  67.     ON_NOTIFY_REFLECT( LVN_ITEMCHANGED, OnItemChanged )
  68.     ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnClick)
  69.     ON_NOTIFY_REFLECT(LVN_DELETEITEM, OnDeleteItem)
  70.     //}}AFX_MSG_MAP
  71. END_MESSAGE_MAP()
  72.  
  73. /////////////////////////////////////////////////////////////////////////////
  74. // CAttachListCtrl: construction
  75. /////////////////////////////////////////////////////////////////////////////
  76.  
  77. CAttachListCtrl::CAttachListCtrl( CAttachListCtrl& other ) : 
  78.     m_pOther( &other ), m_nLastSortCol( 0 ), m_bLastSortWasAsc( FALSE )
  79. {
  80. }
  81.  
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CAttachListCtrl: operations
  84. /////////////////////////////////////////////////////////////////////////////
  85.  
  86. void CAttachListCtrl::PreSubclassWindow()
  87. {
  88.     CString sHeader;
  89.  
  90.     sHeader.LoadString( IDS_HEADER_PROCESS_ID );
  91.     InsertColumn( 0, sHeader, LVCFMT_LEFT, 70, 0 );
  92.  
  93.     sHeader.LoadString( IDS_HEADER_PROCESS_NAME );
  94.     InsertColumn( 1, sHeader, LVCFMT_LEFT, 176, 1 );
  95.  
  96.     CListCtrl::PreSubclassWindow();
  97. }
  98.  
  99. /////////////////////////////////////////////////////////////////////////////
  100.  
  101. void CAttachListCtrl::Refresh()
  102. {
  103.     DeleteAllItems();
  104. }
  105.  
  106. /////////////////////////////////////////////////////////////////////////////
  107.  
  108. void CAttachListCtrl::DeselectOther()
  109. {
  110.     ASSERT( NULL != m_pOther );
  111.  
  112.     int nItem = m_pOther->GetNextItem( -1, LVNI_SELECTED );
  113.  
  114.     if( -1 != nItem )
  115.     {
  116.         m_pOther->SetItemState( nItem, 0, LVIS_SELECTED );
  117.     }
  118. }
  119.     
  120. /////////////////////////////////////////////////////////////////////////////
  121.  
  122. void CAttachListCtrl::AddProcess( DWORD dwProcessId )
  123. {
  124.     CAttachListCtrlItem* pNewItem = new CAttachListCtrlItem( dwProcessId );
  125.  
  126.     CString s;
  127.     s.Format( "0x%08X", dwProcessId );
  128.  
  129.     int idx = InsertItem( GetItemCount(), s );
  130.     SetItemText( idx, 1, pNewItem->m_sBaseName );
  131.     SetItemData( idx, (LPARAM) pNewItem );
  132. }
  133.  
  134. /////////////////////////////////////////////////////////////////////////////
  135.  
  136. CAttachListCtrlItem* CAttachListCtrl::GetProcess( int idx )
  137. {
  138.     return( (CAttachListCtrlItem*) GetItemData( idx ) );
  139. }
  140.  
  141. /////////////////////////////////////////////////////////////////////////////
  142.  
  143. BOOL CAttachListCtrl::OnTerminate() 
  144. {
  145.     int idx = GetNextItem( -1, LVNI_SELECTED );
  146.  
  147.     if( -1 != idx )
  148.     {
  149.         DWORD idProcess = GetProcess( idx )->m_idProcess;
  150.  
  151.         if( ( 0 != idProcess ) &&
  152.             ( IDYES == AfxMessageBox( IDS_RUSURE_2_TERMINATE_PROCES, MB_YESNO|MB_ICONQUESTION ) ) )
  153.         {
  154.             HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, FALSE, idProcess );
  155.  
  156.             if( ( NULL == hProcess ) || !TerminateProcess( hProcess, 0 ) )
  157.             {
  158.                 CString s;
  159.                 s.Format( IDS_CANNOT_TERMINATE_PROCESS, GetLastError() );
  160.                 AfxMessageBox( s, MB_ICONSTOP );
  161.                 CloseHandle( hProcess );
  162.             }
  163.             else
  164.             {
  165.                 CloseHandle( hProcess );
  166.                 return( TRUE );
  167.             }
  168.         }
  169.     }
  170.  
  171.     return( FALSE );
  172. }
  173.  
  174. /////////////////////////////////////////////////////////////////////////////
  175. // CAttachListCtrl: sorting things out
  176. /////////////////////////////////////////////////////////////////////////////
  177.  
  178. void CAttachListCtrl::SortItems( int nCol, int nSort )
  179. {
  180.     if( 0 == nSort )
  181.     {
  182.         if( m_nLastSortCol == nCol )
  183.         {
  184.             m_bLastSortWasAsc = !m_bLastSortWasAsc;
  185.         }
  186.         else
  187.         {
  188.             m_nLastSortCol = nCol;
  189.             m_bLastSortWasAsc = TRUE;
  190.         }
  191.     }
  192.     else
  193.     {
  194.         m_nLastSortCol = nCol;
  195.         m_bLastSortWasAsc = ( nSort > 0 );
  196.     }
  197.             
  198.     LPARAM lParamSort = MAKELPARAM( m_nLastSortCol, m_bLastSortWasAsc );
  199.     CListCtrl::SortItems( (PFNLVCOMPARE) CompareItems, lParamSort );
  200. }
  201.  
  202. /////////////////////////////////////////////////////////////////////////////
  203.  
  204. int CALLBACK CAttachListCtrl::CompareItems( CAttachListCtrlItem* p1, 
  205.                                             CAttachListCtrlItem* p2, 
  206.                                             LPARAM lParamSort )
  207. {
  208.     int nResult = 0;
  209.  
  210.     int nCol = LOWORD( lParamSort );
  211.     BOOL bSortAsc = HIWORD( lParamSort );
  212.  
  213.     if( 0 == nCol )
  214.     {
  215.         nResult = ( p1->m_idProcess == p2->m_idProcess ) ? 0 :
  216.                   ( p1->m_idProcess > p2->m_idProcess ) ? 1 : -1;
  217.     }
  218.     else if( 1 == nCol )
  219.     {
  220.         nResult = p1->m_sBaseName.CompareNoCase( p2->m_sBaseName );
  221.     }
  222.     else
  223.     {
  224.         ASSERT( FALSE );
  225.     }
  226.  
  227.     return( bSortAsc ? nResult : -nResult );
  228. }
  229.  
  230. /////////////////////////////////////////////////////////////////////////////
  231. // CAttachListCtrl: message handlers
  232. /////////////////////////////////////////////////////////////////////////////
  233.  
  234. void CAttachListCtrl::OnItemChanged( NMHDR* pNMHDR, LRESULT* pResult )
  235. {
  236.     NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  237.  
  238.     DeselectOther();
  239.     
  240.     *pResult = 0;
  241. }
  242.  
  243. /////////////////////////////////////////////////////////////////////////////
  244.  
  245. void CAttachListCtrl::OnColumnClick(NMHDR* pNMHDR, LRESULT* pResult) 
  246. {
  247.     NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  248.  
  249.     SortItems( pNMListView->iSubItem );
  250.         
  251.     *pResult = 0;
  252. }
  253.  
  254. /////////////////////////////////////////////////////////////////////////////
  255.  
  256. void CAttachListCtrl::OnDeleteItem(NMHDR* pNMHDR, LRESULT* pResult) 
  257. {
  258.     NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  259.  
  260.     CAttachListCtrlItem* pItem = (CAttachListCtrlItem*) pNMListView->lParam;
  261.     
  262.     if( NULL != pItem )
  263.     {
  264.         delete pItem;
  265.     }
  266.     
  267.     *pResult = 0;
  268. }
  269.  
  270. /////////////////////////////////////////////////////////////////////////////
  271. // CAttachedProcessesListCtrl
  272. /////////////////////////////////////////////////////////////////////////////
  273.  
  274. BEGIN_MESSAGE_MAP(CAttachedProcessesListCtrl, CAttachListCtrl)
  275.     //{{AFX_MSG_MAP(CAttachedProcessesListCtrl)
  276.     //}}AFX_MSG_MAP
  277. END_MESSAGE_MAP()
  278.  
  279. /////////////////////////////////////////////////////////////////////////////
  280. // CAttachedProcessesListCtrl: construction
  281. /////////////////////////////////////////////////////////////////////////////
  282.  
  283. CAttachedProcessesListCtrl::CAttachedProcessesListCtrl( CAttachListCtrl& other ) :
  284.     CAttachListCtrl( other )
  285. {
  286. }
  287.  
  288. /////////////////////////////////////////////////////////////////////////////
  289. // CAttachedProcessesListCtrl: operations
  290. /////////////////////////////////////////////////////////////////////////////
  291.  
  292. void CAttachedProcessesListCtrl::Refresh()
  293. {
  294.     CAttachListCtrl::Refresh();
  295.  
  296.     CMsgTracerProcessList* pProcessList = ThisApp().GetAttachedProcessList();
  297.     ASSERT( NULL != pProcessList );
  298.  
  299.     POSITION pos = pProcessList->GetHeadPosition();
  300.  
  301.     while( NULL != pos )
  302.     {
  303.         CMsgTracerProcess* pProcess = pProcessList->GetNext( pos );
  304.         ASSERT( NULL != pProcess );
  305.  
  306.         AddProcess( pProcess->GetProcessId() );
  307.     }
  308.  
  309.     SortItems( 1, 1 );
  310. }
  311.  
  312. /////////////////////////////////////////////////////////////////////////////
  313. // CUnattachedProcessesListCtrl
  314. /////////////////////////////////////////////////////////////////////////////
  315.  
  316. BEGIN_MESSAGE_MAP(CUnattachedProcessesListCtrl, CAttachListCtrl)
  317.     //{{AFX_MSG_MAP(CUnattachedProcessesListCtrl)
  318.     //}}AFX_MSG_MAP
  319. END_MESSAGE_MAP()
  320.  
  321. /////////////////////////////////////////////////////////////////////////////
  322. // CUnattachedProcessesListCtrl: construction
  323. /////////////////////////////////////////////////////////////////////////////
  324.  
  325. CUnattachedProcessesListCtrl::CUnattachedProcessesListCtrl( CAttachListCtrl& other ) :
  326.     CAttachListCtrl( other )
  327. {
  328. }
  329.  
  330. /////////////////////////////////////////////////////////////////////////////
  331. // CUnattachedProcessesListCtrl: operations
  332. /////////////////////////////////////////////////////////////////////////////
  333.  
  334. void CUnattachedProcessesListCtrl::Refresh()
  335. {
  336.     CAttachListCtrl::Refresh();
  337.  
  338.     // Get the list of process IDs
  339.  
  340.     DWORD aProcesses[ 1024 ];
  341.     DWORD cbNeeded = 0;
  342.  
  343.     if( !EnumProcesses( aProcesses, sizeof( aProcesses ), &cbNeeded ) )
  344.     {
  345.         return;
  346.     }
  347.     
  348.     DWORD cProcesses = cbNeeded / sizeof( DWORD );
  349.  
  350.     for( DWORD idx = 0; idx < cProcesses; idx++ )
  351.     {
  352.         if( !ThisApp().IsAttached( aProcesses[ idx ] ) )
  353.         {
  354.             AddProcess( aProcesses[ idx ] );
  355.         }
  356.     }
  357.  
  358.     SortItems( 1, 1 );
  359. }
  360.  
  361. /////////////////////////////////////////////////////////////////////////////
  362.  
  363. BOOL CUnattachedProcessesListCtrl::OnAttach() 
  364. {
  365.     int idx = GetNextItem( -1, LVNI_SELECTED );
  366.  
  367.     if( ( -1 != idx ) &&
  368.         ( IDYES == AfxMessageBox( IDS_ATTACH_WARNING, MB_YESNO|MB_ICONQUESTION ) ) )
  369.     {
  370.         DWORD idProcess = GetProcess( idx )->m_idProcess;
  371.         ThisApp().AttachProcess( idProcess, TRUE );
  372.         return( TRUE );
  373.     }
  374.  
  375.     return( FALSE );
  376. }
  377.