DAO: Using DAO in DLLs
| Overview | How Do I | FAQ | Sample | | ODBC Driver List
This article provides some guidelines for using DAO in regular DLLs. In general, you should avoid performing any DAO construction, destruction, or operations inside the DLL's InitInstance or ExitInstance functions.
This article discusses:
Calling the AfxDaoTerm function
Destroying MFC DAO objects
Calling the AfxDaoTerm Function
The function terminates the DAO database engine. In applications, AfxDaoTerm is called automatically, but in DLLs, you must explicitly invoke it before the DLL's ExitInstance function.
Some general guidelines you should follow include:
Create any MFC DAO objects after the DLL's InitInstance function.
Destroy these objects before calling AfxDaoTerm.
Call AfxDaoTerm before the DLL's ExitInstance function.
Because the AfxDaoTerm function terminates the database engine, you must call it after all MFC DAO objects have been destroyed.
Destroying MFC DAO Objects
All MFC DAO objects in the DLL must be destroyed before the call to AfxDaoTerm. This means you have to be careful about the scope of local and global DAO objects. For example, the following code will assert:
SomeExportedFunc( .. )
{
CDaoDatabase db;
db.Open( .. );
// do something
db.Close( );
AfxDaoTerm( );
}
Because the DAO object db
is a local variable, it remains in scope until SomeExportedFunc
returns. Therefore, the call to AfxDaoTerm causes an assertion because DAO terminates while db
still has scope. Similarly, a global DAO object will have scope throughout the life of the DLL, so a call to AfxDaoTerm will also result in an assertion.
To ensure that your MFC DAO objects are destroyed before calling AfxDaoTerm, avoid global objects and create local objects dynamically using the new operator:
SomeExportedFunc( .. )
{
CDaoDatabase* pDB = new CDaoDatabase;
pDB->Open( .. );
// do something
pDB->Close( );
// destroy the object with delete
delete pDB;
// can now safely terminate DAO
AfxDaoTerm( );
}
For related information, see .
See Also DAO: Where Is...