Sunday, January 30, 2011

Listing the loaded modules of the specified process

Tip - EnumerateLoadedModules(): Enumerates the loaded modules for the specified process.

Details - EnumerateLoadedModules() API can be used to get the loaded modules for an application. While writing Error Handler frameworks, for crash dumps etc. the list of loaded modules can be dumped using this API, which helps in debugging purpose.
BOOL WINAPI EnumerateLoadedModules(
                                    __in  HANDLE hProcess,
                                    __in  PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
                                    __in_opt PVOID UserContext
                                   );
For calling EnumerateLoadedModules() we need to provide a callback pointer. The EnumerateLoadedModules() will send the loaded module information as callback to that provided function. For example:
#include <Dbghelp.h>
// Callback function.
BOOL CALLBACK EnumerateLoadedModulesProc( PSTR ModuleName,
                                                                     ULONG ModuleBase,
                                                                                     ULONG ModuleSize,
                                           PVOID UserContext )
{
    // Print the module name.
    cout << ModuleName << endl;
    return TRUE;
}
int main(int argc, char* argv[])
{
    // Enumerate loaded modules.
    EnumerateLoadedModules(
                        GetCurrentProcess(),   // Process Handle
// Callback function pointer
          (PENUMLOADED_MODULES_CALLBACK) EnumerateLoadedModulesProc,
          0 );         // User context.
    return 0;
}


Reference   -

Posted By :Jijo Krishnan

No comments:

Post a Comment