系统相关
首页 > 系统相关> > C++.进程2

C++.进程2

作者:互联网

#include <Psapi.h>
#include <vector>
BOOL TraversalProcess2(OUT std::vector<DWORD> &vec)
{
    vec.clear();
    try
    {
        BYTE *pbt = 0;
        DWORD dwSize = 0, dw = 0x1000;
        BOOL b = 0;
        for (; dw < INT_MAX && !b; dw *= 2)
        {
            if (pbt)
            {
                delete pbt;
                pbt = 0;
            }
            pbt = new BYTE[dw];
            if (pbt)
            {
                b = EnumProcesses((DWORD*)pbt, dw, &dwSize);
            }
        }
        if (pbt && b)
        {
            DWORD *pdw = (DWORD*)pbt;
            for (DWORD i = 0; i < dwSize / sizeof(DWORD); ++i)
            {
                vec.push_back(pdw[i]);
            }
            delete pbt;
            pbt = 0;
        }
    }
    catch (...)
    {
        OutputDebugStringA(__FUNCTION__);
        return 0;
    }
    return vec.size() > 0;
}

int main()
{
    std::vector<DWORD> vec;
    std::cout << TraversalProcess2(vec) << endl;
    for each (DWORD var in vec)
    {
        printf("%d\t", var);
        printf("\r\n");
    }

    return 0;
}

标签:std,pbt,dwSize,C++,vec,DWORD,进程,dw
来源: https://www.cnblogs.com/dailycode/p/12459503.html