C++.线程1

#include <TlHelp32.h>
#include <vector>
BOOL TraversalThread1(OUT std::vector<THREADENTRY32> &vec)
{
    /************************************************************************/
    /*
    typedef struct tagTHREADENTRY32
    {
    DWORD   dwSize;
    DWORD   cntUsage;
    DWORD   th32ThreadID;       // this thread
    DWORD   th32OwnerProcessID; // Process this thread is associated with
    LONG    tpBasePri;
    LONG    tpDeltaPri;
    DWORD   dwFlags;
    } THREADENTRY32;
    */
    /************************************************************************/
    vec.clear();
    try
    {
        HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
        if (hSnap != INVALID_HANDLE_VALUE)
        {
            THREADENTRY32 item;
            item.dwSize = sizeof(item);
            if (Thread32First(hSnap, &item))
            {
                do
                {
                    vec.push_back(item);
                } while (Thread32Next(hSnap, &item));
            }
            CloseHandle(hSnap);
        }
    }
    catch (...)
    {
        OutputDebugStringA(__FUNCTION__);
        return 0;
    }
    return vec.size() > 0;
}

int main()
{
    std::vector<THREADENTRY32> vec;
    std::cout << TraversalThread1(vec) << endl;
    for each (THREADENTRY32 var in vec)
    {
        printf("%08X %08X|%d", var.th32ThreadID, var.th32OwnerProcessID, var.th32OwnerProcessID);
        printf("\r\n");
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/dailycode/p/12466065.html