1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/snglinst.cpp
3 // Purpose: implements wxSingleInstanceChecker class for Win32 using
5 // Author: Vadim Zeitlin
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 && defined(__WIN32__)
30 #include "wx/string.h"
34 #include "wx/snglinst.h"
36 #include "wx/msw/private.h"
38 // ----------------------------------------------------------------------------
39 // wxSingleInstanceCheckerImpl: the real implementation class
40 // ----------------------------------------------------------------------------
42 class WXDLLIMPEXP_BASE wxSingleInstanceCheckerImpl
45 wxSingleInstanceCheckerImpl()
47 // we don't care about m_wasOpened, it can't be accessed before being
52 bool Create(const wxString
& name
)
54 m_hMutex
= ::CreateMutex(NULL
, FALSE
, name
.t_str());
57 wxLogLastError(wxT("CreateMutex"));
62 // mutex was either created or opened - see what really happened
63 m_wasOpened
= ::GetLastError() == ERROR_ALREADY_EXISTS
;
68 bool WasOpened() const
70 wxCHECK_MSG( m_hMutex
, false,
71 wxT("can't be called if mutex creation failed") );
76 ~wxSingleInstanceCheckerImpl()
80 if ( !::CloseHandle(m_hMutex
) )
82 wxLogLastError(wxT("CloseHandle(mutex)"));
88 // the result of the CreateMutex() call
91 // the mutex handle, may be NULL
94 wxDECLARE_NO_COPY_CLASS(wxSingleInstanceCheckerImpl
);
97 // ============================================================================
98 // wxSingleInstanceChecker implementation
99 // ============================================================================
101 bool wxSingleInstanceChecker::Create(const wxString
& name
,
102 const wxString
& WXUNUSED(path
))
104 wxASSERT_MSG( !m_impl
,
105 wxT("calling wxSingleInstanceChecker::Create() twice?") );
107 // creating unnamed mutex doesn't have the same semantics!
108 wxASSERT_MSG( !name
.empty(), wxT("mutex name can't be empty") );
110 m_impl
= new wxSingleInstanceCheckerImpl
;
112 return m_impl
->Create(name
);
115 bool wxSingleInstanceChecker::DoIsAnotherRunning() const
117 wxCHECK_MSG( m_impl
, false, wxT("must call Create() first") );
119 // if the mutex had been opened, another instance is running - otherwise we
120 // would have created it
121 return m_impl
->WasOpened();
124 wxSingleInstanceChecker::~wxSingleInstanceChecker()
129 #endif // wxUSE_SNGLINST_CHECKER