15

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?

Brandon
  • 251
  • 1
  • 6

3 Answers3

15

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:

  1. Specific TCP or UDP ports.
  2. Mutually exclusive locks ("mutexes") or semaphores (those two are often related on OS level)
  3. Files (log files are often the most obvious choice)

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.

Martin Sojka
  • 5,709
  • 30
  • 30
4

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.

3

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.

SourceTurtle
  • 296
  • 2
  • 3