Muhammad
Muhammad

Reputation: 331

How to get the process name in C++

How do I get the process name from a PID using C++ in Windows?

Upvotes: 33

Views: 121898

Answers (6)

T.s. Arun
T.s. Arun

Reputation: 357

All the above methods require psapi.dll to be loaded (Read the remarks section) and iterating through process snapshot is an option one should not even consider for getting a name of the executable file from an efficiency standpoint.

The best approach, even according to MSDN recommendation, is to use QueryFullProcessImageName.

std::string ProcessIdToName(DWORD processId)
{
    std::string ret;
    HANDLE handle = OpenProcess(
        PROCESS_QUERY_LIMITED_INFORMATION,
        FALSE,
        processId /* This is the PID, you can find one from windows task manager */
    );
    if (handle)
    {
        DWORD buffSize = 1024;
        CHAR buffer[1024];
        if (QueryFullProcessImageNameA(handle, 0, buffer, &buffSize))
        {
            ret = buffer;
        }
        else
        {
            printf("Error GetModuleBaseNameA : %lu", GetLastError());
        }
        CloseHandle(handle);
    }
    else
    {
        printf("Error OpenProcess : %lu", GetLastError());
    }
    return ret;
}

Upvotes: 15

Iulian Rotaru
Iulian Rotaru

Reputation: 72

Try this function :

std::wstring GetProcName(DWORD aPid)
{ 
 PROCESSENTRY32 processInfo;
    processInfo.dwSize = sizeof(processInfo);
    HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    if (processesSnapshot == INVALID_HANDLE_VALUE)
    {
      std::wcout  << "can't get a process snapshot ";
      return 0;
    }

    for(BOOL bok =Process32First(processesSnapshot, &processInfo);bok;  bok = Process32Next(processesSnapshot, &processInfo))
    {
        if( aPid == processInfo.th32ProcessID)
        {
            std::wcout << "found running process: " << processInfo.szExeFile;
            CloseHandle(processesSnapshot);
            return processInfo.szExeFile;
        }

    }
    std::wcout << "no process with given pid" << aPid;
    CloseHandle(processesSnapshot);
    return std::wstring();
}

Upvotes: 1

Salman Arshad
Salman Arshad

Reputation: 272376

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;
}

Upvotes: 29

amirpc
amirpc

Reputation: 1678

Check out the enumprocess functions in the tool help library:

http://msdn.microsoft.com/en-us/library/ms682629(v=vs.85).aspx

Good example @ http://msdn.microsoft.com/en-us/library/ms682623(v=vs.85).aspx

Upvotes: 1

Brian R. Bondy
Brian R. Bondy

Reputation: 347556

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.

Upvotes: 17

Nicolas Repiquet
Nicolas Repiquet

Reputation: 9265

If you are trying to get the executable image name of a given process, take a look at GetModuleFileName.

Upvotes: 2

Related Questions