Starcraft II restricts me from running multiple instances, but if I run the second instance sandboxed in Sandboxie, it works. What might account for this?
How might I replicate this behavior for my own games?
Starcraft II restricts me from running multiple instances, but if I run the second instance sandboxed in Sandboxie, it works. What might account for this?
How might I replicate this behavior for my own games?
Aside from Starcraft 2, the general answer to this is: Acquire (and lock, if the acquisition itself isn't already equivalent to locking) a specific resource from the operating system. Exit the game if the acquisition/locking fails. Example of such resources are:
On the top-end, most pain-in-the-ass for the customer way, specialised hardware which (sometimes necessarily, like for some robotic control components) can only be used exclusively by a single thread can be used this way, too.
I recommend the mutex approach, but:
Another method that is often used is simply checking if another process with the same name is running.
The advantage is that it's ridiculously easy - you don't have to worry about file permissions or know what a mutex is. The downside is, you'll get a false-positive if a different program called starcraft2.exe
is running on the machine.
If you are using C++ and the Windows API, one way you can do is using FindWindow
, where you can pass parameters like the class name and/or the window name. Then if you find a match, just exit your program before you even load or create a window.