Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve starting address of a thread in windows?

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

like image 887
The Pianist Avatar asked Jun 21 '12 22:06

The Pianist


1 Answers

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?

like image 79
Adam Sznajder Avatar answered Sep 20 '22 21:09

Adam Sznajder