]>
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 | { | |
dc874fb4 | 55 | m_hMutex = ::CreateMutex(NULL, FALSE, name.t_str()); |
68a602fc VZ |
56 | if ( !m_hMutex ) |
57 | { | |
9a83f860 | 58 | wxLogLastError(wxT("CreateMutex")); |
68a602fc | 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, |
9a83f860 | 72 | wxT("can't be called if mutex creation failed") ); |
68a602fc VZ |
73 | |
74 | return m_wasOpened; | |
75 | } | |
76 | ||
77 | ~wxSingleInstanceCheckerImpl() | |
78 | { | |
79 | if ( m_hMutex ) | |
80 | { | |
81 | if ( !::CloseHandle(m_hMutex) ) | |
82 | { | |
9a83f860 | 83 | wxLogLastError(wxT("CloseHandle(mutex)")); |
68a602fc VZ |
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 | 94 | |
c0c133e1 | 95 | wxDECLARE_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, | |
9a83f860 | 106 | wxT("calling wxSingleInstanceChecker::Create() twice?") ); |
68a602fc VZ |
107 | |
108 | // creating unnamed mutex doesn't have the same semantics! | |
9a83f860 | 109 | wxASSERT_MSG( !name.empty(), wxT("mutex name can't be empty") ); |
68a602fc VZ |
110 | |
111 | m_impl = new wxSingleInstanceCheckerImpl; | |
112 | ||
113 | return m_impl->Create(name); | |
114 | } | |
115 | ||
956b3d92 | 116 | bool wxSingleInstanceChecker::DoIsAnotherRunning() const |
68a602fc | 117 | { |
9a83f860 | 118 | wxCHECK_MSG( m_impl, false, wxT("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 |