Article ID: 169496
Article Last Modified on 3/2/2005
// Declare smart pointer of Recordset
_RecordsetPtr p;
void main( void )
{
// Initialize OLE.
::CoInitialize(NULL);
// Instantiate smart pointer.
HRESULT hr = p.CreateInstance( __uuidof( Recordset ) );
...
}
The next code sample, however, is not safe and generates an unhandled
exception. The global smart pointer p is both declared and instantiated (by
virtue of passing a specific uuid in the constructor):
// Declare & instantiate smart pointer of Recordset
_RecordsetPtr p( __uuidof( _Recordset ) );
void main( void )
{
// Initialize OLE
::CoInitialize(NULL);
...
}
Because p is a global variable, it is instantiated before CoInitialize is
ever called in main(). You can correct this with the following code snippet :
struct InitOle {
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} _init_InitOle_;
// Declare & instantiate smart pointer of Recordset
_RecordsetPtr p( __uuidof( _Recordset ) );
...
An instance of the struct InitOle is declared, and instantiated before p,
and, therefore, initializes OLE in it's constructor. Without this kind of
fail-safe, you will see the following error message:#import <msado15.dll> \ no_namespace \ rename( "EOF", "adoEOF" )
169498 INFO: Extracting Error Information from ADO in VC++ with #import
#include <afxdao.h> // MFC DAO // -or- #include <dbdao.h> // DAO SDK #import <msado15.dll> no_namespace ...
#include <afxdao.h>
#import <msado15.dll> \
rename_namespace("AdoNS") rename( "EOF", "adoEOF" )
void ADOOnly( void )
{
using namespace AdoNS;
_RecordsetPtr prs;
// Generates Compile Errors:
CDaoRecordset rs;
}
void MixAdoAndDao( void )
{
AdoNS::_RecordsetPtr prs;
// Compiles just fine
CDaoRecordset drs;
}
...
struct __declspec(uuid("00000274-0000-0010-8000-00aa006d2ea4"))
/* dual interface */ _Connection;
...
struct __declspec(uuid("00000275-0000-0010-8000-00aa006d2ea4"))
/* interface */ ICADOConnection;
...
struct /* coclass */ Connection;
...
Not all interfaces, such as Connection, have multiple implementations.
This depends on the typelib, but for ADO most interfaces are dual and not
implemented as interface or coclass.
... _COM_SMARTPTR_TYPEDEF(_Connection, __uuidof(_Connection)); ... _COM_SMARTPTR_TYPEDEF(ICADOConnection, __uuidof(ICADOConnection)); ...Note that no smart pointer was declared for the coclass Connection interface.
enum CursorTypeEnum
{
adOpenUnspecified = -1,
adOpenForwardOnly = 0,
adOpenKeyset = 1,
adOpenDynamic = 2,
adOpenStatic = 3
};
...
struct __declspec(uuid("00000274-0000-0010-8000-00aa006d2ea4"))
_Connection : _ADO
{
//
// Property data.
//
_declspec(property(get=GetConnectionString,
put=PutConnectionString))
_bstr_t ConnectionString;
...
//
// Wrapper methods for error-handling.
//
_bstr_t GetConnectionString ( );
void PutConnectionString (
_bstr_t pbstr );
...
//
// Raw methods provided by interface.
//
virtual HRESULT __stdcall get_ConnectionString (
BSTR * pbstr ) = 0;
virtual HRESULT __stdcall put_ConnectionString (
BSTR pbstr ) = 0;
...
};
In the preceding code fragment, the Property Data section uses declspec to
declare get and put methods for ConnectionString. The Wrapper methods
section provides methods created by #import, which wrap these methods, and
raise an _com_error exception if they are not successful. The Raw
Methods section declares the actual method that is invoked by the
interface.bstrConnect = SysAllocString( L"DSN=AdoDemo;UID=admin;PWD=sa" ); p->ConnectionString = bstrConnect;
_ConnectionPtr p;
bstrConnect
HRESULT hr = S_OK;
_ConnectionPtr pConn;
hr = pConn.CreateInstance( __uuidof( Connection ) );
if( !FAILED( hr ) )
hr = pConn->Open( L"pubs", L"sa", L"" );
182389 FILE: Adovcbm.exe ADO 1.5 with #import and Getrows/Bookmarks
184968 FILE: Adovcsp.exe Demonstrates Using Stored Procedures with ADO
186387 SAMPLE: Ado2atl.exe Returns ADO Interfaces from COM
181733 FILE: Adovcbtd.exe #import Using UpdateBatch and CancelBatch
166112 PRB: Conflict with EOF when using #import with ADO
168354 INFO: Underlying OLE and OLEDB Provider Errors Are Exposed Through ADO
Additional query words: s_mfc
Keywords: kbinfo kbdatabase kbcode kbusage KB169496