Enumerating symbols in a module!

We can enumerate symbols in a module use dbghelp API’s provided by Microsoft. Make sure you link with dbghelp.lib.

Here is a demo… 🙂

[sourcecode language=’cpp’]// Open a process for enumeration
HANDLE hProcessHandle = ::OpenProcess( PROCESS_ALL_ACCESS, 
FALSE,
dwProcId );

// Initialize process for enumerating debug symbols
if( !hProcessHandle || !SymInitialize( hProcessHandle, 0, TRUE ))
{
ASSERT( FALSE );
return;
}

// Note that you need to enumerate each module in
// the process and pass it’s dll base to this function
VERIFY( SymEnumerateSymbols( hProcessHandle,
&dwDllBase,
EnumSymbolsCB,
lpSomeCustomParam ));

// Don’t forget to call SymCleanup
VERIFY( SymCleanup( hProcessHandle ));
CloseHandle( hProcessHandle );

// Now after above function call this function will
// be invoked, passing in the name and other info
// of a single symbol in a module
BOOL EnumSymbolsCB( LPSTR lpszSymbolName_i,
                    ULONG /*ulSymbolAddress_i*/,
                    ULONG /*ulSymbolSize_i*/,
                    LPVOID /*lpvdUserContext_i*/ )
{
// Trace out symbol name

    CString csSymbolName = lpszSymbolName_i;
    AfxTrace( csSymbolName );
    AfxTrace( _T( “n” ));

// We want more callbacks
    return TRUE;
}[/sourcecode]

Appreciate your comments...