The information in this article applies to:
SYMPTOMSThis article provides some guidelines for using DAO in regular DLLs. In general, avoid performing any DAO construction, destruction, or operations inside the DLL's InitInstance or ExitInstance functions. Otherwise, you might see the following assertions:
(in Visual C++ 4.0)
(in Visual C++ 4.1) STATUSThis behavior is by design. MFC/DAO objects need to be created after CWinApp::InitInstance and destroyed before terminating DAO. By default, MFC attempts to terminate DAO within CWinApp::ExitInstance. MORE INFORMATION
The AfxDaoTerm helper function terminates the DAO database engine. In
applications, AfxDaoTerm is called automatically, but in DLLs, it must be
explicitly invoked before the DLL's ExitInstance function.
AfxDaoInitThis function initializes the DAO database engine. In most cases, you don't need to call AfxDaoInit because the application calls it when it is needed. AfxDaoInit is called during the construction of the first MFC/DAO object.AfxDaoTermThis function terminates the DAO database engine. Typically, this function only needs to be called in a DLL; an application automatically calls AfxDaTerm when it is needed.When DAO is initialized, MFC will set the m_lpfnDaoTerm pointer in the CWinApp class to point to AfxDaoTerm(). When CWinApp::ExitInstance is invoked, by default it invokes the value of m_lpfnDaoTerm if it is set. In a Regular DLL, you need to call AfxDaoTerm() before the default ExitInstance is invoked because ExitInstance is invoked by DllMain. Visual C++ 4.0 had a documented bug where the .exe's ExitInstance would shut down DAO, causing an assertion when the Regular DLL's ExitInstance was called. For additional information, please see the following article in the Microsoft Knowledge Base: Q143084 FIX: Problems with Using the MFC DAO Classes in a .DLL or .OCXAll MFC/DAO objects in the DLL must be destroyed before the call to AfxDaoTerm. Be careful about the scope of local and global DAO objects. For example, the following code causes an assert:
Because the DAO object db is a local variable, it remains in scope until
SomeExportedFunc returns. The call to AfxDaoTerm causes an assertion
because DAO terminates while db still has scope. Similarly, a global DAO
object has scope throughout the life of the DLL, so a call to AfxDaoTerm
also results in an assertion.
To ensure that your MFC/DAO objects are destroyed before calling AfxDaoTerm, avoid global objects and create local objects dynamically by using the new operator as in this example:
A variation is to export special creation and termination functions as in
the following example. The advantage of this method is that DAO is running
and connections remain open during the life of the DLL. The disadvantage is
that the user of the DLL is responsible for explicitly calling these
special functions.
Then, to terminate MFC/DAO:
REFERENCES
For related information, see Technical Note 54. Technical Notes are
available under MFC Technical Notes, under MFC Books Online.
Q140850 HOWTO: Properly Export Functions Using the MFC Shared LibraryAFX_MANAGE_STATE should be used in Regular DLLs only. Additional query words:
Keywords : kbcode kbDAO kbDatabase kbMFC kbVC kbVC400 kbVC410 kbVC420 kbVC500 kbVC600 |
Last Reviewed: August 2, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |