1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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)
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_SNGLINST_CHECKER
30 #include "wx/string.h"
34 #include "wx/snglinst.h"
36 #define INCL_DOSSEMAPHORES
37 #define INCL_DOSERRORS
40 #include "wx/os2/private.h"
42 // ----------------------------------------------------------------------------
43 // wxSingleInstanceCheckerImpl: the real implementation class
44 // ----------------------------------------------------------------------------
46 class WXDLLEXPORT wxSingleInstanceCheckerImpl
49 wxSingleInstanceCheckerImpl()
52 m_anotherRunning
= false;
55 bool Create(const wxString
& name
)
58 semName
.Printf(wxT("\\SEM32\\%s"), name
.c_str());
59 int rc
= DosCreateMutexSem(semName
.c_str(), &m_hMutex
, DC_SEM_SHARED
, 1);
61 if ( rc
== NO_ERROR
) {
62 m_anotherRunning
= false;
64 } else if ( rc
== ERROR_DUPLICATE_NAME
) {
65 m_anotherRunning
= true;
68 m_anotherRunning
= false; // we don't know for sure in this case
69 wxLogLastError(wxT("DosCreateMutexSem"));
74 bool IsAnotherRunning() const
76 return m_anotherRunning
;
79 ~wxSingleInstanceCheckerImpl()
83 if ( !::DosCloseMutexSem(m_hMutex
) )
85 wxLogLastError(wxT("DosCloseMutexSem"));
91 // if true, creating the mutex either succeeded
92 // or we know it failed because another app is running.
93 bool m_anotherRunning
;
95 // the mutex handle, may be NULL
98 DECLARE_NO_COPY_CLASS(wxSingleInstanceCheckerImpl
)
101 // ============================================================================
102 // wxSingleInstanceChecker implementation
103 // ============================================================================
105 bool wxSingleInstanceChecker::Create(const wxString
& name
,
106 const wxString
& WXUNUSED(path
))
108 wxASSERT_MSG( !m_impl
,
109 wxT("calling wxSingleInstanceChecker::Create() twice?") );
111 // creating unnamed mutex doesn't have the same semantics!
112 wxASSERT_MSG( !name
.empty(), wxT("mutex name can't be empty") );
114 m_impl
= new wxSingleInstanceCheckerImpl
;
116 return m_impl
->Create(name
);
119 bool wxSingleInstanceChecker::DoIsAnotherRunning() const
121 wxCHECK_MSG( m_impl
, false, wxT("must call Create() first") );
123 return m_impl
->IsAnotherRunning();
126 wxSingleInstanceChecker::~wxSingleInstanceChecker()
131 #endif // wxUSE_SNGLINST_CHECKER