home *** CD-ROM | disk | FTP | other *** search
- /////////////////////////////////////////////////////////////////////////////
- //
- // File : MsgTracerProcess.cpp
- // Project : MsgTrace
- // Component : MsgTracer
- //---------------------------------------------------------------------------
- // Description : defines an attached process
- //
- /////////////////////////////////////////////////////////////////////////////
- //
- // SourceSafe Strings. Do not change.
- //---------------------------------------------------------------------------
- // $Author: jeskes $
- // $Date: $
- // $Revision: $
- //
- /////////////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include "psapi.h"
- #include "MsgTracerProcess.h"
-
- /////////////////////////////////////////////////////////////////////////////
- // CMsgTracerProcess: construction
- /////////////////////////////////////////////////////////////////////////////
-
- CMsgTracerProcess::CMsgTracerProcess( CMsgTracerProcessList* pOwner, DWORD dwProcessId ) :
-
- m_pOwner( pOwner ), m_dwProcessId( dwProcessId ), m_hProcess( NULL )
-
- {
- OpenProcess();
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- CMsgTracerProcess::~CMsgTracerProcess()
- {
- if( NULL != m_hProcess )
- {
- CloseHandle( m_hProcess );
- }
-
- m_hProcess = NULL;
- m_dwProcessId = 0;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMsgTracerProcess: operations
- /////////////////////////////////////////////////////////////////////////////
-
- BOOL CMsgTracerProcess::OpenProcess()
- {
- m_hProcess = ::OpenProcess( PROCESS_ALL_ACCESS, FALSE, m_dwProcessId );
-
- if( NULL != m_hProcess )
- {
- HMODULE hMod;
- DWORD cbNeeded;
-
- if( EnumProcessModules( m_hProcess, &hMod, sizeof( hMod ), &cbNeeded ) )
- {
- GetModuleBaseName( m_hProcess, hMod, m_sBaseName.GetBuffer( 1024 ), 1024 );
- m_sBaseName.ReleaseBuffer();
- }
- }
-
- return( NULL != m_hProcess );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- BOOL CMsgTracerProcess::AttachProcess()
- {
- BOOL bOK = ::DebugActiveProcess( m_dwProcessId );
- return( bOK );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- BOOL CMsgTracerProcess::TerminateProcess()
- {
- BOOL bTermed = ::TerminateProcess( m_hProcess, 0 );
- return( bTermed );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerProcess::OnTerminateProcess( BOOL bAutoDelete )
- {
- m_pOwner->Remove( this );
-
- if( bAutoDelete )
- {
- delete this;
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMsgTracerProcess: attributes
- /////////////////////////////////////////////////////////////////////////////
-
- DWORD CMsgTracerProcess::GetProcessId() const
- {
- return( m_dwProcessId );
- }
-
- HANDLE CMsgTracerProcess::GetProcessHandle() const
- {
- return( m_hProcess );
- }
-
- CString CMsgTracerProcess::GetBaseName() const
- {
- return( m_sBaseName );
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMsgTracerProcessList: construction
- /////////////////////////////////////////////////////////////////////////////
-
- CMsgTracerProcessList::CMsgTracerProcessList()
- {
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- CMsgTracerProcessList::~CMsgTracerProcessList()
- {
- TerminateAll();
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerProcessList::TerminateAll()
- {
- while( !IsEmpty() )
- {
- CMsgTracerProcess* pProcess = (CMsgTracerProcess*) RemoveTail();
- pProcess->TerminateProcess();
-
- delete pProcess;
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void CMsgTracerProcessList::Remove( CMsgTracerProcess* pProcess )
- {
- POSITION pos = CObList::Find( (CObject*) pProcess );
-
- if( NULL != pos )
- {
- RemoveAt( pos );
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- CMsgTracerProcess* CMsgTracerProcessList::Find( DWORD dwProcessId )
- {
- POSITION pos = GetHeadPosition();
-
- while( NULL != pos )
- {
- CMsgTracerProcess* pProcess = (CMsgTracerProcess*) GetNext( pos );
-
- if( dwProcessId == pProcess->GetProcessId() )
- {
- return( pProcess );
- }
- }
-
- return( NULL );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- CMsgTracerProcess* CMsgTracerProcessList::GetNext( POSITION& pos )
- {
- return( (CMsgTracerProcess*) CObList::GetNext( pos ) );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- POSITION CMsgTracerProcessList::Add( DWORD dwProcessId, CMsgTracerProcess** pp )
- {
- if( NULL != Find( dwProcessId ) )
- {
- return( NULL );
- }
-
- CMsgTracerProcess* pProcess = new CMsgTracerProcess( this, dwProcessId );
-
- if( NULL != pp )
- {
- *pp = pProcess;
- }
-
- return( CObList::AddTail( pProcess ) );
- }
-