5

Is there a way I can force an application on Windows to be a single-instance application, i.e., not launch if another instance of the same application is already running?

Ideally, I want to be able to do this without needing to replace the actual application with a stub, but I would not mind using one if really necessary.

1
  • The reason why it is so hard is because you are essentially modifying the Application's memory, without changing its logic. It is like if someone came into your house and rearranged all your furniture and you came in and just collapse where you thought your bed was. . . At best, the application will churn along like nothing happened. At worst, you get silent data corruption. IMO, neither are worth the time.
    – surfasb
    Jul 29, 2011 at 19:06

2 Answers 2

5

Normally you have to implement that "singleton" feature right into the application. If you want to guard it "from the outside", you have to use a wrapper which checks if you have another instance running already. There might be more solutions to this, but I once did the trick with nsis. The idea is this:

  • create a "fake installer" which itself is only a little wrapper around the application you want to start
  • make that installer a singleton. See here Allow only one installer instance or Check whether your application is running
  • launch your application from that "wrapper installer". See here ExecCmd plug-in
  • "hide" the normal application icon from the user and present that fake installer. Since you can add a nice looking icon to it, you should be able to make it look like the original program.
5
  • Pretty much what I was afraid of. Is there an easy way to create a "generic" wrapper? From the docs, it seems that every wrapper instance will share the same ID, which means I will need to build one wrapper per application.
    – user34993
    Jul 29, 2011 at 10:42
  • you could use nsis.sourceforge.net/… as the base for such a generic wrapper. see ${processName}. but thats the same as just monitoring all processes and check, if there is already a binary running pointing to yourapplication.exe. why do you need such singleton-thing for all the apps anyway?
    – akira
    Jul 29, 2011 at 11:17
  • Not all, but multiple. Especially ones that launch from keyboard shortcuts. I recently killed Windows by accidentally launching about a hundred copies of Task Manager.
    – user34993
    Jul 29, 2011 at 11:38
  • switch to win7 :)
    – akira
    Jul 29, 2011 at 11:43
  • Recommend building the checks into the program itself i.e. Akira's description of the normal way. Otherwise, risk people pinning the app to the quick launch panel, bypassing the fake wrapper installer. An ideal solution would be for this to be configurable from Windows, so that the app could specify whether it should be the sole instance, and also an admin. can specify whether it should be the sole instance by registration. Similar to how a file ext. is registered to have an icon, a default program and a list of rec. apps. I'd be interested to hear other people's opinions too. Aug 25, 2017 at 10:26
2

Unless there is a workaround you can apply with the Application Compatibility Kit that enables this – which I doubt (it isn't the kind of thing that itself breaks compatibility1) – then there is no setting in Windows.

Other than a launcher application the other approach would be to monitor for new processes (eg. WMI instance creation events for Win32_Process objects – filtered by session of course) and perform some action to block the second instance.


1 Of course a fix might be needed where an application uses an unsupported mechanism to detect multiple instances of itself so the detection mechanism needs a shim.

You must log in to answer this question.