Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C Windows Threading
Hello.
 
Could you help me
to find the main (only) thread ID
of a given by ID process, please ? Smile | :)
 
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 !
Posted 3-May-10 20:57pm
Edited 4-May-10 0:23am
Moak11.4K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

An at the moment found "solution" Blush | :O Smile | :) :
 
#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);
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

So accept the answer (there's a button there for it).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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);
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 544
2 Maciej Los 325
3 DamithSL 223
4 Mathew Soji 195
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 8 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100