Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am playing a game called Starcraft II that restricts me from running multiple instances. However, when I run a second instance sandboxed in Sandboxie, I can do so perfectly fine. What might account for this? How might I replicate this behaviour?

I realise that this is a very vague question, but I'm not really sure how else to ask. How do games typically detect & prevent multiple instances from running? I have heard that a CreateMutexW hook often does the trick, but even if this were the case here, I don't know what the the mutex that Starcraft would create is named.

share|improve this question
10  
It's a programming topic that uses the game as a reference. Not meant for Gaming.SE –  Tetrad Aug 9 '11 at 2:03
2  
This question asks (a) how StarCraft works the way it does, (b) how to do this same thing, (c) how other games do this same thing, (d) whether anyone has any ideas about any of the preceding questions, and finally (e) whether anyone has played StarCraft. 'a' and 'e' are irrelevant to this SE, and would be better posed on gaming.SE. 'b' and 'c' are the same question phrased differently (and plausibly relevant here, although probably would be better posed on StackOverflow). –  Trevor Powell Aug 9 '11 at 2:07
5  
Addenum: If we're going to keep the gamedev-relevant portion of this question, we should edit the subject line so that it matches the real question ("how do I prevent users from opening multiple instances of my game") –  Trevor Powell Aug 9 '11 at 2:19
1  
The mutex could be named anything. I think some tools could let you see the name of the mutex, perhaps process explorer(?). –  CiscoIPPhone Aug 9 '11 at 7:34
7  
(a) is totally appropriate for this site. Questions like "How does $game do $specificthing?" are more than welcome ahead of the tenth "my component database MVC graph engine is not letting me shoot myself in the foot hard enough". –  user744 Aug 9 '11 at 18:10
show 1 more comment

3 Answers

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
4  
Also it may be useful to keep in mind possible security complications: "A single-instance program is it's own denial of service". –  Vigil 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
add comment

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. –  Brandon Aug 9 '11 at 21:42
    
@Jonathan: Then don't hardcode the executable name... –  BlueRaja - Danny Pflughoeft Aug 13 '11 at 22:28
    
I meant more like, what if the user copies the program and renames it & runs it twice? –  Brandon Aug 14 '11 at 4:10
add comment

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
add comment

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.