1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: os2/snglinst.cpp
3 // Purpose: implements wxSingleInstanceChecker class for OS/2 using
5 // Author: Vadim Zeitlin
6 // Modified by: Lauri Nurmi (modified for OS/2)
9 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // License: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
28 #if wxUSE_SNGLINST_CHECKER
31 #include "wx/string.h"
35 #include "wx/snglinst.h"
37 #define INCL_DOSSEMAPHORES
38 #define INCL_DOSERRORS
41 #include "wx/os2/private.h"
43 // ----------------------------------------------------------------------------
44 // wxSingleInstanceCheckerImpl: the real implementation class
45 // ----------------------------------------------------------------------------
47 class WXDLLEXPORT wxSingleInstanceCheckerImpl
50 wxSingleInstanceCheckerImpl()
53 m_anotherRunning
= false;
56 bool Create(const wxString
& name
)
59 semName
.Printf(wxT("\\SEM32\\%s"), name
.c_str());
60 int rc
= DosCreateMutexSem(semName
.c_str(), &m_hMutex
, DC_SEM_SHARED
, 1);
62 if ( rc
== NO_ERROR
) {
63 m_anotherRunning
= false;
65 } else if ( rc
== ERROR_DUPLICATE_NAME
) {
66 m_anotherRunning
= true;
69 m_anotherRunning
= false; // we don't know for sure in this case
70 wxLogLastError(wxT("DosCreateMutexSem"));
75 bool IsAnotherRunning() const
77 return m_anotherRunning
;
80 ~wxSingleInstanceCheckerImpl()
84 if ( !::DosCloseMutexSem(m_hMutex
) )
86 wxLogLastError(wxT("DosCloseMutexSem"));
92 // if true, creating the mutex either succeeded
93 // or we know it failed because another app is running.
94 bool m_anotherRunning
;
96 // the mutex handle, may be NULL
99 DECLARE_NO_COPY_CLASS(wxSingleInstanceCheckerImpl
)
102 // ============================================================================
103 // wxSingleInstanceChecker implementation
104 // ============================================================================
106 bool wxSingleInstanceChecker::Create(const wxString
& name
,
107 const wxString
& WXUNUSED(path
))
109 wxASSERT_MSG( !m_impl
,
110 wxT("calling wxSingleInstanceChecker::Create() twice?") );
112 // creating unnamed mutex doesn't have the same semantics!
113 wxASSERT_MSG( !name
.empty(), wxT("mutex name can't be empty") );
115 m_impl
= new wxSingleInstanceCheckerImpl
;
117 return m_impl
->Create(name
);
120 bool wxSingleInstanceChecker::IsAnotherRunning() const
122 wxCHECK_MSG( m_impl
, false, wxT("must call Create() first") );
124 return m_impl
->IsAnotherRunning();
127 wxSingleInstanceChecker::~wxSingleInstanceChecker()
132 #endif // wxUSE_SNGLINST_CHECKER