]> git.saurik.com Git - wxWidgets.git/blame - src/msw/snglinst.cpp
Better markup.
[wxWidgets.git] / src / msw / snglinst.cpp
CommitLineData
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
14f355c2 21#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
68a602fc
VZ
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
47class WXDLLEXPORT wxSingleInstanceCheckerImpl
48{
49public:
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
57f4f925 64 return false;
68a602fc
VZ
65 }
66
67 // mutex was either created or opened - see what really happened
68 m_wasOpened = ::GetLastError() == ERROR_ALREADY_EXISTS;
69
57f4f925 70 return true;
68a602fc
VZ
71 }
72
73 bool WasOpened() const
74 {
57f4f925 75 wxCHECK_MSG( m_hMutex, false,
68a602fc
VZ
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
92private:
93 // the result of the CreateMutex() call
94 bool m_wasOpened;
95
96 // the mutex handle, may be NULL
97 HANDLE m_hMutex;
22f3361e
VZ
98
99 DECLARE_NO_COPY_CLASS(wxSingleInstanceCheckerImpl)
68a602fc
VZ
100};
101
102// ============================================================================
103// wxSingleInstanceChecker implementation
104// ============================================================================
105
106bool wxSingleInstanceChecker::Create(const wxString& name,
d699f48b 107 const wxString& WXUNUSED(path))
68a602fc
VZ
108{
109 wxASSERT_MSG( !m_impl,
110 _T("calling wxSingleInstanceChecker::Create() twice?") );
111
112 // creating unnamed mutex doesn't have the same semantics!
113 wxASSERT_MSG( !name.empty(), _T("mutex name can't be empty") );
114
115 m_impl = new wxSingleInstanceCheckerImpl;
116
117 return m_impl->Create(name);
118}
119
120bool wxSingleInstanceChecker::IsAnotherRunning() const
121{
57f4f925 122 wxCHECK_MSG( m_impl, false, _T("must call Create() first") );
68a602fc
VZ
123
124 // if the mutex had been opened, another instance is running - otherwise we
125 // would have created it
126 return m_impl->WasOpened();
127}
128
129wxSingleInstanceChecker::~wxSingleInstanceChecker()
130{
131 delete m_impl;
132}
133
134#endif // wxUSE_SNGLINST_CHECKER