The Process Tool Help Library 6

 

 

 

 

 

Traversing the Module List Program Example

 

The following example obtains a list of modules for the specified process. The ListProcessModules() function takes a snapshot of the modules associated with a given process using the CreateToolhelp32Snapshot() function, and then walks through the list using the Module32First() and Module32Next() functions. The dwPID parameter of ListProcessModules() identifies the process for which modules are to be enumerated, and is usually obtained by calling CreateToolhelp32Snapshot() to enumerate the processes running on the system. See Taking a Snapshot and Viewing Processes for a simple console application that uses this function.

A simple error-reporting function, printError(), displays the reason for any failures, which usually result from security restrictions.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

 

Traversing the Module List Program Example: Creating new C++ Win32 empty console mode application project

 

Then, add the source file and give it a suitable name.

 

Traversing the Module List Program Example: Adding new C++ source file to the existing VC++ project

 

Next, add the following source code.

 

#include <windows.h>

#include <tlhelp32.h>

#include <stdio.h>

 

//  Forward declarations:

BOOL ListProcessModules(DWORD dwPID);

void printError(WCHAR* msg);

 

int wmain()

{

  ListProcessModules(GetCurrentProcessId());

}

 

BOOL ListProcessModules(DWORD dwPID)

{

  HANDLE hModuleSnap = INVALID_HANDLE_VALUE;

  MODULEENTRY32 me32;

 

  //  Take a snapshot of all modules in the specified process.

  hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);

  if(hModuleSnap == INVALID_HANDLE_VALUE)

  {

    printError(L"CreateToolhelp32Snapshot (of modules)\n");

    return(FALSE);

  }

 

  //  Set the size of the structure before using it.

  me32.dwSize = sizeof(MODULEENTRY32);

 

  //  Retrieve information about the first module,

  //  and exit if unsuccessful

  if(!Module32First(hModuleSnap, &me32))

  {

    printError(L"Module32First\n");  // Show cause of failure

    CloseHandle(hModuleSnap);     // Must clean up the snapshot object!

    return( FALSE );

  }

 

  //  Now walk the module list of the process,

  //  and display information about each module

  do

  {

    wprintf(L"\n\n     MODULE NAME:     %s\n",             me32.szModule );

    wprintf(L"\n     executable     = %s\n",             me32.szExePath );

    wprintf(L"\n     process ID     = 0x%08X\n",         me32.th32ProcessID );

    wprintf(L"\n     ref count (g)  =     0x%04X\n",     me32.GlblcntUsage );

    wprintf(L"\n     ref count (p)  =     0x%04X\n",     me32.ProccntUsage );

    wprintf(L"\n     base address   = 0x%08X\n", (DWORD) me32.modBaseAddr );

    wprintf(L"\n     base size      = %d\n",             me32.modBaseSize );

 

  } while(Module32Next(hModuleSnap, &me32));

 

  //  Do not forget to clean up the snapshot object.

  CloseHandle(hModuleSnap);

  return(TRUE);

}

 

void printError(WCHAR* msg)

{

  DWORD eNum;

  WCHAR sysMsg[256];

  WCHAR* p;

 

  eNum = GetLastError();

  FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,

         NULL, eNum,

         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language

         sysMsg, 256, NULL );

 

  // Trim the end of the line and terminate it with a null

  p = sysMsg;

  while((*p > 31) || (*p == 9))

    ++p;

  do {

        *p-- = 0;

  } while((p >= sysMsg) && ((*p == '.') || (*p < 33)));

 

  // Display the message

  wprintf(L"\n  WARNING: %s failed with error %d (%s)\n", msg, eNum, sysMsg );

}

 

Build and run the project. The following screenshot is a sample output.

 

Traversing the Module List Program Example: A sample console mode program output

 

 

 

 

< Windows Process Tool Help APIs 5 | Windows Process Tool Help APIs | Win32 Programming | Windows Process Tool Help APIs 7 >