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

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File        : MsgTracerProcess.cpp
  4. // Project     : MsgTrace
  5. // Component   : MsgTracer
  6. //---------------------------------------------------------------------------
  7. // Description : defines an attached process
  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 "psapi.h"
  21. #include "MsgTracerProcess.h"
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CMsgTracerProcess: construction
  25. /////////////////////////////////////////////////////////////////////////////
  26.  
  27. CMsgTracerProcess::CMsgTracerProcess( CMsgTracerProcessList* pOwner, DWORD dwProcessId ) :
  28.  
  29.     m_pOwner( pOwner ), m_dwProcessId( dwProcessId ), m_hProcess( NULL )
  30.  
  31. {
  32.     OpenProcess();
  33. }
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36.  
  37. CMsgTracerProcess::~CMsgTracerProcess()
  38. {
  39.     if( NULL != m_hProcess )
  40.     {
  41.         CloseHandle( m_hProcess );
  42.     }
  43.  
  44.     m_hProcess = NULL;
  45.     m_dwProcessId = 0;
  46. }
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CMsgTracerProcess: operations
  50. /////////////////////////////////////////////////////////////////////////////
  51.  
  52. BOOL CMsgTracerProcess::OpenProcess()
  53. {
  54.     m_hProcess = ::OpenProcess( PROCESS_ALL_ACCESS, FALSE, m_dwProcessId );
  55.  
  56.     if( NULL != m_hProcess )
  57.     {
  58.         HMODULE hMod;
  59.         DWORD cbNeeded;
  60.         
  61.         if( EnumProcessModules( m_hProcess, &hMod, sizeof( hMod ), &cbNeeded ) )
  62.         {
  63.             GetModuleBaseName( m_hProcess, hMod, m_sBaseName.GetBuffer( 1024 ), 1024 );
  64.             m_sBaseName.ReleaseBuffer();
  65.         }
  66.     }
  67.  
  68.     return( NULL != m_hProcess );
  69. }
  70.  
  71. /////////////////////////////////////////////////////////////////////////////
  72.  
  73. BOOL CMsgTracerProcess::AttachProcess()
  74. {
  75.     BOOL bOK = ::DebugActiveProcess( m_dwProcessId );
  76.     return( bOK );
  77. }
  78.  
  79. /////////////////////////////////////////////////////////////////////////////
  80.  
  81. BOOL CMsgTracerProcess::TerminateProcess()
  82. {
  83.     BOOL bTermed = ::TerminateProcess( m_hProcess, 0 );
  84.     return( bTermed );
  85. }
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88.  
  89. void CMsgTracerProcess::OnTerminateProcess( BOOL bAutoDelete )
  90. {
  91.     m_pOwner->Remove( this );
  92.  
  93.     if( bAutoDelete )
  94.     {
  95.         delete this;
  96.     }
  97. }
  98.  
  99. /////////////////////////////////////////////////////////////////////////////
  100. // CMsgTracerProcess: attributes
  101. /////////////////////////////////////////////////////////////////////////////
  102.  
  103. DWORD CMsgTracerProcess::GetProcessId() const
  104. {
  105.     return( m_dwProcessId );
  106. }
  107.  
  108. HANDLE CMsgTracerProcess::GetProcessHandle() const
  109. {
  110.     return( m_hProcess );
  111. }
  112.  
  113. CString CMsgTracerProcess::GetBaseName() const
  114. {
  115.     return( m_sBaseName );
  116. }
  117.  
  118. /////////////////////////////////////////////////////////////////////////////
  119. // CMsgTracerProcessList: construction
  120. /////////////////////////////////////////////////////////////////////////////
  121.  
  122. CMsgTracerProcessList::CMsgTracerProcessList()
  123. {
  124. }
  125.  
  126. /////////////////////////////////////////////////////////////////////////////
  127.  
  128. CMsgTracerProcessList::~CMsgTracerProcessList()
  129. {
  130.     TerminateAll();
  131. }
  132.  
  133. /////////////////////////////////////////////////////////////////////////////
  134.  
  135. void CMsgTracerProcessList::TerminateAll()
  136. {
  137.     while( !IsEmpty() )
  138.     {
  139.         CMsgTracerProcess* pProcess = (CMsgTracerProcess*) RemoveTail();
  140.         pProcess->TerminateProcess();
  141.  
  142.         delete pProcess;
  143.     }
  144. }
  145.  
  146. /////////////////////////////////////////////////////////////////////////////
  147.  
  148. void CMsgTracerProcessList::Remove( CMsgTracerProcess* pProcess )
  149. {
  150.     POSITION pos = CObList::Find( (CObject*) pProcess );
  151.  
  152.     if( NULL != pos )
  153.     {
  154.         RemoveAt( pos );
  155.     }
  156. }
  157.  
  158. /////////////////////////////////////////////////////////////////////////////
  159.  
  160. CMsgTracerProcess* CMsgTracerProcessList::Find( DWORD dwProcessId )
  161. {
  162.     POSITION pos = GetHeadPosition();
  163.  
  164.     while( NULL != pos )
  165.     {
  166.         CMsgTracerProcess* pProcess = (CMsgTracerProcess*) GetNext( pos );
  167.  
  168.         if( dwProcessId == pProcess->GetProcessId() )
  169.         {
  170.             return( pProcess );
  171.         }
  172.     }
  173.  
  174.     return( NULL );
  175. }
  176.  
  177. /////////////////////////////////////////////////////////////////////////////
  178.  
  179. CMsgTracerProcess* CMsgTracerProcessList::GetNext( POSITION& pos )
  180. {
  181.     return( (CMsgTracerProcess*) CObList::GetNext( pos ) );
  182. }
  183.  
  184. /////////////////////////////////////////////////////////////////////////////
  185.  
  186. POSITION CMsgTracerProcessList::Add( DWORD dwProcessId, CMsgTracerProcess** pp )
  187. {
  188.     if( NULL != Find( dwProcessId ) )
  189.     {
  190.         return( NULL );
  191.     }
  192.  
  193.     CMsgTracerProcess* pProcess = new CMsgTracerProcess( this, dwProcessId );
  194.     
  195.     if( NULL != pp )
  196.     {
  197.         *pp = pProcess;
  198.     }
  199.  
  200.     return( CObList::AddTail( pProcess ) );
  201. }
  202.