From 5271aca5266f077fa025e9ac68f5998460792e52 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Mon, 23 May 2016 10:33:44 +0200 Subject: [PATCH] Double application launch prevention lock added (fixes #24) --- ZRCola/zrcolaapp.cpp | 37 +++++++++++++++++++++++++++++++++++++ ZRCola/zrcolaapp.h | 9 +++++++++ 2 files changed, 46 insertions(+) diff --git a/ZRCola/zrcolaapp.cpp b/ZRCola/zrcolaapp.cpp index 77519e8..7779c03 100644 --- a/ZRCola/zrcolaapp.cpp +++ b/ZRCola/zrcolaapp.cpp @@ -32,6 +32,9 @@ wxIMPLEMENT_APP(ZRColaApp); ZRColaApp::ZRColaApp() : m_mainWnd(NULL), +#ifdef __WXMSW__ + m_running(NULL), +#endif wxApp() { } @@ -66,6 +69,25 @@ bool ZRColaApp::OnInit() wxVERIFY(m_locale.AddCatalog(wxT("ZRCola-zrcdb"))); } +#ifdef __WXMSW__ + // Create global event. + m_running = ::CreateEvent(NULL, FALSE, FALSE, _T(ZRCOLA_CFG_APPLICATION) _T("{BBDE7AAD-29B6-4B83-ADA1-92AFA81A0883}")); + wxASSERT(m_running); + if (::GetLastError() == ERROR_ALREADY_EXISTS) { + // ZRCola is already running. Find its window. + HWND okno = ::FindWindow(_T("wxWindowNR"), _("ZRCola")); + if (okno) { + if (::IsIconic(okno)) + ::SendMessage(okno, WM_SYSCOMMAND, SC_RESTORE, 0); + ::SetActiveWindow(okno); + ::SetForegroundWindow(okno); + + // Not an error condition actually; Just nothing else to do... + return false; + } + } +#endif + std::fstream dat((LPCTSTR)GetDatabasePath(), std::ios_base::in | std::ios_base::binary); if (dat.good()) { if (stdex::idrec::find(dat, ZRCOLA_DB_ID, sizeof(ZRCola::recordid_t))) { @@ -145,3 +167,18 @@ bool ZRColaApp::OnInit() return true; } + + +int ZRColaApp::OnExit() +{ + int res = wxApp::OnExit(); + +#ifdef __WXMSW__ + if (m_running) { + wxVERIFY(::CloseHandle(m_running)); + m_running = NULL; + } +#endif + + return res; +} diff --git a/ZRCola/zrcolaapp.h b/ZRCola/zrcolaapp.h index 77339d9..544e334 100644 --- a/ZRCola/zrcolaapp.h +++ b/ZRCola/zrcolaapp.h @@ -52,6 +52,12 @@ public: /// virtual bool OnInit(); + /// + /// Called when application uninitializes. + /// + /// \returns Result code to return to OS + /// + virtual int OnExit(); /// /// \returns Path to ZRCola.zrcdb file @@ -70,6 +76,9 @@ public: protected: wxLocale m_locale; ///< Current locale +#ifdef __WXMSW__ + HANDLE m_running; ///< Global Win32 event to determine if another instance of ZRCola is already running +#endif };