Microsoft KB Archive/238100

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 13:47, 21 July 2020 by X010 (talk | contribs) (Text replacement - """ to """)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Base

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

Article ID: 238100

Article Last Modified on 11/21/2006


  • Microsoft Foundation Class Library 4.2, when used with:
    • Microsoft Windows CE Embedded Toolkit for Visual C++ 5.0
    • Microsoft Windows CE Toolkit for Visual C++ 6.0

This article was previously published under Q238100


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
        return FALSE;
        WNDCLASS wndcls;
        memset(&wndcls, 0, sizeof(WNDCLASS));   // start with NULL defaults
        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
           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
        return CWinApp::ExitInstance();
  5. Choose your target, select Rebuild All and then select Run.

Additional query words: VCCE, single, Instance

Keywords: kbhowto KB238100