0

I need to find the ID of a process and I only have its name, I know that only 1 instance will be running at one time so there's not going to be a problem with multiple processes with the same name as the one i'm looking for.

I would greatly appreciate it if somebody could explain how I could go about getting the ID of a process from just its name - and code examples would be brilliant.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Ryan
  • 957
  • 5
  • 16
  • 34

2 Answers2

2

Judging from your other questions (RegSetValueEx in the title makes it somewhat obvious), I'll presume you're on Windows. For a pure Windows API solution, you can use the Toolhelp32 API to go through a snapshot of the running processes and compare their names to the name you're looking for. You have the note about only one name, but it might be beneficial in the future, or to someone else, to have all PIDs for that name, so I'll do that:

std::vector<DWORD> pids;

HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //all processes

PROCESSENTRY32W entry; //current process
entry.dwSize = sizeof entry;

if (!Process32FirstW(snap, &entry)) { //start with the first in snapshot
    return 0;
}

do {
    if (std::wstring(entry.szExeFile) == wantedProcessName) {
        pids.emplace_back(entry.th32ProcessID); //name matches; add to list
    }
} while (Process32NextW(snap, &entry)); //keep going until end of snapshot
chris
  • 60,560
  • 13
  • 143
  • 205
  • Thanks for the answer Chris, but i'm getting an error during compilation. - `Error 1 error C2440: '' : cannot convert from 'WCHAR [260]' to 'std::string' main.cpp 32 1 main` Advice? – Ryan Jan 06 '13 at 14:46
  • @user1661022, If you're using `TCHAR` and `TEXT` and all that, I'd suggest something like `lstrcmp` instead. If you're just using wide strings, use `std::wstring`. – chris Jan 06 '13 at 20:33
  • I tried doing this (I changed it to if `(wstring(entry.szExeFile) == L"test.exe")` however when getting to this portion of the code, my application just closes with no error messages. – Ryan Jan 07 '13 at 18:33
  • @user1661022, I'm nearly certain there's no fault in this code. Could you post a link to a complete program? – chris Jan 07 '13 at 18:45
  • It's worth mentioning that I had to add entry.dwSize = sizeof(PROCESSENTRY32); after the declaration of entry for this to work. – zyzof Jan 04 '14 at 10:57
  • @zyzof, Thank you very much for pointing that out. It's mentioned in the remarks of `Process32First`. I actually stole this code from part of an earlier answer I wrote for a different question, which I fixed this exact bug for a few days ago. I also made this use wide strings for good measure. – chris Jan 04 '14 at 11:27
0

In an *nix environment, man 3 sysctl.

Colselaw
  • 1,069
  • 9
  • 21
  • This is not very useful, can you add sample code with explanation to make it more clear – JOM Jan 05 '13 at 22:24
  • YGWAGAM. See http://forums.devshed.com/c-programming-42/c-function-to-find-a-process-id-and-kill-it-65395.html for an example. – Colselaw Jan 05 '13 at 22:30