I
I
iamdivine2018-11-15 23:34:13
C++ / C#
iamdivine, 2018-11-15 23:34:13

How to find out the path of a process by its name?

Good evening.
I looked at many guides and manuals, but did not find anything suitable.
It is necessary for the console program to search for the path of the running process, or rather by its name.
Windows

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SerJook, 2018-11-21
@iamdivine

Will work on Windows Vista and newer. The QueryFullProcessImageName function is used .

#include <windows.h>
#include <tlhelp32.h>

int _tmain(int argc, _TCHAR* argv[])
{
    PROCESSENTRY32 entry;
    entry.dwSize = sizeof(PROCESSENTRY32);

    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

    if (Process32First(snapshot, &entry) == TRUE) {
        while (Process32Next(snapshot, &entry) == TRUE) {
            if (_tcsicmp(entry.szExeFile, _T("explorer.exe")) == 0) {
                HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, entry.th32ProcessID);

                if (hProcess) {
                    TCHAR path[MAX_PATH];
                    DWORD cchExeName = MAX_PATH;
                    
                    if (QueryFullProcessImageName(hProcess, 0, path, &cchExeName) != 0) {
                        _tprintf(_T("%s\r\n"), path);
                    } 

                    CloseHandle(hProcess);
                }   
            }
        }
    }

    CloseHandle(snapshot);

    return 0;
}

K
Konstantin Tsvetkov, 2018-11-16
@tsklab

Find HANDLE. GetModuleFileNameExA function .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question