From 614354ec8a8313ab71c0c7c8b52df6629c5621b8 Mon Sep 17 00:00:00 2001 From: Silent Date: Fri, 3 Nov 2017 22:22:22 +0100 Subject: [PATCH] Updated ModuleList.hpp --- SilentPatch/ModuleList.hpp | 45 ++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/SilentPatch/ModuleList.hpp b/SilentPatch/ModuleList.hpp index acb3595..4921cd4 100644 --- a/SilentPatch/ModuleList.hpp +++ b/SilentPatch/ModuleList.hpp @@ -108,24 +108,47 @@ public: private: void EnumerateInternal( HMODULE* modules, size_t numModules ) { - m_moduleList.reserve( numModules ); - for ( size_t i = 0; i < numModules; i++ ) + size_t moduleNameLength = MAX_PATH; + wchar_t* moduleName = static_cast( malloc( moduleNameLength * sizeof(moduleName[0]) ) ); + if ( moduleName != nullptr ) { - wchar_t moduleName[MAX_PATH]; - if ( GetModuleFileNameW( *modules, moduleName, MAX_PATH ) != 0 ) + m_moduleList.reserve( numModules ); + for ( size_t i = 0; i < numModules; i++ ) { - const wchar_t* nameBegin = wcsrchr( moduleName, '\\' ) + 1; - const wchar_t* dotPos = wcsrchr( nameBegin, '.' ); - if ( dotPos != nullptr ) + // Obtain module name, with resizing if necessary + DWORD size; + while ( size = GetModuleFileNameW( *modules, moduleName, moduleNameLength ), size == moduleNameLength ) { - m_moduleList.emplace_back( *modules, std::wstring( nameBegin, std::distance( nameBegin, dotPos ) ) ); + wchar_t* newName = static_cast( realloc( moduleName, 2 * moduleNameLength * sizeof(moduleName[0]) ) ); + if ( newName != nullptr ) + { + moduleName = newName; + moduleNameLength *= 2; + } + else + { + size = 0; + break; + } } - else + + if ( size != 0 ) { - m_moduleList.emplace_back( *modules, nameBegin ); + const wchar_t* nameBegin = wcsrchr( moduleName, '\\' ) + 1; + const wchar_t* dotPos = wcsrchr( nameBegin, '.' ); + if ( dotPos != nullptr ) + { + m_moduleList.emplace_back( *modules, std::wstring( nameBegin, std::distance( nameBegin, dotPos ) ) ); + } + else + { + m_moduleList.emplace_back( *modules, nameBegin ); + } } + modules++; } - modules++; + + free( moduleName ); } }