How to run a single instance of an application (part 1 of 4)

Contents, abstract and overview



This article discusses how to ensure that just a single instance of an application can be run. An application does this by checking if an instance is already running and terminating it if so. We also look at how a duplicate instance can pass its command line parameters to the existing instance before terminating.


Mutexes can be used to detect muliple application instances and memory mapped files can be used to exchange data. These techniques are not persued here.

The method we will use to prevent multiple application instances is based on the detection of an application with a known main window class. Once such an application is detected we then pass any command line data via the WM_COPYDATA message.

Listing 1 outlines the methodology we will be using:

search for a top level window of the correct class
if such a window exists
  activate the window
  pass any command line data to the window using WM_COPYDATA
  terminate this application instance
  start this application as normal
  process the command line parameters

We will discuss an initial approach to this problem in the next part.