C++.进程2

#include Psapi.h#include vectorBOOL TraversalProcess2(OUT std::vectorDWORD vec){vec.clear();try{BYTE *pbt = 0;DWORD dwSize = 0, dw = 0x1000;BOOL b = 0;for (; dw INT_MAX !b; dw *= 2){if ...

#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;
}

本文标题为:C++.进程2

基础教程推荐