]>
Commit | Line | Data |
---|---|---|
68a602fc VZ |
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> | |
65571936 | 10 | // License: wxWindows licence |
68a602fc VZ |
11 | /////////////////////////////////////////////////////////////////////////////// |
12 | ||
13 | // ============================================================================ | |
14 | // declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
68a602fc VZ |
21 | // For compilers that support precompilation, includes "wx.h". |
22 | #include "wx/wxprec.h" | |
23 | ||
24 | #ifdef __BORLANDC__ | |
25 | #pragma hdrstop | |
26 | #endif | |
27 | ||
28 | #if wxUSE_SNGLINST_CHECKER && defined(__WIN32__) | |
29 | ||
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/string.h" | |
32 | #include "wx/log.h" | |
33 | #endif //WX_PRECOMP | |
34 | ||
35 | #include "wx/snglinst.h" | |
36 | ||
37 | #include "wx/msw/private.h" | |
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // wxSingleInstanceCheckerImpl: the real implementation class | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
163b3ad7 | 43 | class WXDLLIMPEXP_BASE wxSingleInstanceCheckerImpl |
68a602fc VZ |
44 | { |
45 | public: | |
46 | wxSingleInstanceCheckerImpl() | |
47 | { | |
48 | // we don't care about m_wasOpened, it can't be accessed before being | |
49 | // initialized | |
50 | m_hMutex = NULL; | |
51 | } | |
52 | ||
53 | bool Create(const wxString& name) | |
54 | { | |
e0a050e3 | 55 | m_hMutex = ::CreateMutex(NULL, FALSE, name.wx_str()); |
68a602fc VZ |
56 | if ( !m_hMutex ) |
57 | { | |
58 | wxLogLastError(_T("CreateMutex")); | |
59 | ||
57f4f925 | 60 | return false; |
68a602fc VZ |
61 | } |
62 | ||
63 | // mutex was either created or opened - see what really happened | |
64 | m_wasOpened = ::GetLastError() == ERROR_ALREADY_EXISTS; | |
65 | ||
57f4f925 | 66 | return true; |
68a602fc VZ |
67 | } |
68 | ||
69 | bool WasOpened() const | |
70 | { | |
57f4f925 | 71 | wxCHECK_MSG( m_hMutex, false, |
68a602fc VZ |
72 | _T("can't be called if mutex creation failed") ); |
73 | ||
74 | return m_wasOpened; | |
75 | } | |
76 | ||
77 | ~wxSingleInstanceCheckerImpl() | |
78 | { | |
79 | if ( m_hMutex ) | |
80 | { | |
81 | if ( !::CloseHandle(m_hMutex) ) | |
82 | { | |
83 | wxLogLastError(_T("CloseHandle(mutex)")); | |
84 | } | |
85 | } | |
86 | } | |
87 | ||
88 | private: | |
89 | // the result of the CreateMutex() call | |
90 | bool m_wasOpened; | |
91 | ||
92 | // the mutex handle, may be NULL | |
93 | HANDLE m_hMutex; | |
22f3361e VZ |
94 | |
95 | DECLARE_NO_COPY_CLASS(wxSingleInstanceCheckerImpl) | |
68a602fc VZ |
96 | }; |
97 | ||
98 | // ============================================================================ | |
99 | // wxSingleInstanceChecker implementation | |
100 | // ============================================================================ | |
101 | ||
102 | bool wxSingleInstanceChecker::Create(const wxString& name, | |
d699f48b | 103 | const wxString& WXUNUSED(path)) |
68a602fc VZ |
104 | { |
105 | wxASSERT_MSG( !m_impl, | |
106 | _T("calling wxSingleInstanceChecker::Create() twice?") ); | |
107 | ||
108 | // creating unnamed mutex doesn't have the same semantics! | |
109 | wxASSERT_MSG( !name.empty(), _T("mutex name can't be empty") ); | |
110 | ||
111 | m_impl = new wxSingleInstanceCheckerImpl; | |
112 | ||
113 | return m_impl->Create(name); | |
114 | } | |
115 | ||
116 | bool wxSingleInstanceChecker::IsAnotherRunning() const | |
117 | { | |
57f4f925 | 118 | wxCHECK_MSG( m_impl, false, _T("must call Create() first") ); |
68a602fc VZ |
119 | |
120 | // if the mutex had been opened, another instance is running - otherwise we | |
121 | // would have created it | |
122 | return m_impl->WasOpened(); | |
123 | } | |
124 | ||
125 | wxSingleInstanceChecker::~wxSingleInstanceChecker() | |
126 | { | |
127 | delete m_impl; | |
128 | } | |
129 | ||
130 | #endif // wxUSE_SNGLINST_CHECKER |