【问题标题】:How to retrieve starting address of a thread in windows?如何在windows中检索线程的起始地址?
【发布时间】:2012-06-24 06:13:42
【问题描述】:

我正在使用 C 语言开发一个迷你 Windows 进程资源管理器,我有一个线程句柄。
如何检索该线程的起始地址?像这样:

【问题讨论】:

    标签: c windows multithreading winapi


    【解决方案1】:

    几天前已经有人问过这样的问题。这是一个示例解决方案:

    DWORD WINAPI GetThreadStartAddress(HANDLE hThread)
    {
        NTSTATUS ntStatus;
        HANDLE hDupHandle;
        DWORD dwStartAddress;
    
        pNtQIT NtQueryInformationThread = (pNtQIT)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryInformationThread");
    
        if(NtQueryInformationThread == NULL) 
            return 0;
    
        HANDLE hCurrentProcess = GetCurrentProcess();
        if(!DuplicateHandle(hCurrentProcess, hThread, hCurrentProcess, &hDupHandle, THREAD_QUERY_INFORMATION, FALSE, 0)){
            SetLastError(ERROR_ACCESS_DENIED);
    
            return 0;
        }
    
        ntStatus = NtQueryInformationThread(hDupHandle, ThreadQuerySetWin32StartAddress, &dwStartAddress, sizeof(DWORD), NULL);
        CloseHandle(hDupHandle);
        if(ntStatus != STATUS_SUCCESS) 
           return 0;
    
        return dwStartAddress;
    
    }
    

    来源:http://forum.sysinternals.com/how-to-get-the-start-address-and-modu_topic5127_post18072.html#18072

    您可能必须包含此文件:http://pastebin.com/ieEqR0eL

    相关问题:How to add ntdll.dll to project libraries with LoadLibrary() and GetProcAddress() functions?

    【讨论】:

    • 谢谢,但是这个函数会返回一个数字。如何将其用作起始地址?
    • 该函数返回目标进程地址空间中线程的起始地址。你可以以任何你想要的方式使用它(例如,如果你知道函数定义,你可以转换它并使用CreateRemoteThread函数调用)。
    • 我可以使用那个数字检索类似这样的内容:chrome.dll!chromeMain+0x11ded 吗?
    • 我认为您必须检查您的起始地址位于哪个模块中(它不必在 exe 文件的 .text 段中,而是在例如 ntdll.dllchrome.dll 中),获取其中的代码ImageBaseAddress,然后计算偏移量(+0x11ded 部分)。可以通过直接分析 DLL 文件来检索函数名称(msdn.microsoft.com/en-us/library/ms809762.aspx 参见 PE 文件导出部分)。快乐的黑客攻击!
    • 函数在目标进程中的第一个字节的地址。您必须记住,它可以转换为函数指针,但代码位于目标进程的地址空间中。
    【解决方案2】:

    NtQueryInformationThreadThreadQuerySetWin32StartAddress。另一种可能性是使用StackWalk64 遍历线程的堆栈。

    如果您只需要起始地址,NtQueryInformationProcess 会更简单很多。即使使用相当简洁的编码,遍历堆栈也需要几百个 lines of code 左右。

    【讨论】:

      【解决方案3】:

      您应该能够使用StackWalk64 或相关函数获取堆栈跟踪,然后使用 dbghelp.dll 对其进行解析。

      这篇 CodeProject 文章详细解释了这一切: http://www.codeproject.com/KB/threads/StackWalker.aspx

      【讨论】:

        猜你喜欢
        • 2011-01-04
        • 1970-01-01
        • 2012-03-05
        • 2018-12-18
        • 1970-01-01
        • 1970-01-01
        • 2012-07-18
        • 2012-02-07
        • 2022-01-12
        相关资源
        最近更新 更多