]> git.saurik.com Git - wxWidgets.git/blob - src/msw/snglinst.cpp
Somehow, setting a tint color makes gauge work :/.
[wxWidgets.git] / src / msw / snglinst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
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
42 class WXDLLIMPEXP_BASE wxSingleInstanceCheckerImpl
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 {
54 m_hMutex = ::CreateMutex(NULL, FALSE, name.t_str());
55 if ( !m_hMutex )
56 {
57 wxLogLastError(wxT("CreateMutex"));
58
59 return false;
60 }
61
62 // mutex was either created or opened - see what really happened
63 m_wasOpened = ::GetLastError() == ERROR_ALREADY_EXISTS;
64
65 return true;
66 }
67
68 bool WasOpened() const
69 {
70 wxCHECK_MSG( m_hMutex, false,
71 wxT("can't be called if mutex creation failed") );
72
73 return m_wasOpened;
74 }
75
76 ~wxSingleInstanceCheckerImpl()
77 {
78 if ( m_hMutex )
79 {
80 if ( !::CloseHandle(m_hMutex) )
81 {
82 wxLogLastError(wxT("CloseHandle(mutex)"));
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;
93
94 wxDECLARE_NO_COPY_CLASS(wxSingleInstanceCheckerImpl);
95 };
96
97 // ============================================================================
98 // wxSingleInstanceChecker implementation
99 // ============================================================================
100
101 bool wxSingleInstanceChecker::Create(const wxString& name,
102 const wxString& WXUNUSED(path))
103 {
104 wxASSERT_MSG( !m_impl,
105 wxT("calling wxSingleInstanceChecker::Create() twice?") );
106
107 // creating unnamed mutex doesn't have the same semantics!
108 wxASSERT_MSG( !name.empty(), wxT("mutex name can't be empty") );
109
110 m_impl = new wxSingleInstanceCheckerImpl;
111
112 return m_impl->Create(name);
113 }
114
115 bool wxSingleInstanceChecker::DoIsAnotherRunning() const
116 {
117 wxCHECK_MSG( m_impl, false, wxT("must call Create() first") );
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