Get full running process list ( Visual C++ )

前端 未结 4 1599
傲寒
傲寒 2020-12-05 10:24

I am currently using the EnumProcesses function to obtain a list of running processes. Since my application runs in user space, however, it is not able to get handles for pr

4条回答
  •  余生分开走
    2020-12-05 11:08

    If all you need are just process names, then use WTSEnumerateProcesses as such:

    WTS_PROCESS_INFO* pWPIs = NULL;
    DWORD dwProcCount = 0;
    if(WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, NULL, 1, &pWPIs, &dwProcCount))
    {
        //Go through all processes retrieved
        for(DWORD i = 0; i < dwProcCount; i++)
        {
            //pWPIs[i].pProcessName = process file name only, no path!
            //pWPIs[i].ProcessId = process ID
            //pWPIs[i].SessionId = session ID, if you need to limit it to the logged in user processes
            //pWPIs[i].pUserSid = user SID that started the process
        }
    }
    
    //Free memory
    if(pWPIs)
    {
        WTSFreeMemory(pWPIs);
        pWPIs = NULL;
    }
    

    The benefit of using this method is that you don't have to open each process individually and then retrieve its name as what you'd have to do if you went with EnumProcesses instead, which also won't work if you try to open processes that run with higher privileges than your user account.

    Additionally this method is also much faster than calling Process32First()/Process32Next() in a loop.

    WTSEnumerateProcesses is a lesser known API that has been available since Windows XP.

提交回复
热议问题