BUG: AfxOleInit Returns TRUE Without Initializing OLE in a DLL

Last reviewed: March 11, 1998
Article ID: Q154320
The information in this article applies to:
  • The Microsoft Foundation Classes, included with: Microsoft Visual C++, 32-bit Edition, version 4.2

SYMPTOMS

When developing an MFC Regular DLL, you may want to use OLE without exposing an OLE object, such as displaying a dialog from within the DLL which contains an OLE control. A failure may occur when calling DoModal() in the dialog creation if you used AfxOleInit or AfxEnableControlContainer to initialize OLE within the DLL. Typically, this would be done by calling them in the InitInstance of the CWinApp derived class.

CAUSE

In an MFC Regular DLL, AfxOleInit() will not initialize OLE since MFC cannot uninitialize OLE in the DLL_PROCESS_DETACH, ExitInstance(). In that case, OLE may already have been unloaded. The decision to not initialize OLE is by design. The bug is that AfxOleInit() will return TRUE after setting m_bNeedTerm to -1. According to the documentation, AfxOleInit() returning TRUE means that OLE was initialized, which is not the case in a DLL.

RESOLUTION

In order to uninitialize OLE when initialized in the DLL, would require exported methods for initialization and uninitialization that the client would have to call, since the uninitialization would have to occur before CWinApp::ExitInstance, which is a part of DLL_PROCESS_DETACH.

A better solution is to initialize and uninitialize OLE in the client application. This can be done in MFC by calling AfxOleInit in the application's InitInstance method of the CWinApp derived class. MFC will then uninitialize OLE when exiting the application.

Alternatively, you could use the SDK to initialize and uninitialize OLE in the client application by calling OleInitialize(NULL) in the InitInstance method and OleUninitialize in the ExitInstance method.

To ensure that the container initializes OLE before using the DLL, use the sample code below to display a message to the user when OLE hasn't been initialized.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

Sample Code

/* Compile options needed: None
*/

BOOL CMyRegularDll::InitInstance() {
   #ifdef _DEBUG
   // Initialize OLE
   HRESULT hr = OleInitialize(NULL);
   if (hr == S_FALSE) {
      OleUninitialize();
   }else{
      if (hr == S_OK) {
         AfxMessageBox("OLE needs to be initialized in the Client
                       Application before using this DLL");
      }else{
         AfxMessageBox("There is a problem with initializing OLE");
      }
      return FALSE;
    }
  #endif

     // Call if using OLE Controls
     AfxEnableControlContainer();

   // Register all OLE server (factories) as running. This enables the
   // OLE libraries to create objects from other applications.
   COleObjectFactory::RegisterAll();

   // TODO: Add your specialized code here

   return TRUE;
}


Additional reference words: 4.20 kbdsi CoCreateInstance 0x800401f0
CreateDlgControls Dialog creation failed registered vcbuglist420
Keywords : MfcOLE
Technology : kbMfc; kbole
Version : winnt:4.20
Platform : winnt
Issue type : kbbug
Solution Type : kbnofix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 11, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.