How to Get the List of All Running Tasks in C++?
In C++, we may need the list of active tasks for various reasons. We may retrieve that list from the console with the tasklist system function in OS. In this article, we will learn how to obtain a list of all the processes currently in use in C++. The system() function is provided by C++, which is used to make system calls. We can use this C++ system() function to execute the tasklist command. We can obtain and show the user a list of all the processes that are active on a Windows system by using the tasklist command. We can use the system function to run a command in the operating system shell. We'll use the "tasklist" command to get the list of active processes.
![How to Get the List of All Running Tasks in C++?/>
<!-- /wp:html -->
<!-- wp:paragraph -->
<p><strong>Syntax:</strong></p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>It has the following syntax:</p>
<!-- /wp:paragraph -->
<!-- wp:preformatted -->
<pre class=](https://static.tutorialandexample.com/cpp/how-to-get-the-list-of-all-running-tasks-in-cpp1.png)
In the event that the command is properly executed, the function returns 0. The task list will appear on the console.
Here are two examples of enumerating tasks in C++. In one example, information about tasks within a task folder is displayed; in another, information about all active tasks is displayed.
The steps below explain how to show the names and states of all the tasks within a task folder.
1. Configure general COM security and initialize COM.
2. Construct the ITaskService entity.
This item gives us access to a particular task folder and a means of connecting to the Task Scheduler service.
3. Assemble a task folder including the tasks you wish to know more about.
Use the ITaskService::GetFolder function to obtain the folder.
4. Extract the task collection from the folder.
Use the ITaskFolder::GetTasks function to obtain the task collection (IRegisteredTaskCollection).
5. Determine how many tasks are in the collection and go through each one one by one.
Use the Item Property of IRegisteredTaskCollection to obtain an IRegisteredTask instance. There will be a task in the collection for every instance. After that, we may see the data (property values) from every task that has been registered.
The C++ sample below shows the name and status of each task in the root task folder.
It shows each task's name and state as it goes through the local computer's tasks list.
The steps shown below explain how to show the names and states of all active tasks.
1. Configure general COM security and initialize COM.
2. Construct the ITaskService entity.
We can access a specific task folder and establish a connection to the Task Scheduler service with this object.
Take advantage of the ITaskService::GetRunningTasks function to obtain an array of all the active tasks (IRunningTaskCollection). We can also obtain instances of tasks that are currently running, either with or without hidden tasks.
3. Determine how many tasks are in the collection and go through each one one by one.
Use the Item property of IRunningTaskCollection to obtain an IRunningTask instance. There will be a task in the collection for every instance. After that, we can see the data (property values) from every task that has been registered.
The C++ sample that follows shows the name and status of every executing task, including hidden tasks.
This example shows the name and status of each job that is currently running on the local computer.
Example:
Let us take a C++ program to illustrate how to get the list of all the running tasks.
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
// Print the objective of program
cout << "Running tasklist command to retrieve "
"list of running processes:"
<< endl;
// Execute the tasklist command and display the output
int returnValue = system("tasklist");
if (returnValue != 0) {
cerr << "Error executing tasklist command" << endl;
return 1;
}
return 0;
}
Output:
Running tasklist command to retrieve list of running processes
System 4 Services 0 140 K
Registry 136 Services 0 57,756 K
smss.exe 584 Services 0 948 K
csrss.exe 916 Services 0 5,456 K
wininit.exe 508 Services 0 6,372 K
...........