Opened 5 years ago

Last modified 4 years ago

#13320 new Feature Requests

It would be nice if boost::process could provide an API to get all running processes

Reported by: jpo38 <jean.porcherot@…> Owned by:
Milestone: To Be Determined Component: process
Version: Boost 1.65.0 Severity: Problem
Keywords: Cc:

Description

There is no Xplatform API to get all running processes, it would be noce to have one.

Possible implementation:

    typedef struct ProcessDescr
    {
        /** \brief Constructor
         * \param _sModuleName program's name
         * \param _pid program's PID
         */
        ProcessDescr( const std::string& _sModuleName, int _pid ) :
            sModuleName( _sModuleName ),
            pid( _pid )
        {

        }

        /** \brief program's name */
        std::string sModuleName;
        /** \brief program's PID */
        int pid;
    } ProcessDescr;

    /** \brief List of processus descriptions */
    typedef std::vector<ProcessDescr> ProcessList;

    #if defined(BOOST_WINDOWS_API)
    void addProcessDescr( ProcessList& processes, DWORD processID )
    {
        TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");

        // Get a handle to the process.

        HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                       PROCESS_VM_READ,
                                       FALSE, processID );

        // Get the process name.

        if (NULL != hProcess )
        {
            HMODULE hMod;
            DWORD cbNeeded;

            if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                 &cbNeeded) )
            {
                GetModuleBaseName( hProcess, hMod, szProcessName, 
                                   sizeof(szProcessName)/sizeof(TCHAR) );
            }
        }

        // Print the process name and identifier.

        processes.push_back( ProcessDescr( szProcessName, processID ) );

        // Release the handle to the process.

        CloseHandle( hProcess );
    }
    #endif

    void GetAllProcesses( ProcessList& processes )
    {
    #if defined(BOOST_POSIX_API) 
        #ifdef SDE_MOBILE
            #ifdef SDE_ANDROID
                DIR *d = opendir("/proc");
                if ( d != NULL )
                {
                    struct dirent * de;
                    while((de = readdir(d)) != 0){
                        int pid = -1;
                        // to be tested!!!
                        if(isdigit(de->d_name[0])){
                            pid = atoi(de->d_name);
                        }
                        processes.push_back( ProcessDescr( de->d_name , pid ) );
                    }
                    closedir(d);
                }
            #else

                assert( false );
                #warning Unsupported platform
            #endif
        #else
            struct task_struct *task;
            for_each_process(task)
            {
                processes.push_back( ProcessDescr( task->comm , task->pid ) );
            }
        #endif
    #elif defined(BOOST_WINDOWS_API)
        DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;
        if ( EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
        {
            // Calculate how many process identifiers were returned.
            cProcesses = cbNeeded / sizeof(DWORD);

            // Print the name and process identifier for each process.
            for ( i = 0; i < cProcesses; i++ )
            {
                if( aProcesses[i] != 0 )
                {
                    addProcessDescr( processes, aProcesses[i] );
                }
            }
        }
    #else
        #error Unknown platform
    #endif
    }

Change History (1)

comment:1 by John Maddock, 4 years ago

Component: Noneprocess
Note: See TracTickets for help on using tickets.