2

hey so i made this real quick :

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

int main()
{
    std::cout << ProcessIdToName(GetCurrentProcessId());
}

output :

C:\Users\-_-\source\repos\Project1\x64\Release\Project1.exe

i need to remove

C:\Users\-_-\source\repos\Project1\x64\Release\

and only keep the name of the exe like this

Project1.exe

thanks for the help, heres what im talking about : so i will get the full path of the current process and i just want to keep the name of the exe like i mentioned above so in short words i just want to get the name of the exe not full path.

life less
  • 63
  • 4

1 Answers1

2

Supposing that using Windows API is allowed, you can use PathFindFileNameA function.

#include <shlwapi.h>

std::string ProcessIdToName(DWORD processId)
{
    std::string ret;
    // omit: same as original code
    return PathFindFileNameA(ret.c_str());
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70