DumontEXE 0.0.1
ModuleInstance.h
00001 //---------------------------------------------------------------------------
00002 // ModuleInstance.h
00003 //
00004 // Ivo Ivanov
00005 // ver 1.02
00006 // 05 September 2001
00007 //---------------------------------------------------------------------------
00008 
00009 #ifndef MODULE_INSTANCE_AEBFE4B5_7A54_423d_8402_FB210F96D2D8
00010 #define MODULE_INSTANCE_AEBFE4B5_7A54_423d_8402_FB210F96D2D8
00011 
00012 //---------------------------------------------------------------------------
00013 // Standard includes
00014 //---------------------------------------------------------------------------
00015 
00016 #include <iostream>
00017 #include <vector>
00018 #include <windows.h>
00019 #include <tlhelp32.h>
00020 
00021 #include <QPointer>
00022 #include <QLibrary>
00023 #include <QObject>
00024 #include <QList>
00025 #include <QString>
00026 
00027 //---------------------------------------------------------------------------
00028 //
00029 // Forward declaration
00030 //
00031 //---------------------------------------------------------------------------
00032 class CModuleInstance;
00033 class CModuleList;
00034 class CRunningProcesses;
00035 class CExeModuleInstance;
00036 
00037 //---------------------------------------------------------------------------
00038 //
00039 //             typedefs for ToolHelp32 functions 
00040 //
00041 // We must link to these functions of Kernel32.DLL explicitly. Otherwise 
00042 // a module using this code would fail to load under Windows NT, which does not 
00043 // have the Toolhelp32 functions in the Kernel 32.
00044 //
00045 //---------------------------------------------------------------------------
00046 typedef HANDLE( WINAPI * PFNCREATETOOLHELP32SNAPSHOT )
00047 (
00048   DWORD dwFlags,
00049   DWORD th32ProcessID
00050 );
00051 
00052 typedef BOOL( WINAPI * PFNPROCESS32FIRST )
00053 (
00054   HANDLE hSnapshot,
00055   LPPROCESSENTRY32 lppe
00056 );
00057 
00058 typedef BOOL( WINAPI * PFNPROCESS32NEXT )
00059 (
00060   HANDLE hSnapshot,
00061   LPPROCESSENTRY32 lppe
00062 );
00063 
00064 typedef BOOL( WINAPI * PFNMODULE32FIRST )
00065 (
00066   HANDLE hSnapshot,
00067   LPMODULEENTRY32 lpme
00068 );
00069 
00070 typedef BOOL( WINAPI * PFNMODULE32NEXT )
00071 (
00072   HANDLE hSnapshot,
00073   LPMODULEENTRY32 lpme
00074 );
00075 
00076 
00077 //---------------------------------------------------------------------------
00078 //
00079 //             typedefs for PSAPI.DLL functions 
00080 //
00081 //---------------------------------------------------------------------------
00082 typedef BOOL( WINAPI * PFNENUMPROCESSES )
00083 (
00084   DWORD * lpidProcess,
00085   DWORD cb,
00086   DWORD * cbNeeded
00087 );
00088 
00089 typedef BOOL( WINAPI * PFNENUMPROCESSMODULES )
00090 (
00091   HANDLE hProcess,
00092   HMODULE *lphModule,
00093   DWORD cb,
00094   LPDWORD lpcbNeeded
00095 );
00096 
00097 typedef DWORD( WINAPI * PFNGETMODULEFILENAMEEXA )
00098 (
00099   HANDLE hProcess,
00100   HMODULE hModule,
00101   LPSTR lpFilename,
00102   DWORD nSize
00103 );
00104 
00105 
00106 //---------------------------------------------------------------------------
00107 //
00108 // class CModuleList
00109 //
00110 // A simple container for maintaining CModuleInstance objects 
00111 // 
00112 //---------------------------------------------------------------------------
00113 class CModuleList: private QList<CModuleInstance*>
00114 {
00115 public:
00116   CModuleList();
00117   virtual ~CModuleList();
00118   //
00119   // Add new object to the container
00120   //
00121   void Add( CModuleInstance * moduleInstance );
00122   // 
00123   // Release all objects and clear the list
00124   //
00125   void ReleaseAll();
00126   //
00127   // Return a module object by its index (Pascal-like style)
00128   //
00129   CModuleInstance* GetModule( DWORD dwIndex ) const;
00130   //
00131   // Return number of items in the container
00132   //
00133   DWORD GetCount() const;
00134 
00135 };
00136 
00137 
00138 //---------------------------------------------------------------------------
00139 //
00140 // class CElements
00141 //
00142 //---------------------------------------------------------------------------
00143 class CElements: public CModuleList
00144 {
00145 public:
00146   CElements();
00147   virtual ~CElements();
00148 };
00149 
00150 //---------------------------------------------------------------------------
00151 //
00152 // class CLibHandler
00153 //
00154 //---------------------------------------------------------------------------
00155 class CLibHandler
00156 : public QObject
00157 {
00158   Q_OBJECT
00159   public:
00160 
00161     CLibHandler( CRunningProcesses* pProcesses, QObject * parent );
00162     virtual ~CLibHandler();
00163   
00164     virtual BOOL PopulateModules(CModuleInstance* pProcess) = 0;
00165     virtual BOOL PopulateProcesses() = 0;
00166 
00167   protected:
00168 
00169     CRunningProcesses* m_pProcesses;
00170 };
00171 
00172 //---------------------------------------------------------------------------
00173 //
00174 // class CTaskManager
00175 //
00176 //---------------------------------------------------------------------------
00177 class CTaskManager
00178 : public QObject
00179 {
00180   Q_OBJECT
00181   public:
00182 
00183     CTaskManager( QObject * parent );
00184 
00185     virtual ~CTaskManager();
00186 
00187     BOOL Populate();
00188 
00189     DWORD GetProcessCount() const;
00190 
00191     CExeModuleInstance * GetProcessByIndex( DWORD dwIndex );
00192 
00193     BOOL SafeTerminateProcess( HANDLE hProcess,  UINT uExitCode, DWORD dwTimeout );
00194 
00195   private:
00196 
00197     CLibHandler * m_pLibHandler;
00198 
00199     CRunningProcesses * m_pProcesses;
00200 
00201 }; // endclass CTaskManager
00202 
00203 
00204 //---------------------------------------------------------------------------
00205 //
00206 // class CLoadedModules
00207 //
00208 //---------------------------------------------------------------------------
00209 class CLoadedModules: public CElements
00210 {
00211   public:
00212 
00213     CLoadedModules(DWORD dwProcessId);
00214 
00215     virtual ~CLoadedModules();
00216 
00217   protected:
00218 
00219     DWORD m_dwProcessId;
00220 };
00221 
00222 //---------------------------------------------------------------------------
00223 //
00224 // class CRunningProcesses
00225 //
00226 //---------------------------------------------------------------------------
00227 class CRunningProcesses: public CElements
00228 {
00229   public:
00230 
00231     CRunningProcesses();
00232 
00233     virtual ~CRunningProcesses();
00234 
00235 };
00236 
00237 
00238 //---------------------------------------------------------------------------
00239 //
00240 // class CPsapiHandler
00241 //
00242 //---------------------------------------------------------------------------
00243 class CPsapiHandler: public CLibHandler
00244 {
00245   Q_OBJECT
00246   public:
00247 
00248     CPsapiHandler( CRunningProcesses* pProcesses, QObject * parent);
00249 
00250     virtual ~CPsapiHandler();
00251 
00252     BOOL Initialize();
00253 
00254     void Finalize();
00255 
00256     virtual BOOL PopulateModules( CModuleInstance* pProcess );
00257 
00258     virtual BOOL PopulateProcesses();
00259 
00260   private:
00261 
00262 //    HMODULE m_hModPSAPI;
00263 
00264     QPointer<QLibrary> m_library;
00265 
00266     PFNENUMPROCESSES m_pfnEnumProcesses;
00267 
00268     PFNENUMPROCESSMODULES m_pfnEnumProcessModules;
00269 
00270     PFNGETMODULEFILENAMEEXA m_pfnGetModuleFileNameExA;
00271 
00272 };
00273 
00274 //---------------------------------------------------------------------------
00275 //
00276 // class CToolhelpHandler
00277 //
00278 //---------------------------------------------------------------------------
00279 class CToolhelpHandler: public CLibHandler
00280 {
00281   Q_OBJECT
00282   public:
00283     CToolhelpHandler(CRunningProcesses* pProcesses,QObject * parent);
00284 
00285     virtual ~CToolhelpHandler();
00286 
00287     BOOL Initialize();
00288 
00289     virtual BOOL PopulateModules(CModuleInstance* pProcess);
00290 
00291     virtual BOOL PopulateProcesses();
00292 
00293   private:
00294 
00295     BOOL ModuleFirst(HANDLE hSnapshot, PMODULEENTRY32 pme) const;
00296 
00297     BOOL ModuleNext(HANDLE hSnapshot, PMODULEENTRY32 pme) const;
00298 
00299     BOOL ProcessFirst(HANDLE hSnapshot, PROCESSENTRY32* pe32) const;
00300 
00301     BOOL ProcessNext(HANDLE hSnapshot, PROCESSENTRY32* pe32) const;
00302     //
00303     // ToolHelp function pointers
00304     //
00305     PFNCREATETOOLHELP32SNAPSHOT m_pfnCreateToolhelp32Snapshot;
00306     PFNPROCESS32FIRST m_pfnProcess32First;
00307     PFNPROCESS32NEXT m_pfnProcess32Next;
00308     PFNMODULE32FIRST m_pfnModule32First;
00309     PFNMODULE32NEXT m_pfnModule32Next;
00310 };
00311 
00312 //---------------------------------------------------------------------------
00313 //
00314 // class CModuleInstance
00315 //
00316 // Represents exactly one loaded module
00317 // 
00318 //---------------------------------------------------------------------------
00319 class CModuleInstance  
00320 {
00321 public:
00322   CModuleInstance( const QString & pszName, HMODULE hModule );
00323 
00324   virtual ~CModuleInstance();
00325 
00326   void AddModule( CModuleInstance* pModuleInstance );
00327 
00328   void ReleaseModules();
00329 
00330   // Returns Full path and filename of the executable file for the process or DLL
00331   QString Get_Name() const;
00332 
00333   // Sets Full path and filename of the executable file for the process or DLL
00334   void Set_Name( const QString & pszName );
00335 
00336   // Returns module handle
00337   HMODULE Get_Module() const;
00338 
00339   void Set_Module(HMODULE module);
00340 
00341   // Returns only the filename of the executable file for the process or DLL
00342   QString GetBaseName() const;
00343 
00344 private:
00345   QString m_pszName;
00346 
00347   HMODULE m_hModule;
00348 
00349 protected:
00350 
00351   CModuleList *m_pInternalList;
00352 
00353 };
00354 
00355 //---------------------------------------------------------------------------
00356 //
00357 // class CExeModuleInstance
00358 //
00359 // Represents exactly one loaded EXE module
00360 // 
00361 //---------------------------------------------------------------------------
00362 class CExeModuleInstance: public CModuleInstance
00363 {
00364 public:
00365 
00366   CExeModuleInstance( const QString & pszName, HMODULE hModule, DWORD dwProcessId );
00367 
00368   virtual ~CExeModuleInstance();
00369 
00370   // Returns process id
00371 
00372   DWORD Get_ProcessId() const;
00373 
00374   BOOL PopulateModules( CLibHandler* pLibHandler );
00375 
00376   DWORD GetModuleCount() const;
00377 
00378   CModuleInstance* GetModuleByIndex( DWORD dwIndex );
00379 
00380 private:
00381 
00382   DWORD m_dwProcessId;
00383 
00384 };
00385 
00386 
00387 #endif // #ifndef MODULE_INSTANCE_AEBFE4B5_7A54_423d_8402_FB210F96D2D8
00388 
00389 
 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