There have been many examples,posted on CodeGuru, of how to check for a previously running instance of
an application. Some used DDE, others mutexs but what struck me as the easiest suggestion was to use
a ‘shared section’ to store a flag – accessable by every instance.
The following class uses that technique, along with a couple of the ‘interlocked’ functions to make
it threadsafe. Instead of a simple flag, a count of running instances is kept. The count obtained by
the class is only a snapshot.
To use the class simply create a static instance of it. Then check the ‘Count()’ method to determine
how many instances of your application where running before the CPreviousInstance object was
Code tested on WinNT 4.0 spk 4 VC6.0 spk 2
LONG Count() const
static LONG s_count;
CPreviousInstance& operator=(const CPreviousInstance&);
// static instance count stored in a shared read/write section
LONG CPreviousInstance::s_count = 0;
m_previous = ::InterlockedIncrement(&s_count);