[Boost-bugs] [Boost C++ Libraries] #13320: It would be nice if boost::process could provide an API to get all running processes

Subject: [Boost-bugs] [Boost C++ Libraries] #13320: It would be nice if boost::process could provide an API to get all running processes
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2017-12-04 11:21:21


#13320: It would be nice if boost::process could provide an API to get all running
processes
--------------------------------------+---------------------
 Reporter: jpo38 <jean.porcherot@…> | Owner: (none)
     Type: Feature Requests | Status: new
Milestone: To Be Determined | Component: None
  Version: Boost 1.65.0 | Severity: Problem
 Keywords: |
--------------------------------------+---------------------
 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
     }

 }}}

-- 
Ticket URL: <https://svn.boost.org/trac10/boost/ticket/13320>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-12-04 11:27:56 UTC