Retrieving Symbol Information by Name

Debug Help Library

Retrieving Symbol Information by Name

The following code demonstrates how to call the SymFromName function. This function fills in a SYMBOL_INFO structure. Because the name is variable in length, you must supply a buffer that is large enough to hold the name stored at the end of the SYMBOL_INFO structure. Also, the MaxNameLen member must be set to the number of bytes reserved for the name. In this example, szSymbolName is a buffer that stores the name of the requested symbol. The example assumes you have initialized the symbol handler using the code in Initializing the Symbol Handler.

BYTE szSymbolName[MAX_SYM_NAME];
ULONG64 buffer[(sizeof(SYMBOL_INFO) +
    MAX_SYM_NAME*sizeof(TCHAR) +
    sizeof(ULONG64) - 1) /
    sizeof(ULONG64)];
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;

StringCchCopy(szSymbolName, MAX_SYM_NAME, TEXT("WinMain"));
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
pSymbol->MaxNameLen = MAX_SYM_NAME;

if (SymFromName(hProcess, szSymbolName, pSymbol))
{
    // SymFromName returned success
}
else
{
    // SymFromName failed
    error = GetLastError();
    printf("SymFromName returned error : %d\n", error);
}

If an application has a module or source file name as well as line number information, it can use SymGetLineFromName64 to retrieve a virtual code address. This function requires a pointer to an IMAGEHLP_LINE64 structure to receive the virtual code address. Note that the symbol handler can retrieve line number information only when SYMOPT_LOAD_LINES option is set using the SymSetOptions function. This option must be set before loading the module. The szModuleName parameter contains the source module name; it is optional and can be NULL. The szFileName parameter should contain the source file name, and dwLineNumber parameter should contain the line number for which the virtual address will be retrieved.

BYTE   szModuleName[MAX_PATH];
BYTE   szFileName[MAX_PATH];
DWORD  dwLineNumber;
LONG   lDisplacement;
IMAGEHLP_LINE64 line;

SymSetOptions(SYMOPT_LOAD_LINES);

line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
StringCchCopy(szModuleName, MAX_PATH, TEXT("MyApp"));
StringCchCopy(szFileName, MAX_PATH, TEXT("main.c"));
dwLineNumber = 248;

if (SymGetLineFromName64(hProcess, szModuleName, szFileName,
    dwLineNumber, &lDisplacement, &line))
{
    // SymGetLineFromName64 returned success
}
else
{
    // SymGetLineFromName64 failed
    error = GetLastError();
    printf("SymGetLineFromName64 returned error : %d\n", error);
}

Send comments about this topic to Microsoft

Build date: 9/25/2007

© 2007 Microsoft Corporation. All rights reserved.