BUG: AfxOleInit Returns TRUE Without Initializing OLE in a DLLLast reviewed: March 11, 1998Article ID: Q154320 |
The information in this article applies to:
SYMPTOMSWhen 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.
CAUSEIn 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.
RESOLUTIONIn 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.
STATUSMicrosoft 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |