Platform SDK: Debugging and Error Handling |
The following code demonstrates how to call the SymGetSymFromName function. This function fills in an IMAGEHLP_SYMBOL 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 IMAGEHLP_SYMBOL structure. Also, the MaxNameLength 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.
BYTE szSymbolName[256]; BYTE buffer[256]; PIMAGEHLP_SYMBOL pSymbol = (PIMAGEHLP_SYMBOL)buffer; lstrcpy(szSymbolName, "WinMain"); pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL); pSymbol->MaxNameLength = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL) + 1; if (SymGetSymFromName(hProcess, szSymbolName, pSymbol)) { // SymGetSymFromName returned success } else { // SymGetSymFromName failed error = GetLastError(); printf("SymGetSymFromName returned error : %d\n", error); }
If an application has a module or source file name as well as line number information, it can use SymGetLineFromName to retrieve a virtual code address. This function requires a pointer to an IMAGEHLP_LINE 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_LINE line; SymSetOptions(SYMOPT_LOAD_LINES); line.SizeOfStruct = sizeof(IMAGEHLP_LINE); lstrcpy(szModuleName, "MyApp"); lstrcpy(szFileName, "main.c"); dwLineNumber = 248; if (SymGetLineFromName(hProcess, szModuleName, szFileName, dwLineNumber, &lDisplacement, &line)) { // SymGetLineFromName returned success } else { // SymGetLineFromName failed error = GetLastError(); printf("SymGetLineFromName returned error : %d\n", error); }