6
votes

Énumérer des fils sous Windows

Comment puis-je énumérer tous les threads dans un processus donné une poignée au processus (ou un identifiant de processus)? Je suis intéressé à ce que cela puisse faire cela afin que je puisse faire davantage un Enumthreadwindows sur chaque fil.


0 commentaires

3 Réponses :


3
votes

La bibliothèque de ToolHelP offre une API pour les processus d'instantanée et énumérant leurs threads (entre autres propriétés). Voir MSDN pour plus de détails .


0 commentaires


0
votes
// returns the tid of threads in a given process pid. it is recursive and called from a while loop.
// return 0 when there are no more threads.

DWORD EnumerateThreads(DWORD pid)
{// needs working for a simpler loop
    char szText[MAX_PATH];

    static BOOL bStarted;
    static HANDLE hSnapPro, hSnapThread;
    static LPPROCESSENTRY32 ppe32;
    static PTHREADENTRY32 pte32;


    if (!bStarted)
    {
        if (!bStarted)
        {
            bStarted++;
            pte32 = new THREADENTRY32;
            pte32->dwSize = sizeof(THREADENTRY32);

            hSnapThread = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);

            if (!hSnapThread)
            {
                FormatErrorMessage("GetLastError -> hSnapThread = CreateToolhelp32Snapshot\n", GetLastError());
                delete pte32;
                bStarted = 0;
                return 0;
            }

            if (Thread32First(hSnapThread, pte32))
            {
                do
                {
                    if (pid == pte32->th32OwnerProcessID)
                    {
                        wsprintf(szText, "__yes Thread32First pid: 0x%X - tid: 0x%X\n", pid, pte32->th32ThreadID);
                        OutputDebugString(szText);
                        return pte32->th32ThreadID;
                    }
                } 
                while (Thread32Next(hSnapThread, pte32));
            }
            else
                FormatErrorMessage("GetLastError ->Thread32First\n", GetLastError());
        }
    }

    if (Thread32Next(hSnapThread, pte32))
    {
        do
        {
            if (pid == pte32->th32OwnerProcessID)
            {
                wsprintf(szText, "__yes Thread32First pid: 0x%X - tid: 0x%X\n", pid, pte32->th32ThreadID);
                OutputDebugString(szText);
                return pte32->th32ThreadID;
            }
        }
        while (Thread32Next(hSnapThread, pte32));
    }
    else
        FormatErrorMessage("GetLastError ->Thread32First\n", GetLastError());

    CloseHandle(hSnapThread);
    bStarted = 0;
    delete pte32;

    OutputDebugString("__finished EnumerateThreads\n");

    return 0;
}

0 commentaires