1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/snglinst.cpp
3 // Purpose: implements wxSingleInstanceChecker class for Win32 using
5 // Author: Vadim Zeitlin
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 && defined(__WIN32__)
31 #include "wx/string.h"
35 #include "wx/snglinst.h"
37 #include "wx/msw/private.h"
39 // ----------------------------------------------------------------------------
40 // wxSingleInstanceCheckerImpl: the real implementation class
41 // ----------------------------------------------------------------------------
43 class WXDLLEXPORT wxSingleInstanceCheckerImpl
46 wxSingleInstanceCheckerImpl()
48 // we don't care about m_wasOpened, it can't be accessed before being
53 bool Create(const wxString
& name
)
55 m_hMutex
= ::CreateMutex(NULL
, FALSE
, name
.wx_str());
58 wxLogLastError(_T("CreateMutex"));
63 // mutex was either created or opened - see what really happened
64 m_wasOpened
= ::GetLastError() == ERROR_ALREADY_EXISTS
;
69 bool WasOpened() const
71 wxCHECK_MSG( m_hMutex
, false,
72 _T("can't be called if mutex creation failed") );
77 ~wxSingleInstanceCheckerImpl()
81 if ( !::CloseHandle(m_hMutex
) )
83 wxLogLastError(_T("CloseHandle(mutex)"));
89 // the result of the CreateMutex() call
92 // the mutex handle, may be NULL
95 DECLARE_NO_COPY_CLASS(wxSingleInstanceCheckerImpl
)
98 // ============================================================================
99 // wxSingleInstanceChecker implementation
100 // ============================================================================
102 bool wxSingleInstanceChecker::Create(const wxString
& name
,
103 const wxString
& WXUNUSED(path
))
105 wxASSERT_MSG( !m_impl
,
106 _T("calling wxSingleInstanceChecker::Create() twice?") );
108 // creating unnamed mutex doesn't have the same semantics!
109 wxASSERT_MSG( !name
.empty(), _T("mutex name can't be empty") );
111 m_impl
= new wxSingleInstanceCheckerImpl
;
113 return m_impl
->Create(name
);
116 bool wxSingleInstanceChecker::IsAnotherRunning() const
118 wxCHECK_MSG( m_impl
, false, _T("must call Create() first") );
120 // if the mutex had been opened, another instance is running - otherwise we
121 // would have created it
122 return m_impl
->WasOpened();
125 wxSingleInstanceChecker::~wxSingleInstanceChecker()
130 #endif // wxUSE_SNGLINST_CHECKER