diff options
Diffstat (limited to 'Common/OneInstance.h')
-rw-r--r-- | Common/OneInstance.h | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/Common/OneInstance.h b/Common/OneInstance.h new file mode 100644 index 0000000..5eec36f --- /dev/null +++ b/Common/OneInstance.h @@ -0,0 +1,63 @@ +// OneInstance.h: interface for the COneInstance class. +// +////////////////////////////////////////////////////////////////////// + + +#if !defined(AFX_ONEINSTANCE_H__A3059F66_BFED_11D2_A501_0020182B97FC__INCLUDED_) +#define AFX_ONEINSTANCE_H__A3059F66_BFED_11D2_A501_0020182B97FC__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include <string> + +class COneInstance +{ +public: + + // Constructor + COneInstance(LPCSTR szID) + : sMtxName(szID) {}; + + // Activates a Window based on the following... + // szClass: Class name of Window (get from SPY) + // szTitle: Title bar of Window + bool FindAndActivate(LPCSTR szClass, LPCSTR szTitle) const + { + // Search for the Window + HWND hWnd = ::FindWindow(szClass, szTitle); + + // Check if we have any popups + HWND hChild = GetLastActivePopup(hWnd); + + bool bRet = SetForegroundWindow(hWnd) ? true : false; // bring main window to top + + if (hWnd != hChild) + SetForegroundWindow(hChild); // a pop-up window is active + // bring it to the top too + // do not run second instance + return bRet; + } + + // Checks if there is already a program loaded + // with the ID passed when creating COneInstance + bool IsAlreadyLoaded() const + { + if(sMtxName.empty()) + return false; + + // Use a Mutex and not a FindWindow because process could exist + // without a Window ( + ::CreateMutex(NULL, true, sMtxName.c_str()); + + // mutex will be automatically deleted when process ends + return (::GetLastError() == ERROR_ALREADY_EXISTS); + } + + +protected: + std::string sMtxName; +}; + +#endif // !defined(AFX_ONEINSTANCE_H__A3059F66_BFED_11D2_A501_0020182B97FC__INCLUDED_) |