Updated ModuleList.hpp

This commit is contained in:
Silent 2017-11-03 22:22:22 +01:00
parent a76b06a400
commit 614354ec8a

View file

@ -108,24 +108,47 @@ public:
private: private:
void EnumerateInternal( HMODULE* modules, size_t numModules ) void EnumerateInternal( HMODULE* modules, size_t numModules )
{ {
m_moduleList.reserve( numModules ); size_t moduleNameLength = MAX_PATH;
for ( size_t i = 0; i < numModules; i++ ) wchar_t* moduleName = static_cast<wchar_t*>( malloc( moduleNameLength * sizeof(moduleName[0]) ) );
if ( moduleName != nullptr )
{ {
wchar_t moduleName[MAX_PATH]; m_moduleList.reserve( numModules );
if ( GetModuleFileNameW( *modules, moduleName, MAX_PATH ) != 0 ) for ( size_t i = 0; i < numModules; i++ )
{ {
const wchar_t* nameBegin = wcsrchr( moduleName, '\\' ) + 1; // Obtain module name, with resizing if necessary
const wchar_t* dotPos = wcsrchr( nameBegin, '.' ); DWORD size;
if ( dotPos != nullptr ) 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<wchar_t*>( 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 );
} }
} }