home *** CD-ROM | disk | FTP | other *** search
- /////////////////////////////////////////////////////////////////////////////
- //
- // File : AttachListCtrl.cpp
- // Project : KARLOS
- // Component : MsgTracer
- //---------------------------------------------------------------------------
- // Description :
- //
- /////////////////////////////////////////////////////////////////////////////
- //
- // SourceSafe Strings. Do not change.
- //---------------------------------------------------------------------------
- // $Author: jeskes $
- // $Date: $
- // $Revision: $
- //
- /////////////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include "resource.h"
- #include "psapi.h"
-
- #include "MsgTracerApp.h"
- #include "MsgTracerProcess.h"
-
- #include "AttachListCtrl.h"
-
- /////////////////////////////////////////////////////////////////////////////
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CAttachListCtrlItem
- /////////////////////////////////////////////////////////////////////////////
-
- CAttachListCtrlItem::CAttachListCtrlItem( DWORD idProcess ) :
- m_idProcess( idProcess ), m_sBaseName( "unknown" )
- {
- HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
- FALSE,
- m_idProcess );
- if( NULL != hProcess )
- {
- HMODULE hMod;
- DWORD cbNeeded;
-
- if( EnumProcessModules( hProcess, &hMod, sizeof( hMod ), &cbNeeded ) )
- {
- GetModuleBaseName( hProcess, hMod, m_sBaseName.GetBuffer( MAX_PATH ), MAX_PATH );
- m_sBaseName.ReleaseBuffer();
- }
-
- CloseHandle( hProcess );
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CAttachListCtrl
- /////////////////////////////////////////////////////////////////////////////
-
- BEGIN_MESSAGE_MAP(CAttachListCtrl, CListCtrl)
- //{{AFX_MSG_MAP(CAttachListCtrl)
- ON_NOTIFY_REFLECT( LVN_ITEMCHANGED, OnItemChanged )
- ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnClick)
- ON_NOTIFY_REFLECT(LVN_DELETEITEM, OnDeleteItem)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CAttachListCtrl: construction
- /////////////////////////////////////////////////////////////////////////////
-
- CAttachListCtrl::CAttachListCtrl( CAttachListCtrl& other ) :
- m_pOther( &other ), m_nLastSortCol( 0 ), m_bLastSortWasAsc( FALSE )
- {
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CAttachListCtrl: operations
- /////////////////////////////////////////////////////////////////////////////
-
- void CAttachListCtrl::PreSubclassWindow()
- {
- CString sHeader;
-
- sHeader.LoadString( IDS_HEADER_PROCESS_ID );
- InsertColumn( 0, sHeader, LVCFMT_LEFT, 70, 0 );
-
- sHeader.LoadString( IDS_HEADER_PROCESS_NAME );
- InsertColumn( 1, sHeader, LVCFMT_LEFT, 176, 1 );
-
- CListCtrl::PreSubclassWindow();
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CAttachListCtrl::Refresh()
- {
- DeleteAllItems();
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CAttachListCtrl::DeselectOther()
- {
- ASSERT( NULL != m_pOther );
-
- int nItem = m_pOther->GetNextItem( -1, LVNI_SELECTED );
-
- if( -1 != nItem )
- {
- m_pOther->SetItemState( nItem, 0, LVIS_SELECTED );
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CAttachListCtrl::AddProcess( DWORD dwProcessId )
- {
- CAttachListCtrlItem* pNewItem = new CAttachListCtrlItem( dwProcessId );
-
- CString s;
- s.Format( "0x%08X", dwProcessId );
-
- int idx = InsertItem( GetItemCount(), s );
- SetItemText( idx, 1, pNewItem->m_sBaseName );
- SetItemData( idx, (LPARAM) pNewItem );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- CAttachListCtrlItem* CAttachListCtrl::GetProcess( int idx )
- {
- return( (CAttachListCtrlItem*) GetItemData( idx ) );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- BOOL CAttachListCtrl::OnTerminate()
- {
- int idx = GetNextItem( -1, LVNI_SELECTED );
-
- if( -1 != idx )
- {
- DWORD idProcess = GetProcess( idx )->m_idProcess;
-
- if( ( 0 != idProcess ) &&
- ( IDYES == AfxMessageBox( IDS_RUSURE_2_TERMINATE_PROCES, MB_YESNO|MB_ICONQUESTION ) ) )
- {
- HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, FALSE, idProcess );
-
- if( ( NULL == hProcess ) || !TerminateProcess( hProcess, 0 ) )
- {
- CString s;
- s.Format( IDS_CANNOT_TERMINATE_PROCESS, GetLastError() );
- AfxMessageBox( s, MB_ICONSTOP );
- CloseHandle( hProcess );
- }
- else
- {
- CloseHandle( hProcess );
- return( TRUE );
- }
- }
- }
-
- return( FALSE );
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CAttachListCtrl: sorting things out
- /////////////////////////////////////////////////////////////////////////////
-
- void CAttachListCtrl::SortItems( int nCol, int nSort )
- {
- if( 0 == nSort )
- {
- if( m_nLastSortCol == nCol )
- {
- m_bLastSortWasAsc = !m_bLastSortWasAsc;
- }
- else
- {
- m_nLastSortCol = nCol;
- m_bLastSortWasAsc = TRUE;
- }
- }
- else
- {
- m_nLastSortCol = nCol;
- m_bLastSortWasAsc = ( nSort > 0 );
- }
-
- LPARAM lParamSort = MAKELPARAM( m_nLastSortCol, m_bLastSortWasAsc );
- CListCtrl::SortItems( (PFNLVCOMPARE) CompareItems, lParamSort );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- int CALLBACK CAttachListCtrl::CompareItems( CAttachListCtrlItem* p1,
- CAttachListCtrlItem* p2,
- LPARAM lParamSort )
- {
- int nResult = 0;
-
- int nCol = LOWORD( lParamSort );
- BOOL bSortAsc = HIWORD( lParamSort );
-
- if( 0 == nCol )
- {
- nResult = ( p1->m_idProcess == p2->m_idProcess ) ? 0 :
- ( p1->m_idProcess > p2->m_idProcess ) ? 1 : -1;
- }
- else if( 1 == nCol )
- {
- nResult = p1->m_sBaseName.CompareNoCase( p2->m_sBaseName );
- }
- else
- {
- ASSERT( FALSE );
- }
-
- return( bSortAsc ? nResult : -nResult );
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CAttachListCtrl: message handlers
- /////////////////////////////////////////////////////////////////////////////
-
- void CAttachListCtrl::OnItemChanged( NMHDR* pNMHDR, LRESULT* pResult )
- {
- NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
-
- DeselectOther();
-
- *pResult = 0;
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CAttachListCtrl::OnColumnClick(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
-
- SortItems( pNMListView->iSubItem );
-
- *pResult = 0;
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CAttachListCtrl::OnDeleteItem(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
-
- CAttachListCtrlItem* pItem = (CAttachListCtrlItem*) pNMListView->lParam;
-
- if( NULL != pItem )
- {
- delete pItem;
- }
-
- *pResult = 0;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CAttachedProcessesListCtrl
- /////////////////////////////////////////////////////////////////////////////
-
- BEGIN_MESSAGE_MAP(CAttachedProcessesListCtrl, CAttachListCtrl)
- //{{AFX_MSG_MAP(CAttachedProcessesListCtrl)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CAttachedProcessesListCtrl: construction
- /////////////////////////////////////////////////////////////////////////////
-
- CAttachedProcessesListCtrl::CAttachedProcessesListCtrl( CAttachListCtrl& other ) :
- CAttachListCtrl( other )
- {
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CAttachedProcessesListCtrl: operations
- /////////////////////////////////////////////////////////////////////////////
-
- void CAttachedProcessesListCtrl::Refresh()
- {
- CAttachListCtrl::Refresh();
-
- CMsgTracerProcessList* pProcessList = ThisApp().GetAttachedProcessList();
- ASSERT( NULL != pProcessList );
-
- POSITION pos = pProcessList->GetHeadPosition();
-
- while( NULL != pos )
- {
- CMsgTracerProcess* pProcess = pProcessList->GetNext( pos );
- ASSERT( NULL != pProcess );
-
- AddProcess( pProcess->GetProcessId() );
- }
-
- SortItems( 1, 1 );
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CUnattachedProcessesListCtrl
- /////////////////////////////////////////////////////////////////////////////
-
- BEGIN_MESSAGE_MAP(CUnattachedProcessesListCtrl, CAttachListCtrl)
- //{{AFX_MSG_MAP(CUnattachedProcessesListCtrl)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CUnattachedProcessesListCtrl: construction
- /////////////////////////////////////////////////////////////////////////////
-
- CUnattachedProcessesListCtrl::CUnattachedProcessesListCtrl( CAttachListCtrl& other ) :
- CAttachListCtrl( other )
- {
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CUnattachedProcessesListCtrl: operations
- /////////////////////////////////////////////////////////////////////////////
-
- void CUnattachedProcessesListCtrl::Refresh()
- {
- CAttachListCtrl::Refresh();
-
- // Get the list of process IDs
-
- DWORD aProcesses[ 1024 ];
- DWORD cbNeeded = 0;
-
- if( !EnumProcesses( aProcesses, sizeof( aProcesses ), &cbNeeded ) )
- {
- return;
- }
-
- DWORD cProcesses = cbNeeded / sizeof( DWORD );
-
- for( DWORD idx = 0; idx < cProcesses; idx++ )
- {
- if( !ThisApp().IsAttached( aProcesses[ idx ] ) )
- {
- AddProcess( aProcesses[ idx ] );
- }
- }
-
- SortItems( 1, 1 );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- BOOL CUnattachedProcessesListCtrl::OnAttach()
- {
- int idx = GetNextItem( -1, LVNI_SELECTED );
-
- if( ( -1 != idx ) &&
- ( IDYES == AfxMessageBox( IDS_ATTACH_WARNING, MB_YESNO|MB_ICONQUESTION ) ) )
- {
- DWORD idProcess = GetProcess( idx )->m_idProcess;
- ThisApp().AttachProcess( idProcess, TRUE );
- return( TRUE );
- }
-
- return( FALSE );
- }
-