获取进程主线程的句柄

时间:2009-12-28 13:56:25

标签: c++ windows multithreading dll-injection

我在一些小型测试应用程序中创建了一个额外的线程,并希望从这个额外的线程中挂起主线程。附加线程是通过CreateRemoteThread从外部进程创建的。

由于SuspendThread需要HANDLE到应该暂停的线程,我想知道如何从我的附加线程中运行的代码中获取HANDLE

5 个答案:

答案 0 :(得分:16)

我不认为在进程启动后,主线程与其他线程有区别。但是,您可以enumerate all threads in the process,并使用GetThreadTimes查找具有最早创建时间的主题。调用OpenThread从线程ID中获取HANDLE

答案 1 :(得分:7)

DWORD GetMainThreadId () {
    const std::tr1::shared_ptr<void> hThreadSnapshot(
        CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0), CloseHandle);
    if (hThreadSnapshot.get() == INVALID_HANDLE_VALUE) {
        throw std::runtime_error("GetMainThreadId failed");
    }
    THREADENTRY32 tEntry;
    tEntry.dwSize = sizeof(THREADENTRY32);
    DWORD result = 0;
    DWORD currentPID = GetCurrentProcessId();
    for (BOOL success = Thread32First(hThreadSnapshot.get(), &tEntry);
        !result && success && GetLastError() != ERROR_NO_MORE_FILES;
        success = Thread32Next(hThreadSnapshot.get(), &tEntry))
    {
        if (tEntry.th32OwnerProcessID == currentPID) {
            result = tEntry.th32ThreadID;
        }
    }
    return result;
}

答案 2 :(得分:6)

使用此函数获取线程ID:

/* CAUTION: ONLY x86 TESTED
 * get the thread id of the main thread of a target process
 *
 * params:
 *     DWORD dwPid  process id of the target process
 *
 * return:
 *     Success      thread id
 *     Error        NULL
 */
DWORD GetMainThreadId(DWORD dwPid)
{
    LPVOID lpTid;

    _asm
    {
        mov eax, fs:[18h]
        add eax, 36
        mov [lpTid], eax
    }

    HANDLE hProcess = OpenProcess(PROCESS_VM_READ, FALSE, dwPid);
    if(hProcess == NULL)
        return NULL;

    DWORD dwTid;
    if(ReadProcessMemory(hProcess, lpTid, &dwTid, sizeof(dwTid), NULL) == FALSE)
    {
        CloseHandle(hProcess);
        return NULL;
    }

    CloseHandle(hProcess);

    return dwTid;
}

简单地打开线程来获取句柄:

/*
 * get a handle to the main thread of a target process
 * if successfull, the returned handle must be closed with CloseHandle()
 *
 * params:
 *     DWORD dwPid              process id of the target process
 *     DWORD dwDesiredAccess    desired access rights to the thread
 *
 * return:
 *     Success      thread handle with desired access rights
 *     Error        NULL
 */
HANDLE GetMainThreadHandle(DWORD dwPid, DWORD dwDesiredAccess)
{
    DWORD dwTid = GetMainThreadId(dwPid);
    if(dwTid == FALSE)
        return NULL;

    return OpenThread(dwDesiredAccess, FALSE, dwTid);
}

答案 3 :(得分:4)

为什么不创建程序范围的全局(如果必须,请使用extern

HANDLE mainThread ;
DWORD mainThreadId ;

在main的第一行,(在创建任何线程之前)执行

mainThread = GetCurrentThread() ;
mainThreadId = GetCurrentThreadId() ;

你可以使用any form of IPC与远程进程共享id或HANDLE(尚未验证共享HANDLE会起作用但是应该!)

答案 4 :(得分:2)

这种类型的许多有用的API函数都在(当然!)Tool Help套件下。 CreateToolhelp32Snapshot() API将为指定进程拍摄正在运行的线程的快照。

// Take a snapshot of all running threads  
hThreadSnap = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 ); 
if( hThreadSnap == INVALID_HANDLE_VALUE ) 
  return( FALSE );

完整示例代码here.

返回的结构不区分主线程与其他主线程。我不知道这样做的机制;虽然某些版本的C运行时将在主线程结束时全部使用ExitProcess(),但在所有最新版本中,该过程将继续运行,直到最后一个线程退出。

Interjay建议使用GetThreadTimes可能是最好的选择。如果您可以CreateProcess()目标进程,则PROCESS_INFORMATION块的hThread成员包含主线程的tid。欢迎来自他人的任何想法。