SAMPLE: Ado2atl.exe Returns ADO Interfaces from COM

ID: Q186387


The information in this article applies to:


SUMMARY

Ado2atl.exe is a sample that demonstrates how to create a simple ATL COM object that returns an ADO interface. The sample also includes a Visual Basic client project that uses the ATL COM objects to create an interface to an ADO _Recordset. After extracting the files, build the Visual C++ projects first to register the COM Objects, then use Visual Basic 5.0 to build the client program.

Sample: ATL2ADO.exe Returns Disconnected ADO Recordset ID: Q200122 contains a more complete sample that returns a disconnected recordset. You will normally want to disconnect an ADO recordset before returning it.


MORE INFORMATION

The following file is available for download from the Microsoft Software Library:

~ Ado2atl.exe

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

Q119591 How to Obtain Microsoft Support Files from Online Services
You must register the ATL COM objects before you can use the Visual Basic project. The objects automatically register when built. There are two Visual C++ 5.0 projects: RkADO and RdADO2. When building these projects it may be necessary to remove the _ATL_MIN_CRT preprocessor from the setting for the release builds. To do this, from the Project menu, choose Settings, and then select the preprocessor category from the C/C++ tab. You must include the type libraries from these projects in the Visual Basic project. From the Visual Basic Project menu choose "References ..." and then select "RkADO 1.0 Type Library" and RdADO2 1.0 Type Library.

The RdADO project creates a COM object that directly returns an ADO _Recordset interface. To return an ADO interface, you must modify the IDL file the ATL wizard created. After you add an interface that returns a _Recordset, you must cut the interface from the top of the IDL file and paste it into the library section. You must also import (via importlib) the ADO DLL, which contains the ADO type lib information needed to marshal ADO interfaces. Compare the RdADO2.idl file (which requires no modification) to the RkADO.idl file that requires you to move the interface from the top of the file to the Library section. The RdADO2 project creates a COM object that returns the ADO _Recordset interface as a IDispatch interface. IDispatch interfaces can be returned directly without modification.

In order to use ADO smart pointers in the COM object implementation, you must add the following to the StdAFX.h files:


      #import <msado15.dll>     no_namespace 
While performing the MIDL step (compiling with the Microsoft Interface Definition Language compiler), you will get the following warning message:
Warning MIDL2039 : interface does not conform to [oleautomation] attribute : [ Parameter 'ppRs' of Procedure 'GetADOrs' ( Interface 'IGetRS' ) ]
This warning occurs because the ADO _Recordset interface does not directly derive from IDispatch or IUnknown. You can safely ignore this warning message.

You must copy the file Adostudent.mdb to the C:\Temp directory. You may have to change the IDL file importlib("path\msado15.dll") statement to reflect the actual location of your ADO DLL file.

You must successfully build each ATL project (so it can register the COM objects) before running the Visual Basic project.

To activate the Visual Basic client, enter a Graduation Year (88 for example). This enables the "Get RS" (Get Record Set) and Get RS Id (for Get Record Set via IDispatch) buttons.

There is minimal error checking in the Visual Basic sample. For more information, please see the following article in the Microsoft Knowledge Base:
Q167957 INFO: Extracting Error Information from ADO in VB
CAUTION: The RkADO.idl file uses the importlib directive to bring in the ADO tlb information. The problem with this is that the import occurs at run- time, not compile time. This means that every computer where this typelib is used needs to have the Msado15.dll file installed in the same location, which is not something you can always expect.

The use of the import directive, which causes the import to occur at compile time instead of the importlib directive, gets you around this issue.


REFERENCES

For additional information, please see the following articles in the Microsoft Knowledge Base:

Q181733 FILE: Adovcbtd.exe #import Using UpdateBatch and CancelBatch
Q182389 FILE: Adovcbm.exe ADO 1.5 with #import and Getrows/Bookmarks
Q172403 FILE: Adovb.exe Demonstrates How to Use ADO with Visual Basic


c) Microsoft Corporation 1998. All Rights Reserved. Contributions by Rick Anderson, Microsoft Corporation.

Additional query words: kbDSupport kbDatabase kbVBp500 kbVS97 kbADO150 kbVC500


Keywords          : 
Version           : WINDOWS:1.5,97
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: May 13, 1999