/***********************************************************************/ /* Copyright (C) 2002 Definitive Solutions, Inc. All Rights Reserved. */ /* THIS COMPUTER PROGRAM IS PROPRIETARY AND CONFIDENTIAL TO DEFINITIVE */ /* SOLUTIONS, INC. AND ITS LICENSORS AND CONTAINS TRADE SECRETS OF */ /* DEFINITIVE SOLUTIONS, INC. THAT ARE PROVIDED PURSUANT TO A WRITTEN */ /* AGREEMENT CONTAINING RESTRICTIONS ON USE AND DISCLOSURE. ANY USE, */ /* REPRODUCTION, OR TRANSFER EXCEPT AS PROVIDED IN SUCH AGREEMENT */ /* IS STRICTLY PROHIBITED. */ /***********************************************************************/ #include "stdafx.h" #include "MySingleInstance.h" #include "MyApp.h" #include "MyLog.h" #include "Generic.h" ///////////////////////////////////////////////////////////////////////////// // MySingleInstance // Constructor. MySingleInstance::MySingleInstance() : m_hMutex(NULL) { } // Destructor. /* virtual */ MySingleInstance::~MySingleInstance() { } // Called by CMainFrame::PreCreateWindow(). CString MySingleInstance::GetClassName() const { DOMYLOGT ("ClassName returned as <%s>.\n", m_sClassName); return m_sClassName; } // Create the mutex; or if some other instance has it open, activate that one. // Called by your app's InitInstance(). HRESULT MySingleInstance::Create(UINT nID) { HRESULT hr(S_OK); // Create our class name string. This function just parses substrings // from strings that look like "abc\n123\nxyz\n999". EC_B(AfxExtractSubString(m_sClassName, Generic::LoadString(nID), 0)); // Add the word 'Class' to the end. m_sClassName += _T(" Class"); // Create the mutex. m_hMutex = ::CreateMutex(NULL, FALSE, m_sClassName); // See if that failed. if (GetLastError() != ERROR_ALREADY_EXISTS) { // Register the unique window class name so others can find it. WNDCLASS wc = { 0 }; wc.style = CS_DBLCLKS; wc.lpfnWndProc = AfxWndProc; wc.hInstance = AfxGetInstanceHandle(); wc.hIcon = LoadIcon(wc.hInstance, MAKEINTRESOURCE(nID)); // Or AFX_IDI_STD_FRAME. wc.hCursor = LoadCursor(wc.hInstance, IDC_ARROW); wc.hbrBackground = NULL; wc.lpszMenuName = NULL; // You may need to fix this. wc.lpszClassName = m_sClassName; // Register name, exit if it fails. EC_B(AfxRegisterClass(&wc)); } else { // The mutex already exists, which means an instance is already // running. Find the app and pop it up. HWND hWnd(NULL); EC_B(hWnd = ::FindWindowEx(NULL, NULL, m_sClassName, NULL)); EC_B(::ShowWindow(hWnd, SW_RESTORE)); EC_B(::BringWindowToTop(hWnd)); EC_B(::SetForegroundWindow(hWnd)); // This tells the caller to exit this app. hr = E_FAIL; m_hMutex = NULL; } return hr; }