Get thread id of main thread
<p>I am writing a plugin. I am only allowed to run code from a spawned main thread. I can't run code on the main thread.</p>

<p>From my thread, is it possible to get the thread id of the main thread?</p>

<p>I thought to do <code>OpenProcess</code> like this below - however it seems overkill, as my process of the thread is the same.</p>

<pre><code>/* 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, &amp;tId, sizeof(tId), NULL) == FALSE)
    {
        CloseHandle(hProcess);
        return NULL;
    }

    CloseHandle(hProcess);

    return tId;
}
</code></pre>

<p>Is there any better way?</p>