Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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.

share|improve this answer
5  
Also it may be useful to keep in mind possible security complications: "A single-instance program is it's own denial of service". – EnoughTea Aug 9 '11 at 13:53
    
Since it works in sandboxie, it's probably not the first. 2 or 3 are good possibilities, taking a look at what files are open to their processes will probably pinpoint it. – Doug-W Aug 9 '11 at 15:42
3  
@Doug-W I don't see how it working in sandboxie rules out #1. – AttackingHobo Aug 9 '11 at 17:33
    
@AttackingHobo unless I miss understood the complexity of sandboxie, wouldn't the fact that a port is bound outside sandboxie preclude it from binding within? Or does sandboxie provide a virtual interface for each program running inside of it? – Doug-W Aug 10 '11 at 16:29
    
@Vigil As far as I'm concerned, you may quote Raymond Chen all day, but please don't add grammatical errors to his quotes. :P (Just copy/paste them instead.) – hheimbuerger Aug 16 '11 at 9:35

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.

share|improve this answer
2  
Hehe, or if the user decides to rename the program. – Hilbert Aug 9 '11 at 21:42
    
@Jonathan: Then don't hardcode the executable name... – BlueRaja - Danny Pflughoeft Aug 13 '11 at 22:28
1  
I meant more like, what if the user copies the program and renames it & runs it twice? – Hilbert Aug 14 '11 at 4:10

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.