Much faster, but only WIN32:
Get the ThreadId with this function:
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:
HANDLE GetThreadHandle(DWORD pId, DWORD dwDesiredAccess)
{
DWORD tId = GetMainThreadId(pId);
if(tId == FALSE)
return NULL;
return OpenThread(dwDesiredAccess, FALSE, tId);
}