How to find instance of a program?

In the program I want to write, I need a reliable method in which to check to see if a particular program is running. I thought about looping through the windows processes and getting the window title of each process ID. However I think even with that method I am still prone to not finding the program. What do you guys think? Is there a reliable method to finding a running instance of a program? The program has it's own API. I suppose I could try and get a reference to the programs object model in some way, but if that fails, what other methods could I use?
You can check if a certain named binary is loaded in memory using "CreateToolhelp32Snapshot", "Process32First" and "Process32Next" and checking the szExeFile property for the 'PROCESSENTRY32' struct that the last two fill out:

- https://msdn.microsoft.com/en-us/library/windows/desktop/ms682489(v=vs.85).aspx
- https://msdn.microsoft.com/en-us/library/windows/desktop/ms684834(v=vs.85).aspx
- https://msdn.microsoft.com/en-us/library/windows/desktop/ms684836(v=vs.85).aspx
- https://msdn.microsoft.com/en-us/library/windows/desktop/ms684839(v=vs.85).aspx

But this would have to be scheduled to run periodically. See if autolaunching your process as a debugger works better for you: https://msdn.microsoft.com/en-us/library/a329t4ed(v=vs.100).aspx
Hey this looks great, thank you for the links as well. Found some useful code to test. What if however, there was more than one instance open? If there were 2 or 3 instances of a program open, how would you tell them apart? Could you do that using this method?
Hi, so I have been testing the code examples above. Specifically in this link here:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms686701(v=vs.85).aspx

However so far, the code isn't working as I hoped. It does not return all of the running processes on my machine. It only returns a few of them. I thought perhaps that the problem might be because I am compiling the code as a 32 bit console application. So I tried changing the Solution Platform in Visual Studio 2015 to x64. However that didn't improve the results. It yielded less processes. I find if Solution Platform is on x86, it actually yields more processes. I read online that in order to find x64 bit processes you have to run the code from a x64 bit process.

Anyway, I'm not sure what I am doing wrong. Any help would be appreciated. I have spent a long time trying to get this code to give me all of the processes. It would be nice to finally be done with it.
Last edited on
Have to tried using a program like WinSpy++ or sth. similar to see if maybe this particular program has a unique class or window name you can search for?

http://www.catch22.net/software/winspy-17
What about the following statement from the following MSDN article:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms682489(v=vs.85).aspx

"Note that you can use the QueryFullProcessImageName function to retrieve the full name of an executable image for both 32- and 64-bit processes from a 32-bit process."

Can that function get me 64 bit processes?
Topic archived. No new replies allowed.