Why isn't my GetProcessID function compiling in VS?
By : Alex Camargo
Date : September 15 2020, 05:00 PM
help you fix your problem The failing project is compiled as Unicode. When you do that, strings in PROCESSENTRY32 are defined as WCHAR  rather than char . One solution would be to explicitly code PROCESSENTRY32A (and Process32FirstA / Process32NextA), although Unicode strings are better able to represent non-ASCII characters.
With these it helps Your Get Process ID function will return the first process with the matching executable filename. That is why it's returning the parent process, because it's the first one to match the string comparison. If you have 2 running processes with the same name this will happen. Not much you can do about that. Also here is some slightly better code (using unicode)
DWORD GetProcId(const wchar_t* targetProcess)
DWORD procId = 0;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE)
procEntry.dwSize = sizeof(procEntry);
if (Process32First(hSnap, &procEntry))
if (!wcscmp(procEntry.szExeFile, targetProcess))
procId = procEntry.th32ProcessID;
} while (Process32Next(hSnap, &procEntry));