如何修复SymFromAddr返回的ERROR_INVALID_ADDRESS(错误487)

时间:2019-05-21 07:35:49

标签: c function symbols dbghelp

我正在尝试按地址获取函数名称。

我正在使用VS Community 2019,第一步是复制可以在以下位置找到的Microsoft示例:
https://docs.microsoft.com/en-us/windows/desktop/debug/initializing-the-symbol-handler
https://docs.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://docs.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://docs.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);
    }

}

但是此代码始终打印“ SymFromAddr返回错误:487” ...
我想我做错了。
你能帮忙吗?

谢谢,
阿尔贝托

0 个答案:

没有答案