DumontEXE 0.0.1
ModuleInstance.cpp
00001 //---------------------------------------------------------------------------
00002 // ModuleInstance.cpp
00003 //
00004 // Ivo Ivanov
00005 // ver 1.02
00006 // 05 September 2001
00007 //---------------------------------------------------------------------------
00008 #include "ModuleInstance.h"
00009 
00010 //---------------------------------------------------------------------------
00011 // IsToolHelpSupported
00012 //
00013 // Checks whether Tool Help library is supported by the current platform
00014 //---------------------------------------------------------------------------
00015 static BOOL IsToolHelpSupported()
00016 {
00017   /*
00018   ** Default to false
00019   **
00020   */
00021   BOOL bResult(FALSE);
00022 
00023   /*
00024   ** Try to load the KERNEL32.dll library
00025   **
00026   */
00027   HMODULE hModToolHelp = ::LoadLibrary( L"KERNEL32.DLL" );
00028 
00029   /*
00030   ** If we were able to get the kernel library loaded, then
00031   **  proceed with the analysis
00032   **
00033   */
00034   if( NULL != hModToolHelp )
00035   {
00036     /*
00037     ** Try to get the proc address of the CreateToolHelp32Snapshot
00038     **
00039     */
00040     PROC pfnCreateToolhelp32Snapshot = 
00041       ::GetProcAddress( hModToolHelp, "CreateToolhelp32Snapshot" );
00042 
00043     /*
00044     ** Cache the result of attempting to get that proc address.
00045     **
00046     */
00047     bResult = (NULL != pfnCreateToolhelp32Snapshot);
00048 
00049     /*
00050     ** Free the library that we loaded.
00051     **
00052     */
00053     ::FreeLibrary(hModToolHelp);
00054 
00055   }
00056 
00057   /*
00058   ** Return the results of our probe.
00059   **
00060   */
00061   return bResult;
00062 
00063 } // endstatic BOOL IsToolHelpSupported()
00064 
00065 //---------------------------------------------------------------------------
00066 // IsPsapiSupported
00067 //
00068 // Checks whether PSAPI dll has been installed
00069 //---------------------------------------------------------------------------
00070 static BOOL IsPsapiSupported()
00071 {
00072   /*
00073   ** Try to load the psapi library
00074   **
00075   */
00076   HMODULE hModPSAPI = ::LoadLibrary(L"PSAPI.DLL");
00077 
00078   /*
00079   ** See if the library was loadable.  Cache the result.
00080   **
00081   */
00082   BOOL bResult = ( NULL != hModPSAPI );
00083 
00084   /*
00085   ** If the module was able to be loaded then free it
00086   **
00087   */
00088   if( NULL != hModPSAPI )
00089     ::FreeLibrary(hModPSAPI);
00090 
00091   /*
00092   ** Return the result if the library could be loaded.
00093   **
00094   */
00095   return bResult;
00096 
00097 } // endstatic BOOL IsPsapiSupported()
00098 
00099 //---------------------------------------------------------------------------
00100 //
00101 // class CElements
00102 //
00103 //---------------------------------------------------------------------------
00104 CElements::CElements():
00105    CModuleList()
00106 {
00107 
00108 }
00109 
00110 CElements::~CElements()
00111 {
00112 
00113 }
00114 //---------------------------------------------------------------------------
00115 //
00116 // class CLibHandler
00117 //
00118 //---------------------------------------------------------------------------
00119 CLibHandler::CLibHandler(CRunningProcesses* pProcesses,QObject *parent):
00120   QObject(parent),
00121   m_pProcesses(pProcesses)
00122 {
00123 
00124 }
00125 
00126 CLibHandler::~CLibHandler()
00127 {
00128 
00129 }
00130 
00131 
00132 //---------------------------------------------------------------------------
00133 //
00134 // class CTaskManager
00135 //
00136 //---------------------------------------------------------------------------
00137 CTaskManager::CTaskManager( QObject * parent )
00138 :
00139   QObject(parent),
00140   m_pLibHandler( NULL )
00141 {
00142   /*
00143   ** Initialize our running processes object
00144   **
00145   */
00146   m_pProcesses = new CRunningProcesses();
00147 
00148   /*
00149   ** Check for Psapi support.  This is preferred
00150   **
00151   */
00152   if( IsPsapiSupported() )
00153   {
00154     /*
00155     ** If we have psapi support, then install that handler
00156     **
00157     */
00158     m_pLibHandler = new CPsapiHandler( m_pProcesses, this );
00159 
00160     qDebug( "::CTaskManager - using CPsapiHandler" );
00161 
00162   }
00163   else
00164   {
00165     /*
00166     ** If we have toolhelp support, then install that handler.
00167     **
00168     */
00169     if( IsToolHelpSupported() )
00170     {
00171       m_pLibHandler = new CToolhelpHandler( m_pProcesses, this );
00172 
00173       qDebug( "::CTaskManager - using CToolhelpHandler" );
00174     }
00175     else
00176     {
00177       qDebug( "::CTaskManager - no handlers installed" );
00178     }
00179   }
00180 }
00181 
00182 CTaskManager::~CTaskManager()
00183 {
00184   /*
00185   ** Cleanup
00186   **
00187   */
00188   delete m_pLibHandler;
00189   delete m_pProcesses;
00190 }
00191 
00192 // Populate the module list using PSAPI or ToolHlp32
00193 BOOL CTaskManager::Populate()
00194 {
00195   /*
00196   ** Release any processes already in the cache
00197   **
00198   */
00199   m_pProcesses->ReleaseAll();
00200 
00201   /*
00202   ** Repopulate everything
00203   **
00204   */
00205   return( m_pLibHandler->PopulateProcesses() );
00206 
00207 } // endBOOL CTaskManager::Populate()
00208 
00209 
00210 // Returns a number of currently loaded processes
00211 DWORD CTaskManager::GetProcessCount() const
00212 {
00213   return( m_pProcesses->GetCount() );
00214 }
00215 
00216 
00217 // Returns a process from the its container
00218 CExeModuleInstance * CTaskManager::GetProcessByIndex( DWORD dwIndex )
00219 {
00220   return( static_cast<CExeModuleInstance*>(m_pProcesses->GetModule(dwIndex)) );
00221 }
00222 
00223 // Terminate a running process safely
00224 BOOL CTaskManager::SafeTerminateProcess
00225 (
00226   HANDLE hProcess, 
00227   UINT   uExitCode,
00228   DWORD  dwTimeout
00229   )
00230 {
00231   DWORD dwTID, dwCode, dwErr = 0;
00232   HANDLE hProcessDup = INVALID_HANDLE_VALUE;
00233   HANDLE hRT = NULL;
00234   HINSTANCE hKernel = GetModuleHandle(L"Kernel32");
00235   BOOL bSuccess = FALSE;
00236 
00237   BOOL bDup = DuplicateHandle
00238   (
00239     GetCurrentProcess(),
00240     hProcess,
00241     GetCurrentProcess(),
00242     &hProcessDup,
00243     PROCESS_ALL_ACCESS,
00244     FALSE,
00245     0
00246   );
00247 
00248   // Detect the special case where the process is 
00249   // already dead...
00250   if( GetExitCodeProcess((bDup) ? hProcessDup : hProcess, &dwCode) && 
00251        (dwCode == STILL_ACTIVE) ) 
00252   {
00253     FARPROC pfnExitProc;
00254        
00255     pfnExitProc = GetProcAddress(hKernel, "ExitProcess");
00256 
00257     hRT = CreateRemoteThread
00258     (
00259       (bDup) ? hProcessDup : hProcess, 
00260       NULL, 
00261       0, 
00262       (LPTHREAD_START_ROUTINE)pfnExitProc,
00263       (PVOID)uExitCode, 0, &dwTID
00264     );
00265 
00266     if ( hRT == NULL )
00267       dwErr = GetLastError();
00268   }
00269   else
00270   {
00271     dwErr = ERROR_PROCESS_ABORTED;
00272   }
00273 
00274 
00275   if( hRT )
00276   {
00277     // Must wait process to terminate to 
00278     // guarantee that it has exited...
00279     DWORD dwResult = WaitForSingleObject(
00280     (bDup) ? hProcessDup : hProcess,  dwTimeout);
00281 
00282     bSuccess = dwResult == WAIT_OBJECT_0; 
00283 
00284     if (!bSuccess)
00285       dwErr = ERROR_PROCESS_ABORTED;
00286 
00287 
00288     CloseHandle(hRT);
00289   }
00290 
00291   if ( bDup )
00292     CloseHandle(hProcessDup);
00293 
00294   if ( !bSuccess )
00295     SetLastError(dwErr);
00296 
00297   return bSuccess;
00298 }
00299 
00300 //---------------------------------------------------------------------------
00301 // class CLoadedModules
00302 //---------------------------------------------------------------------------
00303 CLoadedModules::CLoadedModules(DWORD dwProcessId):
00304   CElements(),
00305   m_dwProcessId(dwProcessId)
00306 {
00307 
00308 }
00309 
00310 CLoadedModules::~CLoadedModules()
00311 {
00312 
00313 }
00314 
00315 //---------------------------------------------------------------------------
00316 //
00317 // class CRunningProcesses
00318 //
00319 //---------------------------------------------------------------------------
00320 CRunningProcesses::CRunningProcesses():
00321   CElements()
00322 {
00323 
00324 }
00325 
00326 CRunningProcesses::~CRunningProcesses()
00327 {
00328 
00329 }
00330 
00331 //---------------------------------------------------------------------------
00332 //
00333 // class CPsapiHandler
00334 //
00335 //---------------------------------------------------------------------------
00336 CPsapiHandler::CPsapiHandler( CRunningProcesses* pProcesses, QObject * parent ):
00337   CLibHandler(pProcesses,parent),
00338 //  m_hModPSAPI(NULL),
00339   m_pfnEnumProcesses(NULL),
00340   m_pfnEnumProcessModules(NULL),
00341   m_pfnGetModuleFileNameExA(NULL)
00342 {
00343 
00344 }
00345 
00346 CPsapiHandler::~CPsapiHandler()
00347 {
00348   Finalize();
00349 }
00350 
00351 //---------------------------------------------------------------------------
00352 // Initialize
00353 //
00354 //---------------------------------------------------------------------------
00355 BOOL CPsapiHandler::Initialize()
00356 {
00357   BOOL bResult = FALSE;
00358 
00359   /*
00360   ** Get to the 3 functions in PSAPI.DLL dynamically.  We can't
00361   ** be sure that PSAPI.DLL has been installed
00362   */
00363 //  if( NULL == m_hModPSAPI )
00364 //     m_hModPSAPI = ::LoadLibrary(L"PSAPI.DLL");
00365 
00366   m_library = new QLibrary( "PSAPI", this );
00367 
00368 //  if( NULL != m_hModPSAPI )
00369 //  {
00370 //    m_pfnEnumProcesses = (PFNENUMPROCESSES)
00371 //      ::GetProcAddress(m_hModPSAPI,"EnumProcesses");
00372 //
00373 //    m_pfnEnumProcessModules = (PFNENUMPROCESSMODULES)
00374 //      ::GetProcAddress(m_hModPSAPI, "EnumProcessModules");
00375 //
00376 //    m_pfnGetModuleFileNameExA = (PFNGETMODULEFILENAMEEXA)
00377 //      ::GetProcAddress(m_hModPSAPI, "GetModuleFileNameExA");
00378 //
00379 //  }
00380 
00381   if( m_library )
00382   {
00383     m_pfnEnumProcesses = (PFNENUMPROCESSES)
00384       m_library-> resolve( "EnumProcesses" );
00385 
00386     m_pfnEnumProcessModules = (PFNENUMPROCESSMODULES)
00387       m_library-> resolve( "EnumProcessModules");
00388 
00389     m_pfnGetModuleFileNameExA = (PFNGETMODULEFILENAMEEXA)
00390       m_library-> resolve( "GetModuleFileNameExA");
00391 
00392   }
00393 
00394   /*
00395   ** Calculate the result if all three functions could be
00396   **  loaded
00397   **
00398   */
00399   bResult = 
00400     m_pfnEnumProcesses         &&
00401     m_pfnEnumProcessModules    &&
00402     m_pfnGetModuleFileNameExA
00403     ;
00404 
00405   /*
00406   ** Return the result of the load.
00407   **
00408   */
00409   return bResult;
00410 
00411 }
00412 
00413 //---------------------------------------------------------------------------
00414 // Finalize
00415 //
00416 //---------------------------------------------------------------------------
00417 void CPsapiHandler::Finalize()
00418 {
00419 //  if( NULL != m_hModPSAPI )
00420 //    ::FreeLibrary(m_hModPSAPI);
00421 }
00422 
00423 //---------------------------------------------------------------------------
00424 // PopulateModules
00425 //
00426 // Populate the module list using PSAPI
00427 //---------------------------------------------------------------------------
00428 BOOL CPsapiHandler::PopulateModules( CModuleInstance* pProcess )
00429 {
00430   BOOL bResult = TRUE;
00431 
00432   CModuleInstance  *pDllModuleInstance = NULL;
00433 
00434   if( TRUE == Initialize() )
00435   {
00436     DWORD pidArray[1024];
00437     DWORD cbNeeded;
00438     DWORD nProcesses;
00439 
00440     /*
00441     ** EnumProcesses returns an array with process IDs
00442     */
00443     if( m_pfnEnumProcesses(pidArray, sizeof(pidArray), &cbNeeded) )
00444     {
00445       /*
00446       ** Determine number of processes
00447       */
00448       nProcesses = cbNeeded / sizeof(DWORD);
00449 
00450       /*
00451       ** Release the container
00452       */
00453       pProcess->ReleaseModules();
00454 
00455       for( DWORD i = 0; i < nProcesses; i++ )
00456       {
00457         HMODULE hModuleArray[1024];
00458         HANDLE hProcess;
00459         DWORD pid = pidArray[i];
00460         DWORD nModules;
00461 
00462         /*
00463         ** Let's open the process
00464         */
00465         hProcess = ::OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid );
00466 
00467         if (!hProcess)
00468           continue;
00469 
00470         if (static_cast<CExeModuleInstance*>(pProcess)->Get_ProcessId() != pid)
00471         {
00472           ::CloseHandle(hProcess);
00473           continue;
00474         }
00475 
00476         /*
00477         ** EnumProcessModules function retrieves a handle for 
00478         ** each module in the specified process.
00479         */
00480         if(!m_pfnEnumProcessModules( hProcess, hModuleArray, sizeof(hModuleArray), &cbNeeded) )
00481         {
00482           ::CloseHandle(hProcess);
00483           continue;
00484         }
00485 
00486         /*
00487         ** Calculate number of modules in the process
00488         */
00489         nModules = cbNeeded / sizeof(hModuleArray[0]);
00490 
00491         for (DWORD j = 0; j < nModules; j++)
00492         {
00493           HMODULE hModule = hModuleArray[j];
00494           char szModuleName[MAX_PATH];
00495 
00496           m_pfnGetModuleFileNameExA( hProcess, hModule, szModuleName, sizeof(szModuleName) );
00497 
00498           if (0 == j)   // First module is the EXE.  
00499           {
00500             // Do nothing.
00501           } // if
00502           else    // Not the first module.  It's a DLL
00503           {
00504             pDllModuleInstance = new CModuleInstance( szModuleName, hModule );
00505             pProcess->AddModule(pDllModuleInstance);
00506           } // else
00507 
00508         } // for
00509 
00510         ::CloseHandle(hProcess);    // We're done with this process handle
00511 
00512       } // for
00513 
00514       bResult = TRUE;
00515 
00516     } // if
00517     else
00518     {
00519        bResult = FALSE;
00520     }
00521   } // if 
00522   else
00523   {
00524     bResult = FALSE;
00525   }
00526 
00527    return bResult;
00528 }
00529 
00530 
00531 //---------------------------------------------------------------------------
00532 // PopulateProcesses
00533 //
00534 // Populate the process list using PSAPI
00535 //---------------------------------------------------------------------------
00536 
00537 BOOL CPsapiHandler::PopulateProcesses()
00538 {
00539   BOOL   bResult = TRUE;
00540   CExeModuleInstance* pProcessInfo;
00541 
00542   if( TRUE == Initialize() )
00543   {
00544     DWORD pidArray[1024];
00545     DWORD cbNeeded;
00546     DWORD nProcesses;
00547     
00548     if (m_pfnEnumProcesses(pidArray, sizeof(pidArray), &cbNeeded))
00549     {
00550       // Determine number of processes
00551       nProcesses = cbNeeded / sizeof(DWORD);  
00552       m_pProcesses->ReleaseAll();
00553       for (DWORD i = 0; i < nProcesses; i++)
00554       {
00555         HMODULE hModuleArray[1024];
00556         HANDLE hProcess;
00557         DWORD pid = pidArray[i];
00558         DWORD nModules;
00559         hProcess = OpenProcess(
00560            PROCESS_QUERY_INFORMATION |   PROCESS_VM_READ,
00561            FALSE, pid);
00562 
00563         if (!hProcess)
00564            continue;
00565 
00566         if (!m_pfnEnumProcessModules(hProcess, hModuleArray,
00567                              sizeof(hModuleArray), &cbNeeded))
00568         {
00569            ::CloseHandle(hProcess);
00570            continue;
00571         }
00572 
00573         // Calculate number of modules in the process
00574         nModules = cbNeeded / sizeof(hModuleArray[0]);
00575 
00576         for( DWORD j = 0; j < nModules; j++ )
00577         {
00578           HMODULE hModule = hModuleArray[j];
00579           char szModuleName[MAX_PATH];
00580 
00581           m_pfnGetModuleFileNameExA
00582           (
00583             hProcess,
00584             hModule,
00585             szModuleName, 
00586             sizeof(szModuleName)
00587           );
00588 
00589           if (0 == j)   // First module is the EXE.  Just add it to the map
00590           {
00591             pProcessInfo = new CExeModuleInstance(
00592                szModuleName, hModule, pid);
00593             m_pProcesses->Add( pProcessInfo );
00594             pProcessInfo->PopulateModules(this);
00595             break;
00596           } // if
00597         } // for
00598         ::CloseHandle(hProcess);    
00599       } // for
00600   
00601       bResult = TRUE;
00602     } // if
00603     else
00604     {
00605       bResult = FALSE;
00606     }
00607   } // if 
00608   else
00609   {
00610     bResult = FALSE;
00611   }
00612 
00613   return bResult;
00614 
00615 }
00616 
00617 
00618 //---------------------------------------------------------------------------
00619 //
00620 // class CToolhelpHandler
00621 //
00622 //---------------------------------------------------------------------------
00623 CToolhelpHandler::CToolhelpHandler(CRunningProcesses* pProcesses,QObject * parent):
00624    CLibHandler(pProcesses,parent)
00625 {
00626    
00627 }
00628 
00629 CToolhelpHandler::~CToolhelpHandler()
00630 {
00631    
00632 }
00633 
00634 
00635 //---------------------------------------------------------------------------
00636 // Initialize
00637 //
00638 //---------------------------------------------------------------------------
00639 BOOL CToolhelpHandler::Initialize()
00640 {
00641   /*
00642   ** Default to fail
00643   **
00644   */
00645   BOOL bResult = FALSE;
00646 
00647   /*
00648   ** Try to load the Kernel32.DLL library.
00649   **
00650   */
00651   HINSTANCE hInstLib = ::LoadLibraryA("Kernel32.DLL");
00652 
00653   /*
00654   ** If we got an instance, hook the procedures.
00655   **
00656   */
00657   if( NULL != hInstLib )
00658   {
00659     /*
00660     ** We must link to these functions of Kernel32.DLL explicitly. Otherwise 
00661     ** a module using this code would fail to load under Windows NT, which does not 
00662     ** have the Toolhelp32 functions in the Kernel32.
00663     **
00664     */
00665     m_pfnCreateToolhelp32Snapshot = (PFNCREATETOOLHELP32SNAPSHOT) 
00666        ::GetProcAddress( hInstLib, "CreateToolhelp32Snapshot" );
00667 
00668     m_pfnProcess32First = (PFNPROCESS32FIRST)
00669        ::GetProcAddress( hInstLib, "Process32First" );
00670 
00671     m_pfnProcess32Next = (PFNPROCESS32NEXT)
00672        ::GetProcAddress( hInstLib, "Process32Next" );
00673 
00674     m_pfnModule32First = (PFNMODULE32FIRST)
00675        ::GetProcAddress( hInstLib, "Module32First" );
00676 
00677     m_pfnModule32Next = (PFNMODULE32NEXT)
00678        ::GetProcAddress( hInstLib, "Module32Next" );
00679 
00680     ::FreeLibrary( hInstLib );
00681 
00682     bResult = m_pfnCreateToolhelp32Snapshot &&
00683               m_pfnProcess32First           &&
00684               m_pfnProcess32Next            &&
00685               m_pfnModule32First            &&
00686               m_pfnModule32Next;
00687   } // if
00688 
00689   return( bResult );
00690 
00691 } // endBOOL CToolhelpHandler::Initialize()
00692 
00693 //---------------------------------------------------------------------------
00694 // PopulateModules
00695 //
00696 // Populate the module list using ToolHelp32
00697 //---------------------------------------------------------------------------
00698 BOOL CToolhelpHandler::PopulateModules( CModuleInstance* pProcess )
00699 {
00700   BOOL   bResult = TRUE;
00701   CModuleInstance  *pDllModuleInstance = NULL;
00702   HANDLE hSnapshot = INVALID_HANDLE_VALUE;
00703 
00704   hSnapshot = m_pfnCreateToolhelp32Snapshot
00705   (
00706     TH32CS_SNAPMODULE, 
00707     static_cast<CExeModuleInstance*>(pProcess)->Get_ProcessId()
00708   );
00709 
00710   MODULEENTRY32 me = { sizeof(me) };
00711 
00712   for
00713   (
00714     BOOL fOk = ModuleFirst(hSnapshot, &me); 
00715     fOk; 
00716     fOk = ModuleNext(hSnapshot, &me)
00717   )
00718   {
00719     QString qme = QString::fromWCharArray(me.szModule);
00720 
00721     /*
00722     ** We don't need to add to the list the process itself.
00723     ** The module list should keep references to DLLs only
00724     */
00725     if( pProcess->GetBaseName() == qme )
00726     {
00727       pDllModuleInstance = new CModuleInstance( QString::fromWCharArray(me.szExePath), me.hModule );
00728       pProcess->AddModule(pDllModuleInstance);
00729     }
00730 
00731   } // for
00732 
00733   if( hSnapshot != INVALID_HANDLE_VALUE )
00734      ::CloseHandle(hSnapshot);
00735 
00736   return bResult;
00737 }
00738 
00739 BOOL CToolhelpHandler::ModuleFirst(HANDLE hSnapshot, PMODULEENTRY32 pme) const
00740 {
00741   return( m_pfnModule32First(hSnapshot, pme) );
00742 }
00743 
00744 BOOL CToolhelpHandler::ModuleNext(HANDLE hSnapshot, PMODULEENTRY32 pme) const
00745 {
00746   return( m_pfnModule32Next(hSnapshot, pme) );
00747 }
00748 
00749 
00750 BOOL CToolhelpHandler::ProcessFirst(HANDLE hSnapshot, PROCESSENTRY32* pe32) const
00751 {
00752   return( m_pfnProcess32First(hSnapshot, pe32) );
00753 }
00754 
00755 BOOL CToolhelpHandler::ProcessNext(HANDLE hSnapshot, PROCESSENTRY32* pe32) const
00756 {
00757   return( m_pfnProcess32Next(hSnapshot, pe32) );
00758 }
00759 
00760 //---------------------------------------------------------------------------
00761 // PopulateProcesses
00762 //
00763 // Populate the process list using Toolhelp
00764 //---------------------------------------------------------------------------
00765 
00766 BOOL CToolhelpHandler::PopulateProcesses()
00767 {
00768    BOOL   bResult    = FALSE;
00769    CExeModuleInstance* pProcessInfo;
00770    HANDLE hSnapshot  = INVALID_HANDLE_VALUE;
00771 
00772    if( TRUE == Initialize() )
00773    {
00774       hSnapshot = m_pfnCreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
00775 
00776       PROCESSENTRY32 pe32 = { sizeof(pe32) };
00777 
00778       for
00779       (
00780         BOOL fOk = ProcessFirst( hSnapshot, &pe32 ); 
00781         fOk; 
00782         fOk = ProcessNext(hSnapshot, &pe32)
00783       )
00784       {
00785          pProcessInfo = new CExeModuleInstance
00786          (
00787            QString::fromWCharArray(pe32.szExeFile),
00788            (HINSTANCE)pe32.th32ModuleID,
00789            pe32.th32ProcessID
00790          );
00791 
00792          m_pProcesses->Add(pProcessInfo);
00793 
00794          pProcessInfo->PopulateModules(this);
00795 
00796       } // for
00797 
00798       if (hSnapshot != INVALID_HANDLE_VALUE)
00799          ::CloseHandle(hSnapshot);
00800 
00801       bResult = TRUE;
00802    }
00803 
00804    return bResult;
00805 }   
00806 
00807 //---------------------------------------------------------------------------
00808 //
00809 // class CModuleList
00810 //
00811 //---------------------------------------------------------------------------
00812 
00813 CModuleList::CModuleList()
00814 {
00815 
00816 }
00817 
00818 CModuleList::~CModuleList()
00819 {
00820    ReleaseAll();
00821 }
00822 
00823 //---------------------------------------------------------------------------
00824 // Add
00825 //
00826 // Add new object to the container
00827 //---------------------------------------------------------------------------
00828 void CModuleList::Add( CModuleInstance * moduleInstance )
00829 {
00830   push_back( moduleInstance );
00831 }
00832 
00833 //---------------------------------------------------------------------------
00834 // ReleaseAll
00835 //
00836 // Release all objects and clear the list
00837 //---------------------------------------------------------------------------
00838 void CModuleList::ReleaseAll()
00839 {
00840    CModuleList::iterator itr;
00841    CModuleInstance *pModuleInstance;
00842 
00843    for (itr = begin(); itr != end(); ++itr)
00844    {
00845       pModuleInstance = *itr;
00846       delete pModuleInstance;
00847    } // for
00848    clear();
00849 }
00850 
00851 //---------------------------------------------------------------------------
00852 // GetModule
00853 //
00854 // Return a module object by its index (Pascal-like style)
00855 //---------------------------------------------------------------------------
00856 CModuleInstance* CModuleList::GetModule(DWORD dwIndex) const
00857 {
00858    return at(dwIndex);   
00859 }
00860 
00861 //---------------------------------------------------------------------------
00862 // GetCount
00863 //
00864 // Return number of items in the container
00865 //---------------------------------------------------------------------------
00866 DWORD CModuleList::GetCount() const
00867 {
00868    return size();
00869 }
00870 
00871 //---------------------------------------------------------------------------
00872 //
00873 // class CModuleInstance
00874 //
00875 //---------------------------------------------------------------------------
00876 CModuleInstance::CModuleInstance( const QString & pszName, HMODULE hModule )
00877 :
00878   m_pszName(),
00879   m_hModule(),
00880   m_pInternalList()
00881 {
00882   Set_Name( pszName );
00883   Set_Module( hModule );
00884 }
00885 
00886 CModuleInstance::~CModuleInstance()
00887 {
00888   delete m_pInternalList;
00889 }
00890 
00891 
00892 void CModuleInstance::AddModule( CModuleInstance* pModuleInstance )
00893 {
00894   if( NULL == m_pInternalList )
00895      m_pInternalList = new CModuleList();
00896 
00897   m_pInternalList->Add(pModuleInstance);
00898 }
00899 
00900 void CModuleInstance::ReleaseModules()
00901 {
00902   if( NULL != m_pInternalList )
00903     m_pInternalList->ReleaseAll();
00904 }
00905 
00906 QString CModuleInstance::Get_Name() const
00907 {
00908   return( m_pszName );
00909 }
00910 
00911 QString CModuleInstance::GetBaseName() const
00912 {
00913 #ifdef NEVER
00914   char *pdest;
00915   int  ch = '\\';
00916   // Search backward 
00917   pdest = strrchr(m_pszName, ch);
00918   if(pdest != NULL)
00919     return &pdest[1];
00920   else
00921     return m_pszName;
00922 #endif
00923   return( m_pszName );
00924 }
00925 
00926 void CModuleInstance::Set_Name( const QString & pszName )
00927 {
00928   m_pszName = pszName;
00929 }
00930 
00931 HMODULE CModuleInstance::Get_Module() const
00932 {
00933   return( m_hModule );
00934 }
00935 
00936 void CModuleInstance::Set_Module(HMODULE module)
00937 {
00938   m_hModule = module;
00939 }
00940 
00941 
00942 //---------------------------------------------------------------------------
00943 //
00944 // class CExeModuleInstance
00945 //
00946 //---------------------------------------------------------------------------
00947 
00948 CExeModuleInstance::CExeModuleInstance
00949 (
00950   const QString & pszName,
00951   HMODULE hModule,
00952   DWORD dwProcessId
00953 ):
00954   CModuleInstance( pszName, hModule ),
00955   m_dwProcessId( dwProcessId )
00956 {
00957 }
00958 
00959 CExeModuleInstance::~CExeModuleInstance()
00960 {
00961 }
00962 
00963 DWORD CExeModuleInstance::Get_ProcessId() const
00964 {
00965   return( m_dwProcessId );
00966 }
00967 
00968 
00969 BOOL CExeModuleInstance::PopulateModules(CLibHandler* pLibHandler)
00970 {
00971   return( pLibHandler->PopulateModules(this) );
00972 }
00973 
00974 
00975 DWORD CExeModuleInstance::GetModuleCount() const
00976 {
00977   return( m_pInternalList ? m_pInternalList->GetCount() : 0 );
00978 }
00979 
00980 CModuleInstance* CExeModuleInstance::GetModuleByIndex(DWORD dwIndex)
00981 {
00982   return( m_pInternalList ? m_pInternalList->GetModule(dwIndex) : NULL );
00983 }
00984 
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties




~ ~ ~ ~ ~ ~
Source Code without Comments is like a Cranberry Garland
without the berries. Comment your Code!
 
Commence Database User Support Group Forum
http://newsgroup.showoff-db.org/
~ ~ ~ ~ ~ ~
Author: Mark Petryk
Lorimark Solutions, LLC
mark@lorimarksolutions.com