How to limit 32-Bit MFC SDI applications to a single instance on WinCE

This article was previously published under Q238100
This article has been archived. It is offered "as is" and will no longer be updated.
This article disscusses how to limit 32-bit applications to a single instance on WinCE.
The reason the steps outlined in article Q141752 do not work for WinCE is because, under WinCE, wce_PreCreateWindow function prefixes "WCE_" to class name of the window. For example, if you register a class name of "MyNewClass", the actual class name you need to pass to FindWindow() is "WCE_MyNewClass".

Sample code

  1. Create a default WCE MFC AppWizard (exe)
  2. Select the ClassView tab, and go to the definition of InitInstance in the Application class. Add the code at the begining of the definition of InitInstance:
       BOOL CSampleApp::InitInstance()   {   /********************************************************************/     // If a previous instance of the application is already running,    // then activate it and return FALSE from InitInstance to    // end the execution of this instance.    CWnd *pWndPrev;     // Determine if another window with your class name exists...    if (pWndPrev = CWnd::FindWindow(_T("WCE_MyNewClass"),NULL))    {        // Bring previous instance to the foreground	pWndPrev->SetForegroundWindow();	return FALSE;    }    WNDCLASS wndcls;    memset(&wndcls, 0, sizeof(WNDCLASS));   // start with NULL defaults = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;    wndcls.lpfnWndProc = ::DefWindowProc;    wndcls.hInstance = AfxGetInstanceHandle();    wndcls.hIcon = LoadIcon(IDR_MAINFRAME); // or load a different icon    wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);    wndcls.lpszMenuName = NULL;    // Specify your own class name for using FindWindow later    wndcls.lpszClassName = _T("MyNewClass");    // Register the new class and exit if it fails    if(!AfxRegisterClass(&wndcls))    {       AfxMessageBox(_T("Class Registration Failed\n"));       return FALSE;    }   /********************************************************************/    // Other code here...   }					
  3. Find the definition of PreCreateWindow for the Main Frame and add the following code.
       BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)   {     if( !CFrameWnd::PreCreateWindow(cs) )	return FALSE;   /*************************************/      cs.lpszClass = _T("MyNewClass");   /*************************************/      return TRUE;   }					
  4. Right-click CSampleApp and add a Virtual Function ExitInstance. Add the following code in the function body:
       int CSampleApp::ExitInstance()    {    // Add your specialized code here and/or call the base class   /*********************/     ::UnregisterClass(_T("MyNewClass"),AfxGetInstanceHandle());   /*********************/        	    return CWinApp::ExitInstance();   }					
  5. Choose your target, select Rebuild All and then select Run.
VCCE, single, Instance

Article ID: 238100 - Last Review: 02/21/2014 00:37:31 - Revision: 3.1

Microsoft Foundation Class Library 4.2

  • kbnosurvey kbarchive kbhowto KB238100