C++ (Qt)#include <string>#include <map>#include <vector>#include <libloaderapi.h> struct Module{ typedef HMODULE Handle; typedef ::std::string Name; typedef ::std::vector< Name > Names; typedef ::std::string Path; Handle m_handle; Path m_path; Names m_names; Module () : m_handle( ) , m_path() , m_names() { }}; struct Application{ typedef ::std::map< Module::Handle, Module > Modules; Modules m_modules; Application (); void init ( Module::Name name = Module::Name() );}; Application::Application (){ init();} void Application::init ( Module::Name name ){ Module::Handle handle = GetModuleHandleA( name.empty() ? LPCSTR() : name.c_str() ); Module & module = m_modules[ handle ]; for ( size_t i = 0; i < module.m_names.size(); ++i ) if ( name == module.m_names[ i ] ) return; module.m_names.push_back( name ); if ( module.m_names.size() == 1 ) { module.m_handle = handle; DWORD path_buffer_size = DWORD(); DWORD path_size = MAX_PATH; while ( path_size && path_size >= path_buffer_size ) { path_buffer_size += MAX_PATH; module.m_path.resize( path_buffer_size ); path_size = GetModuleFileNameA( handle, &module.m_path.front(), path_buffer_size ); } module.m_path[ path_size ] = '\0'; // case Windows XP. It is guaranteed that 'path_size < path_buffer_size' here. PIMAGE_DOS_HEADER dos_header = PIMAGE_DOS_HEADER( module.m_handle ); if ( dos_header->e_magic != IMAGE_DOS_SIGNATURE ) return; PIMAGE_NT_HEADERS nt_headers = PIMAGE_NT_HEADERS( LPBYTE( dos_header ) + dos_header->e_lfanew ); if ( nt_headers->Signature != IMAGE_NT_SIGNATURE ) return; DWORD virtual_address = nt_headers->OptionalHeader.DataDirectory[ 1 ].VirtualAddress; if ( virtual_address != DWORD() ) { PIMAGE_IMPORT_DESCRIPTOR image_import_descriptor = PIMAGE_IMPORT_DESCRIPTOR( LPBYTE( dos_header ) + virtual_address ); for( int i = 0; image_import_descriptor[ i ].Characteristics != DWORD(); ++i ) { init( LPCSTR( LPBYTE( dos_header ) + image_import_descriptor[ i ].Name ) ); } } }} void printApplication ( const Application & application ){ for ( const auto & pr : application.m_modules ) { const Module & module = pr.second; printf( "%s\n", module.m_path.c_str() ); for ( const auto & name : module.m_names ) printf( "\t%s\n", name.c_str() ); }}
C++ (Qt)printApplication( Application() );