2

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

我正在使用 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”......
我想我做错了什么。
你能帮我吗?

谢谢,
阿尔贝托

4

0 回答 0