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