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.
TCHAR 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;
_tcscpy_s(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
DWORD error = GetLastError();
_tprintf(TEXT("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.
TCHAR szModuleName[MAX_PATH];
TCHAR szFileName[MAX_PATH];
DWORD dwLineNumber;
LONG lDisplacement;
IMAGEHLP_LINE64 line;
SymSetOptions(SYMOPT_LOAD_LINES);
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
_tcscpy_s(szModuleName, MAX_PATH, TEXT("MyApp"));
_tcscpy_s(szFileName, MAX_PATH, TEXT("main.c"));
dwLineNumber = 248;
if (SymGetLineFromName64(hProcess, szModuleName, szFileName,
dwLineNumber, &lDisplacement, &line))
{
// SymGetLineFromName64 returned success
}
else
{
// SymGetLineFromName64 failed
DWORD error = GetLastError();
_tprintf(TEXT("SymGetLineFromName64 returned error : %d\n"), error);
}