How to retrieve starting address of a thread in windows?

爷,独闯天下 提交于 2021-02-06 05:32:27

问题


I'm working on a mini windows process explorer in C, I have a handle to a thread.
How can I retrieve starting address of that thread? Something like this:
enter image description here


回答1:


Such question was already asked a few days ago. Here is a sample solution:

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;

}

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

You might have to include this file: http://pastebin.com/ieEqR0eL

Related question: How to add ntdll.dll to project libraries with LoadLibrary() and GetProcAddress() functions?




回答2:


NtQueryInformationThread with ThreadQuerySetWin32StartAddress. Another possibility is to walk the thread's stack with StackWalk64.

If you only need the start address, NtQueryInformationProcess is a lot simpler. Even with fairly terse coding, walking the stack takes a couple hundred lines of code or so.




回答3:


You should be able to get a stack trace with StackWalk64 or a related function, and then parse it with the dbghelp.dll .

This CodeProject article explains it all in detail: http://www.codeproject.com/KB/threads/StackWalker.aspx



来源:https://stackoverflow.com/questions/11147846/how-to-retrieve-starting-address-of-a-thread-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!