Microsoft KB Archive/238100: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
 
(2 intermediate revisions by the same user not shown)
Line 58: Line 58:
== MORE INFORMATION ==
== MORE INFORMATION ==


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".
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 ===
=== Sample code ===


Line 74: Line 74:
   
   
     // Determine if another window with your class name exists...
     // Determine if another window with your class name exists...
     if (pWndPrev = CWnd::FindWindow(_T("WCE_MyNewClass"),NULL))
     if (pWndPrev = CWnd::FindWindow(_T("WCE_MyNewClass"),NULL))
     {
     {
         // Bring previous instance to the foreground
         // Bring previous instance to the foreground
     pWndPrev->SetForegroundWindow();
     pWndPrev->SetForegroundWindow();
     return FALSE;
     return FALSE;
     }
     }
     WNDCLASS wndcls;
     WNDCLASS wndcls;


     memset(&wndcls, 0, sizeof(WNDCLASS));  // start with NULL defaults
     memset(&wndcls, 0, sizeof(WNDCLASS));  // start with NULL defaults


     wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
     wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
Line 91: Line 91:
     wndcls.lpszMenuName = NULL;
     wndcls.lpszMenuName = NULL;
     // Specify your own class name for using FindWindow later
     // Specify your own class name for using FindWindow later
     wndcls.lpszClassName = _T("MyNewClass");
     wndcls.lpszClassName = _T("MyNewClass");


     // Register the new class and exit if it fails
     // Register the new class and exit if it fails
     if(!AfxRegisterClass(&wndcls))
     if(!AfxRegisterClass(&wndcls))
     {
     {
       AfxMessageBox(_T("Class Registration Failed\n"));
       AfxMessageBox(_T("Class Registration Failed\n"));
       return FALSE;
       return FALSE;
     }
     }
Line 104: Line 104:
                     </pre></li>
                     </pre></li>
<li><p>Find the definition of PreCreateWindow for the Main Frame and add the following code.</p>
<li><p>Find the definition of PreCreateWindow for the Main Frame and add the following code.</p>
<pre class="codesample">  BOOL CMainFrame::PreCreateWindow(CREATESTRUCT&amp; cs)
<pre class="codesample">  BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
   {
   {
     if( !CFrameWnd::PreCreateWindow(cs) )
     if( !CFrameWnd::PreCreateWindow(cs) )
     return FALSE;
     return FALSE;
   /*************************************/  
   /*************************************/  
     cs.lpszClass = _T(&quot;MyNewClass&quot;);
     cs.lpszClass = _T("MyNewClass");
   /*************************************/  
   /*************************************/  
     return TRUE;
     return TRUE;
Line 119: Line 119:
     // Add your specialized code here and/or call the base class
     // Add your specialized code here and/or call the base class
   /*********************/  
   /*********************/  
     ::UnregisterClass(_T(&quot;MyNewClass&quot;),AfxGetInstanceHandle());
     ::UnregisterClass(_T("MyNewClass"),AfxGetInstanceHandle());
   /*********************/           
   /*********************/           
     return CWinApp::ExitInstance();
     return CWinApp::ExitInstance();

Latest revision as of 13:47, 21 July 2020

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



APPLIES TO

  • 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

SUMMARY

This article disscusses how to limit 32-bit applications to a single instance on WinCE.

MORE INFORMATION

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
    
        wndcls.style = 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.



Additional query words: VCCE, single, Instance

Keywords: kbhowto KB238100