]> git.saurik.com Git - wxWidgets.git/blob - src/msw/snglinst.cpp
added wxSingleInstanceChecker class
[wxWidgets.git] / src / msw / snglinst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/snglinst.cpp
3 // Purpose: implements wxSingleInstanceChecker class for Win32 using
4 // named mutexes
5 // Author: Vadim Zeitlin
6 // Modified by:
7 // Created: 08.06.01
8 // RCS-ID: $Id$
9 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // License: wxWindows license
11 ///////////////////////////////////////////////////////////////////////////////
12
13 // ============================================================================
14 // declarations
15 // ============================================================================
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 #ifdef __GNUG__
22 #pragma implementation "snglinst.h"
23 #endif
24
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 #if wxUSE_SNGLINST_CHECKER && defined(__WIN32__)
33
34 #ifndef WX_PRECOMP
35 #include "wx/string.h"
36 #include "wx/log.h"
37 #endif //WX_PRECOMP
38
39 #include "wx/snglinst.h"
40
41 #include "wx/msw/private.h"
42
43 // ----------------------------------------------------------------------------
44 // wxSingleInstanceCheckerImpl: the real implementation class
45 // ----------------------------------------------------------------------------
46
47 class WXDLLEXPORT wxSingleInstanceCheckerImpl
48 {
49 public:
50 wxSingleInstanceCheckerImpl()
51 {
52 // we don't care about m_wasOpened, it can't be accessed before being
53 // initialized
54 m_hMutex = NULL;
55 }
56
57 bool Create(const wxString& name)
58 {
59 m_hMutex = ::CreateMutex(NULL, FALSE, name);
60 if ( !m_hMutex )
61 {
62 wxLogLastError(_T("CreateMutex"));
63
64 return FALSE;
65 }
66
67 // mutex was either created or opened - see what really happened
68 m_wasOpened = ::GetLastError() == ERROR_ALREADY_EXISTS;
69
70 return TRUE;
71 }
72
73 bool WasOpened() const
74 {
75 wxCHECK_MSG( m_hMutex, FALSE,
76 _T("can't be called if mutex creation failed") );
77
78 return m_wasOpened;
79 }
80
81 ~wxSingleInstanceCheckerImpl()
82 {
83 if ( m_hMutex )
84 {
85 if ( !::CloseHandle(m_hMutex) )
86 {
87 wxLogLastError(_T("CloseHandle(mutex)"));
88 }
89 }
90 }
91
92 private:
93 // the result of the CreateMutex() call
94 bool m_wasOpened;
95
96 // the mutex handle, may be NULL
97 HANDLE m_hMutex;
98 };
99
100 // ============================================================================
101 // wxSingleInstanceChecker implementation
102 // ============================================================================
103
104 bool wxSingleInstanceChecker::Create(const wxString& name,
105 const wxString& path)
106 {
107 wxASSERT_MSG( !m_impl,
108 _T("calling wxSingleInstanceChecker::Create() twice?") );
109
110 // creating unnamed mutex doesn't have the same semantics!
111 wxASSERT_MSG( !name.empty(), _T("mutex name can't be empty") );
112
113 m_impl = new wxSingleInstanceCheckerImpl;
114
115 return m_impl->Create(name);
116 }
117
118 bool wxSingleInstanceChecker::IsAnotherRunning() const
119 {
120 wxCHECK_MSG( m_impl, FALSE, _T("must call Create() first") );
121
122 // if the mutex had been opened, another instance is running - otherwise we
123 // would have created it
124 return m_impl->WasOpened();
125 }
126
127 wxSingleInstanceChecker::~wxSingleInstanceChecker()
128 {
129 delete m_impl;
130 }
131
132 #endif // wxUSE_SNGLINST_CHECKER