如何获取进程的主线程ID(通过其ID知道)? [英] How to get the main thread ID of a process (known by its ID) ?

查看:159
本文介绍了如何获取进程的主线程ID(通过其ID知道)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.

你能帮我吗
查找主(唯一)线程ID
通过ID流程给定的,请吗? :)

任务上下文:
正在运行的进程(目前)没有窗口,只有(一些)线程.

想要:
WM_QUIT仅在主线程上发布.

不需要:
在非主线程上使用TerminateProcess或发布WM_QUIT.

谢谢!

Hello.

Could you help me
to find the main (only) thread ID
of a given by ID process, please ? :)

Task context:
A running process has (at the moment) no windows but a(some) thread(s).

Wanted:
Posting of WM_QUIT at the main thread only.

Not-wanted:
Using of TerminateProcess or posting WM_QUIT at the non-primary threads.

Thank you !

推荐答案

此刻找到了解决方案":-O :):

An at the moment found "solution" :-O :) :

#ifndef MAKEULONGLONG
#define MAKEULONGLONG(ldw, hdw) ((ULONGLONG(hdw) << 32) | ((ldw) & 0xFFFFFFFF))
#endif
  
#ifndef MAXULONGLONG
#define MAXULONGLONG ((ULONGLONG)~((ULONGLONG)0))
#endif
  
bool CloseProcessMainThread(DWORD dwProcID)
{
  DWORD dwMainThreadID = 0;
  ULONGLONG ullMinCreateTime = MAXULONGLONG;
  
  HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
  if (hThreadSnap != INVALID_HANDLE_VALUE) {
    THREADENTRY32 th32;
    th32.dwSize = sizeof(THREADENTRY32);
    BOOL bOK = TRUE;
    for (bOK = Thread32First(hThreadSnap, &th32); bOK;
         bOK = Thread32Next(hThreadSnap, &th32)) {
      if (th32.th32OwnerProcessID == dwProcID) {
        HANDLE hThread = OpenThread(THREAD_QUERY_INFORMATION,
                                    TRUE, th32.th32ThreadID);
        if (hThread) {
          FILETIME afTimes[4] = {0};
          if (GetThreadTimes(hThread,
                             &afTimes[0], &afTimes[1], &afTimes[2], &afTimes[3])) {
            ULONGLONG ullTest = MAKEULONGLONG(afTimes[0].dwLowDateTime,
                                              afTimes[0].dwHighDateTime);
            if (ullTest && ullTest < ullMinCreateTime) {
              ullMinCreateTime = ullTest;
              dwMainThreadID = th32.th32ThreadID; // let it be main... :)
            }
          }
          CloseHandle(hThread);
        }
      }
    }
#ifndef UNDER_CE
    CloseHandle(hThreadSnap);
#else
    CloseToolhelp32Snapshot(hThreadSnap);
#endif
  }
  
  if (dwMainThreadID) {
    PostThreadMessage(dwMainThreadID, WM_QUIT, 0, 0); // close your eyes...
  }
  
  return (0 != dwMainThreadID);
}


所以接受答案(那里有一个按钮).
So accept the answer (there''s a button there for it).


要快得多,但只有WIN32:

使用以下函数获取ThreadId:

Much faster, but only WIN32:

Get the ThreadId with this function:

/* CAUTION: ONLY WIN32
 * get the threadId of the main thread of a target process
 *
 * params:
 *     DWORD pId    processId of the target process
 *
 * return:
 *     Success      threadId
 *     Error        NULL
 */
DWORD GetMainThreadId(DWORD pId)
{
    LPVOID lpThId;

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

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

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

    CloseHandle(hProcess);

    return tId;
}




只需打开线程即可获取句柄:




Simple open the thread to get the handle:

/*
 * get a handle to the main thread of a target process
 *
 * params:
 *     DWORD pId                processId of the target process
 *     DWORD dwDesiredAccess    desired access rights to the thread
 *
 * return:
 *     Success      threadHandle with desired access rights
 *     Error        NULL
 */
HANDLE GetThreadHandle(DWORD pId, DWORD dwDesiredAccess)
{
    DWORD tId = GetMainThreadId(pId);
    if(tId == FALSE)
        return NULL;

    return OpenThread(dwDesiredAccess, FALSE, tId);
}


这篇关于如何获取进程的主线程ID(通过其ID知道)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆