How do I get the process name from a PID using C++ in Windows?
In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column. Click on any column name to sort. You can right click a process name to see more options for a process.
You can use the ps command to find out which processes are running and display information about those processes. The ps command has several flags that enable you to specify which processes to list and what information to display about each process.
Type the ps aux to see all running process in Linux. Alternatively, you can issue the top command or htop command to view running process in Linux.
I guess the OpenProcess function should help, given that your process possesses the necessary rights. Once you obtain a handle to the process, you can use the GetModuleFileNameEx function to obtain full path (path to the .exe file) of the process.
#include "stdafx.h" #include "windows.h" #include "tchar.h" #include "stdio.h" #include "psapi.h" // Important: Must include psapi.lib in additional dependencies section // In VS2005... Project > Project Properties > Configuration Properties > Linker > Input > Additional Dependencies int _tmain(int argc, _TCHAR* argv[]) { HANDLE Handle = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, 8036 /* This is the PID, you can find one from windows task manager */ ); if (Handle) { TCHAR Buffer[MAX_PATH]; if (GetModuleFileNameEx(Handle, 0, Buffer, MAX_PATH)) { // At this point, buffer contains the full path to the executable } else { // You better call GetLastError() here } CloseHandle(Handle); } return 0; }
You can obtain the process name by using the WIN32 API GetModuleBaseName after having the process handle. You can get the process handle by using OpenProcess.
To get the executable name you can also use GetProcessImageFileName.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With