While you probably know how to use Task Manager to manually find the ID of a process, you probably don’t want to hard code process IDs (PIDs) into your code as they often change each time your target executable is started.

The following tutorial in C describes how you can find a process ID given the name of the process.

First of all – lists of processes are constantly changing. As such it is unfeasible to directly sort through currently running processes. Instead, we take a picture or snapshot of currently running processes. We can do this through a function called CreateToolhelp32Snapshot() which can be found in <tlhelp32.h>. Note that this is part of the Windows API and as such also requires <Windows.h>. As for the purposes of this tutorial, we are only interested in running processes, our first argument is TH32CS_SNAPPROCESS and our second argument can be 0 as it is not applicable in this scenario.

#include <Windows.h>
#include <tlhelp32.h>
//<stdio.h> not actually needed in this code snippet but may be helpful for printf later on.
#include <stdio.h>

int main(void){
//Create a snapshot of currently running processes
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
//Some error handling in case we failed to get a snapshot of running processes
if(snap==INVALID_HANDLE_VALUE){
printf("%s",GetLastError());
}
}

We have now taken a snapshot of our running processes in a variable named ‘snap’ which should contain a vector containing PROCESSENTRY32 elements. Since we want to cycle through the processes within our snapshot, we should probably declare a PROCESSENTRY32 element that we can use to manipulate our snapshot.

    //Declare a PROCESSENTRY32 variable
PROCESSENTRY32 pe32;
//Set the size of the structure before using it.
pe32.dwSize = sizeof(PROCESSENTRY32);

Time for some more error handling.(which shouldn’t be needed considering we did do some error handling earlier on….) Basically if for some reason we can’t read the first process of our snapshot, this should return an error.

    //Retrieve information about the first process and exit if unsuccessful
if(!Process32First(snap, &pe32) )
{
printf("%s",GetLastError());
//Clean the snapshot object to prevent resource leakage
CloseHandle(snap);
}

Now that we have taken a snapshot of running processes and ensured that any errors are handled, let’s start looking for the name of the process we want within our snapshot! For this we need to refer to the ‘szExeFile’ property of PROCESSENTRY32 which contains a char array containing the executable name.

The idea is to declare an integer to hold our process ID, and a char array containing the name of the process we want to find the PID of. When a process name in our snapshot matches the name of the process we want, we can then refer to the th32ProcessID property of PROCESSENTRY32 which contains the process ID that we are looking for.

    int pid = 0;
char target[] = "main.exe"
//Cycle through Process List
do {
//Uncomment line below if you want your program to spit out every single list
//printf("%s\t\t\t%d\n",pe32.szExeFile,pe32.th32ProcessID);
//Comparing two strings containing process names for 'equality'
if(strcmp(pe32.szExeFile, target)==0) {
pid = pe32.th32ProcessID;
}
} while (Process32Next(snap, &pe32));
//Clean the snapshot object to prevent resource leakage
CloseHandle(snap);

Finally, remember to do some error checking just in case you failed to find the Process ID.

    if(pid != 0){
printf("The process ID of process %s is %d", target, pid);
} else {
printf("Process '%s' not found. Exiting…", target);
}

With that, we are done!

Hopefully you found what you are looking on this page. Remember to google for relevant Microsoft documentation if you did not understand some of the functions used on this page.

Full source code for a working example can be found here. Remember to change the value of ‘target’ to the process name that you want.

Note that some programs spawn multiple processes. e.g. Google Chrome – for this, you will need to create an array and store each individual process ID as you cycle through your snapshot. You may find this post on finding PIDs in C++ useful. Alternatively see this example provided in Microsoft documentation on how to take snapshots and view processes.

If you have any questions about anything on this page, feel free to leave a comment.


Leave a Reply

Your email address will not be published. Required fields are marked *