Using DAO Interfaces

DAO acquires functionality through DAO Component Object Model (COM) interfaces. DAO simplifies managing COM interfaces by incorporating them into dbDAO classes such as CdbRecordset, CdbWorkspace, etc.

Incorporating an interface into a DAO object makes the interface unavailable to the programmer, but sometimes access to the interface is required. For example, using DAO in a multi-threaded apartment-model application requires access to the interface so it can be marshaled across apartments (see DAO and the Apartment-threading Model).

Normally, acquiring an interface would require you to call CoCreateInstance() with the appropriate class and interface identifiers, then manage the returned interface pointer. However, DAO has already acquired these interfaces for its own operation and offers them to you through the GetInterface() method and the LPUNKNOWN operator.

GetInterface and LPUNKNOWN are members of every DAO class. The LPUNKOWN operator looks like a type cast, and internally calls the GetInterface() method.

Syntax

LPUNKNOWNGetInterface(BOOL bAddRef = FALSE,

BOOL bThrowException = TRUE) const;

operator LPUNKNOWN(){ return GetInterface();}

Type Example Description
BOOL bAddRef = FALSE Optional. A Boolean. If TRUE, the DAO Automation interface AddRef function is called.
BOOL bThrowException = TRUE Optional. A Boolean. If TRUE, an error is thrown if an interface is not set in the target object.

Usage

#include <dbdao.h>
#include <dbdaoerr.h>
CdbDBEngine   engine;         // Declare a DAO object
DAODBEngine   *pIdben;         // Declare a DAO interface pointer
// Construct four DAO objects. Increment AddRef.
/* 1 */
// Use GetInterface to AddRef. Explicitly Release when // done.
pIdben = (DAODBEngine   *)engine.GetInterface(TRUE);
...
pIdben->Release();
/* 2 */
// Use DAO to AddRef and Release.
CdbDBEngine Engine2(engine.GetInterface(), TRUE);
Engine2.GetVersion();
/* 3 */
CdbDBEngine Engine3((LPUNKNOWN) engine, TRUE);
Engine2.GetVersion();
/* 4 */
CdbDBEngine *pEngine4 = new CdbDBEngine(pIdben, TRUE);
pEngine4->GetVersion();

Remarks

See DAO and the Apartment-threading Model for an extended example. See the "DAO OLE Automation Interfaces" section of "Using Microsoft Visual C++ and DAO" for additional remarks on DAO interfaces.