Hi,
Can anyone tell me how to check for open programs? For example, at the start of my program I would like to check if there is another instance of my program running. Does anyone know how to do that? Thanks.
Hi,
Can anyone tell me how to check for open programs? For example, at the start of my program I would like to check if there is another instance of my program running. Does anyone know how to do that? Thanks.
You could probably use findwindow and set the code up before window creation, though there's probably better methods of doing it.
Code:HWND find; find = FindWindow(NULL, "MyWindow name"); if (find) // or try find !=NULL { MessageBox(NULL,"hey, you're already using this program!", "Error",MB_TOPMOST); return 0; }
Put this at the start of your program.
That will work even if there is a different program running that has the same window name as you.Code:CreateMutex(NULL, FALSE, "MYPROGRAMNAME"); if (GetLastError() == ERROR_ALREADY_EXISTS) return 1;
Thanks for the help. However, would this work even if the program was hidden? (For example, if it was doing work in the background). By hidden I mean in the taskbar, but with no display window.
The one i posted will. It can even be a console program (doesnt have to have any window at all).
As 39ster has said, create a mutex -- See MSDN (http://msdn2.microsoft.com/en-us/lib...11(VS.85).aspx)
Don't forget to free the mutex when you're done, not that it matters -- it is good practice.
I only want to add that you should not restrict users to run your program several times unless absolutely necessary.
Many, I think (including me), get incredibly annoyed just because I can't open another instance because the program says I can't. Who is to decide here? Me or the application?
I always use an approach that gives the user full control, but perhaps warns a bit if it might be dangerous.
As a programmer, it seems cool to do things like everyone else does. Stop a program from running multiple instances, a nice splash screen, credits, etc. But the user does not enjoy those.
Ponder on that one.
You think you can run several windows Messagers on the same comp?
Or several programs that should start listening on one "well-known" port?
Or several programs that should open the same COM-port to configure the external device?
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
But that should, really, be restricted by the single resource that those two apps would require to use. I could for example run a terminal program using COM1, and another one using COM2. Or two different messenger applications using different ports, two mail servers using individual ports, etc, etc. Obviously, if I try to use COM1 from two different apps, I definitely would like to know about it - but that should be the case even if I try to use both teraterm and hyperterminal on COM1, not only if I try to use two instances of teraterm or hyperterminal. And the OS is quite competent on telling you if the resource is busy in these cases, so it shouldn't be a problem.
There are a few instances where there is no resource that prevents the app from running, where you still want to ensure that the same app is not running - in which case an "invented" resource such as a Mutex or other named object could be used (lock-files are the common way to do this in a portable way - instance 1 creates a file, instance 2 tries to creat the same file, and fails because instance 1 got there first).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
No, that's partly the responsibility of the programmer to decide if running multiple instances of the program would cause woes to the end-user. Who knows the software better than the author? If there are valid reasons to prevent multiple instances from being launched, by all means, preventing it is smart programming.