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

问题描述

你能帮助我找到的主要(唯一)线程由ID进程给予了ID,好吗?

任务方面:
正在运行的进程(目前)没有窗户,但一个(一些)线程(S)。

求购:
在主线程 WM_QUIT 的发布只。

不是通缉:
使用 TerminateProcess 或发布 WM_QUIT 在非主线程。


解决方案

 的#ifndef MAKEULONGLONG
的#define MAKEULONGLONG(LDW,HDW)((ULONGLONG(HDW)所述;&下; 32)|((LDW)及为0xFFFFFFFF))
#万一的#ifndef MAXULONGLONG
#定义MAXULONGLONG((ULONGLONG)〜((ULONGLONG)0))
#万一布尔CloseProcessMainThread(DWORD dwProcID)
{
  DWORD dwMainThreadID = 0;
  ULONGLONG ullMinCreateTime = MAXULONGLONG;  HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0);
  如果(hThreadSnap!= INVALID_HANDLE_VALUE){
    THREADENTRY32 TH32;
    th32.dwSize = sizeof的(THREADENTRY32);
    BOOL BOK = TRUE;
    对于(BOK = Thread32First(hThreadSnap,&安培; TH32); BOK;
         BOK = Thread32Next(hThreadSnap,&安培; TH32)){
      如果(th32.th32OwnerProcessID == dwProcID){
        HANDLE hThread =的OpenThread(THREAD_QUERY_INFORMATION,
                                    TRUE,th32.th32ThreadID);
        如果(hThread){
          FILETIME afTimes [4] = {0};
          如果(GetThreadTimes(hThread,
                             &放大器; afTimes [0],&放大器; afTimes [1],&放大器; afTimes [2],&放大器; afTimes [3])){
            ULONGLONG ullTest = MAKEULONGLONG(afTimes [0] .dwLowDateTime,
                                              afTimes [0] .dwHighDateTime);
            如果(ullTest&安培;&安培; ullTest< ullMinCreateTime){
              ullMinCreateTime = ullTest;
              dwMainThreadID = th32.th32ThreadID; //让它成为主... :)
            }
          }
          CloseHandle的(hThread);
        }
      }
    }
UNDER_CE的#ifndef
    CloseHandle的(hThreadSnap);
#其他
    CloseToolhelp32Snapshot(hThreadSnap);
#万一
  }  如果(dwMainThreadID){
    PostThreadMessage(dwMainThreadID,WM_QUIT,0,0); //闭上你的眼睛...
  }  回报(0 = dwMainThreadID!);
}

Can 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.

解决方案
#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);
}

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

08-23 05:50