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 license
11 ///////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
22 #pragma implementation "snglinst.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
32 #if wxUSE_SNGLINST_CHECKER && defined(__WIN32__)
35 #include "wx/string.h"
39 #include "wx/snglinst.h"
41 #include "wx/msw/private.h"
43 // ----------------------------------------------------------------------------
44 // wxSingleInstanceCheckerImpl: the real implementation class
45 // ----------------------------------------------------------------------------
47 class WXDLLEXPORT wxSingleInstanceCheckerImpl
50 wxSingleInstanceCheckerImpl()
52 // we don't care about m_wasOpened, it can't be accessed before being
57 bool Create(const wxString
& name
)
59 m_hMutex
= ::CreateMutex(NULL
, FALSE
, name
);
62 wxLogLastError(_T("CreateMutex"));
67 // mutex was either created or opened - see what really happened
68 m_wasOpened
= ::GetLastError() == ERROR_ALREADY_EXISTS
;
73 bool WasOpened() const
75 wxCHECK_MSG( m_hMutex
, FALSE
,
76 _T("can't be called if mutex creation failed") );
81 ~wxSingleInstanceCheckerImpl()
85 if ( !::CloseHandle(m_hMutex
) )
87 wxLogLastError(_T("CloseHandle(mutex)"));
93 // the result of the CreateMutex() call
96 // the mutex handle, may be NULL
100 // ============================================================================
101 // wxSingleInstanceChecker implementation
102 // ============================================================================
104 bool wxSingleInstanceChecker::Create(const wxString
& name
,
105 const wxString
& WXUNUSED(path
))
107 wxASSERT_MSG( !m_impl
,
108 _T("calling wxSingleInstanceChecker::Create() twice?") );
110 // creating unnamed mutex doesn't have the same semantics!
111 wxASSERT_MSG( !name
.empty(), _T("mutex name can't be empty") );
113 m_impl
= new wxSingleInstanceCheckerImpl
;
115 return m_impl
->Create(name
);
118 bool wxSingleInstanceChecker::IsAnotherRunning() const
120 wxCHECK_MSG( m_impl
, FALSE
, _T("must call Create() first") );
122 // if the mutex had been opened, another instance is running - otherwise we
123 // would have created it
124 return m_impl
->WasOpened();
127 wxSingleInstanceChecker::~wxSingleInstanceChecker()
132 #endif // wxUSE_SNGLINST_CHECKER