From 9023f61fc8c6bfe26a1015b312c7ff3788e41403 Mon Sep 17 00:00:00 2001 From: mjjo Date: Sat, 20 Jul 2013 23:27:29 +0000 Subject: [PATCH] --- Common/LimitSingleInstance.h | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Common/LimitSingleInstance.h diff --git a/Common/LimitSingleInstance.h b/Common/LimitSingleInstance.h new file mode 100644 index 0000000..a5347ba --- /dev/null +++ b/Common/LimitSingleInstance.h @@ -0,0 +1,38 @@ +#ifndef LimitSingleInstance_H +#define LimitSingleInstance_H + +#include + +//this code is from Q243953 in case you lose the article and wonder +//where this code came from... +class CLimitSingleInstance +{ +protected: + DWORD m_dwLastError; + HANDLE m_hMutex; + +public: + CLimitSingleInstance(TCHAR *strMutexName) + { + //be sure to use a name that is unique for this application otherwise + //two apps may think they are the same if they are using same name for + //3rd parm to CreateMutex + m_hMutex = CreateMutex(NULL, FALSE, strMutexName); //do early + m_dwLastError = GetLastError(); //save for use later... + } + + ~CLimitSingleInstance() + { + if (m_hMutex) //don't forget to close handles... + { + CloseHandle(m_hMutex); //do as late as possible + m_hMutex = NULL; //good habit to be in + } + } + + BOOL IsAnotherInstanceRunning() + { + return (ERROR_ALREADY_EXISTS == m_dwLastError); + } +}; +#endif \ No newline at end of file