PRB: OLE DB Consumer Template Wizard Requires ATL Project

ID: Q190959


The information in this article applies to:


SYMPTOMS

If you attempt to add an ATL OLE DB Consumer class (with the ATL Object Wizard) to a non-ATL Visual C++ MFC project, you are prompted to add ATL support to the project. If you choose No, the Wizard will not continue.


CAUSE

The ATL OLE DB Consumer classes do not require ATL support; however, the ATL Object Wizard requires an ATL project.


RESOLUTION

The ATL OLE DB Consumer classes do not require full ATL support. For example, you can use an ATL OLE DB Consumer class in a console project that does not have a global CComModule object and is not set up to be an ATL COM .exe or .dll file.

If you want to add one or more OLE DB Consumer Template classes to a non- ATL project, create a temporary ATL project to create the classes and then manually transfer the class files to your non-ATL project. The ATL OLE DB Consumer Template wizard creates a single .h file each time you run it. Transfer the wizard-generated .h files to your actual project and then add the following code to the main header in your project:


   #include <atldbcli.h>
   // You must call CoInitialize before the first call to an ATL
   // OLE DB Consumer class or you will get an hresult of
   // 0x0040cba8, which is "CoInitialize has not been called."
   // Because this structure has global scope, it is constructed first
   // and destroyed last and thus automatically handles the necessary
   // COM initialization and termination calls.
   struct HandleCOM
   {
      HandleCOM()  { ::CoInitialize(NULL); }
      ~HandleCOM() { ::CoUninitialize();   }
   } _HandleCOM_; 
The wizard-generated ATL OLE DB Consumer Templates require only the Atldbcli.h ATL header file in order to compile properly.


STATUS

This behavior is by design.


MORE INFORMATION

Follow these steps to create a temporary ATL project:

  1. Open Visual Studio. On the File menu, click New.


  2. Click the Project tab and select ATL COM AppWizard.


  3. Type a name in the project box for your temporary project, and click OK.


  4. Click Finish and then click OK.


You now have a temporary ATL project. Follow these steps to create an ATL OLE DB Consumer Template class:

  1. On the Insert menu, click New ATL Object.


  2. Select Category of Data Access, select the Consumer object, and click Next.


  3. Click Select DataSource to set up your data source and select the desired table.


  4. Make a note of the name in the .H box and click OK.


The wizard creates a single .h file with all of the code necessary to create an OLE DB Consumer Template class. To use the class, just copy the wizard-generated .h file to your actual project and include Atldbcli.h in your project. You can use the temporary ATL project to generate as many classes as you need.

NOTE: You need to call CoInitialize in your application prior to opening any OLE DB Consumer Template class because these classes use COM. The HandleCOM structure in the RESOLUTION section above can be used to automatically make the appropriate COM calls.

Additional query words: kbTemplate kbATL kbDatabase kbOLEDB kbvc600


Keywords          : kbtemplate kbATL kbDatabase kbOLEDB 
Version           : WINNT:6.0
Platform          : winnt 
Issue type        : kbprb 

Last Reviewed: August 5, 1999