BOOL CToolhelpHandler::PopulateProcesses() { BOOL bResult = FALSE; CExeModuleInstance* pProcessInfo; HANDLE hSnapshot = INVALID_HANDLE_VALUE; if (TRUE == Initialize()) { hSnapshot = m_pfnCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); PROCESSENTRY32 pe32 = { sizeof(pe32) }; for (BOOL fOk = ProcessFirst(hSnapshot, &pe32); fOk; fOk = ProcessNext(hSnapshot, &pe32)) { pProcessInfo = new CExeModuleInstance( pe32.szExeFile, (HINSTANCE)pe32.th32ModuleID, pe32.th32ProcessID); m_pProcesses->Add(*pProcessInfo); pProcessInfo->PopulateModules(this); } // for if (hSnapshot != INVALID_HANDLE_VALUE) ::CloseHandle(hSnapshot); bResult = TRUE; } return bResult; }
//--------------------------------------------------------------------------- // PopulateModules // // Populate the module list using ToolHelp32 //--------------------------------------------------------------------------- BOOL CToolhelpHandler::PopulateModules(CModuleInstance* pProcess) { BOOL bResult = TRUE; CModuleInstance *pDllModuleInstance = NULL; HANDLE hSnapshot = INVALID_HANDLE_VALUE; hSnapshot = m_pfnCreateToolhelp32Snapshot( TH32CS_SNAPMODULE, static_cast<CExeModuleInstance*>(pProcess)->Get_ProcessId()); MODULEENTRY32 me = { sizeof(me) }; for (BOOL fOk = ModuleFirst(hSnapshot, &me); fOk; fOk = ModuleNext(hSnapshot, &me)) { // We don't need to add to the list the process itself. // The module list should keep references to DLLs only if (0 != stricmp(pProcess->GetBaseName(), me.szModule)) { pDllModuleInstance = new CModuleInstance(me.szExePath, me.hModule); pProcess->AddModule(pDllModuleInstance); } } // for if (hSnapshot != INVALID_HANDLE_VALUE) ::CloseHandle(hSnapshot); return bResult; }
HMODULE CXTPSkinManagerModuleList::CToolHelpModuleEnumerator::GetFirstModule() { if (m_pfnCreateToolhelp32Snapshot == NULL || m_pfnModule32First == NULL || m_pfnModule32Next == NULL) { return NULL; } m_hSnapshot = m_pfnCreateToolhelp32Snapshot(TH32CS_SNAPMODULE, m_dwProcessId); if (m_hSnapshot == INVALID_HANDLE_VALUE) return NULL; if (m_pfnModule32First(m_hSnapshot, &m_me)) { return m_me.hModule; } return NULL; }
////////////////////////////////////////////////////////////////////////////// // PopulateProcess // // Populate all modules of a single process // ////////////////////////////////////////////////////////////////////////////// BOOL CToolhelpHandler::PopulateProcess(DWORD dwProcessId, BOOL bPopulateModules) { BOOL bResult = FALSE; CExeModuleInstance* pProcessInfo; HANDLE hSnapshot = INVALID_HANDLE_VALUE; if (TRUE == Initialize()) { hSnapshot = m_pfnCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, dwProcessId); PROCESSENTRY32 pe32 = { sizeof(pe32) }; for (BOOL bOk = ProcessFirst(hSnapshot, &pe32); bOk; bOk = ProcessNext(hSnapshot, &pe32)) { if ( (dwProcessId != NULL) && (dwProcessId != pe32.th32ProcessID) ) continue; pProcessInfo = new CExeModuleInstance( this, pe32.szExeFile, NULL, // We will fix up later this value pe32.th32ProcessID ); m_pProcesses->Add(*pProcessInfo); if (bPopulateModules) pProcessInfo->PopulateModules(); if (dwProcessId != NULL) break; } // for if (hSnapshot != INVALID_HANDLE_VALUE) ::CloseHandle(hSnapshot); bResult = TRUE; } return bResult; }
BOOL CXTPSkinManagerModuleList::CToolHelpModuleEnumerator::GetModuleInformation(HMODULE hModule, XTP_MODULEINFO* lpmodinfo) { if (m_pfnCreateToolhelp32Snapshot == NULL || m_pfnModule32First == NULL || m_pfnModule32Next == NULL) { return FALSE; } MODULEENTRY32 me; ZeroMemory(&me, sizeof(MODULEENTRY32)); me.dwSize = sizeof(MODULEENTRY32); HANDLE hSnapshot = m_pfnCreateToolhelp32Snapshot(TH32CS_SNAPMODULE, m_dwProcessId); if (hSnapshot == INVALID_HANDLE_VALUE) return FALSE; if (!m_pfnModule32First(hSnapshot, &me)) return FALSE; do { if (me.hModule == hModule) { lpmodinfo->lpBaseOfDll = me.modBaseAddr; lpmodinfo->EntryPoint = 0; lpmodinfo->SizeOfImage = me.modBaseSize; CloseHandle(hSnapshot); return TRUE; } } while (m_pfnModule32Next(hSnapshot, &me)); CloseHandle(hSnapshot); return FALSE; }
////////////////////////////////////////////////////////////////////////////// // PopulateModules // // Populate the module list using ToolHelp32 ////////////////////////////////////////////////////////////////////////////// BOOL CToolhelpHandler::PopulateModules(CModuleInstance* pProcess) { BOOL bResult = TRUE; CModuleInstance *pDllModuleInstance = NULL; HANDLE hSnapshot = INVALID_HANDLE_VALUE; hSnapshot = m_pfnCreateToolhelp32Snapshot( TH32CS_SNAPMODULE, static_cast<CExeModuleInstance*>(pProcess)->Get_ProcessId()); MODULEENTRY32 me = { sizeof(me) }; for (BOOL bOk = ModuleFirst(hSnapshot, &me); bOk; bOk = ModuleNext(hSnapshot, &me)) { // We don't need to add to the list the process itself. // The module list should keep references to DLLs only if (0 != _stricmp(pProcess->GetBaseName(), me.szModule)) { pDllModuleInstance = new CModuleInstance(me.szExePath, me.hModule); pProcess->AddModule(pDllModuleInstance); } else // // However, we should fix up the module of the EXE, because // th32ModuleID member has meaning only to the tool help functions // and it is not usable by Win32 API elements. // { pProcess->Set_Module( me.hModule ); } } // for if (hSnapshot != INVALID_HANDLE_VALUE) ::CloseHandle(hSnapshot); return bResult; }