I need the simplest way from C using Win32 to get the process handle of another process by its executable file name.
The process I am looking for does not have any registered window classes. I also know that if it is running there will be only one instance of it running.
Use CreateToolhelp32Snapshot, Process32First, and Process32Next to enumerate all of the processes.
Inside the PROCESSENTRY32 you can find a
szExeFilemember.You can get the process handle by calling OpenProcess with the process ID
th32ProcessIDwithin the same struct.Once you find a process matching your exe name, you can break out of your loop and obtain the handle.
Note: If you need to enumerate EVERY process no matter what the session is, you should acquire the SE_DEBUG privilege.
At the top of your main call this:
And here is the definition of
acquirePrivilegeByName:In addition to what I said above, there is an example on how to use the above Win32 API here.