1 /////////////////////////////////////////////////////////////////////////////// 
   2 // Name:        msw/snglinst.cpp 
   3 // Purpose:     implements wxSingleInstanceChecker class for Win32 using 
   5 // Author:      Vadim Zeitlin 
   9 // Copyright:   (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> 
  10 // License:     wxWindows licence 
  11 /////////////////////////////////////////////////////////////////////////////// 
  13 // ============================================================================ 
  15 // ============================================================================ 
  17 // ---------------------------------------------------------------------------- 
  19 // ---------------------------------------------------------------------------- 
  21 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) 
  22     #pragma implementation "snglinst.h" 
  25 // For compilers that support precompilation, includes "wx.h". 
  26 #include "wx/wxprec.h" 
  32 #if wxUSE_SNGLINST_CHECKER && defined(__WIN32__) 
  35     #include "wx/string.h" 
  39 #include "wx/snglinst.h" 
  41 #include "wx/msw/private.h" 
  43 // ---------------------------------------------------------------------------- 
  44 // wxSingleInstanceCheckerImpl: the real implementation class 
  45 // ---------------------------------------------------------------------------- 
  47 class WXDLLEXPORT wxSingleInstanceCheckerImpl
 
  50     wxSingleInstanceCheckerImpl() 
  52         // we don't care about m_wasOpened, it can't be accessed before being 
  57     bool Create(const wxString
& name
) 
  59         m_hMutex 
= ::CreateMutex(NULL
, FALSE
, name
); 
  62             wxLogLastError(_T("CreateMutex")); 
  67         // mutex was either created or opened - see what really happened 
  68         m_wasOpened 
= ::GetLastError() == ERROR_ALREADY_EXISTS
; 
  73     bool WasOpened() const 
  75         wxCHECK_MSG( m_hMutex
, FALSE
, 
  76                      _T("can't be called if mutex creation failed") ); 
  81     ~wxSingleInstanceCheckerImpl() 
  85             if ( !::CloseHandle(m_hMutex
) ) 
  87                 wxLogLastError(_T("CloseHandle(mutex)")); 
  93     // the result of the CreateMutex() call 
  96     // the mutex handle, may be NULL 
  99     DECLARE_NO_COPY_CLASS(wxSingleInstanceCheckerImpl
) 
 102 // ============================================================================ 
 103 // wxSingleInstanceChecker implementation 
 104 // ============================================================================ 
 106 bool wxSingleInstanceChecker::Create(const wxString
& name
, 
 107                                      const wxString
& WXUNUSED(path
)) 
 109     wxASSERT_MSG( !m_impl
, 
 110                   _T("calling wxSingleInstanceChecker::Create() twice?") ); 
 112     // creating unnamed mutex doesn't have the same semantics! 
 113     wxASSERT_MSG( !name
.empty(), _T("mutex name can't be empty") ); 
 115     m_impl 
= new wxSingleInstanceCheckerImpl
; 
 117     return m_impl
->Create(name
); 
 120 bool wxSingleInstanceChecker::IsAnotherRunning() const 
 122     wxCHECK_MSG( m_impl
, FALSE
, _T("must call Create() first") ); 
 124     // if the mutex had been opened, another instance is running - otherwise we 
 125     // would have created it 
 126     return m_impl
->WasOpened(); 
 129 wxSingleInstanceChecker::~wxSingleInstanceChecker() 
 134 #endif // wxUSE_SNGLINST_CHECKER