Reputation: 103
I'm tryng to get function name by address.
I'm using VS Community 2019 and my first step is to copy microsoft examples that you can find at:
https://learn.microsoft.com/en-us/windows/desktop/debug/initializing-the-symbol-handler
https://learn.microsoft.com/en-us/windows/desktop/debug/retrieving-symbol-information-by-address
#include <stdio.h>
#include <windows.h>
#include <dbghelp.h>
#pragma comment(lib, "dbghelp")
int main() {
DWORD64 SOME_ADDRESS = (DWORD64) (main);
// https://learn.microsoft.com/en-us/windows/desktop/debug/initializing-the-symbol-handler
DWORD error;
HANDLE hProcess;
SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
hProcess = GetCurrentProcess();
if (!SymInitialize(hProcess, NULL, TRUE))
{
// SymInitialize failed
error = GetLastError();
printf("SymInitialize returned error : %d\n", error);
return FALSE;
}
// https://learn.microsoft.com/en-us/windows/desktop/debug/retrieving-symbol-information-by-address
DWORD64 dwDisplacement = 0;
DWORD64 dwAddress = SOME_ADDRESS;
char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
pSymbol->MaxNameLen = MAX_SYM_NAME;
if (SymFromAddr(hProcess, dwAddress, &dwDisplacement, pSymbol))
{
// SymFromAddr returned success
}
else
{
// SymFromAddr failed
DWORD error = GetLastError();
printf("SymFromAddr returned error : %d\n", error);
}
}
But this code print always "SymFromAddr returned error : 487"...
I think I'm doing something wrong.
Can you help?
Thanks,
Alberto
Upvotes: 2
Views: 1177