Ado2atl.exe Returns ADO Interfaces from COM (186387)



The information in this article applies to:

  • ActiveX Data Objects (ADO), when used with:
    • Microsoft Visual C++, 32-bit Editions 5.0
    • Microsoft Visual C++, 32-bit Editions 6.0

This article was previously published under Q186387

SUMMARY

The Ado2atl.exe sample demonstrates how to create a simple Active Template Library (ATL) Component Object Model (COM) object that returns an ADO recordset interface. The sample also includes a Visual Basic client project that uses the ATL COM object to create an interface to an ADO _Recordset.

After extracting the files, first build the Visual C++ projects to register the COM Objects, then use Visual Basic to build the client program.

For additional information, and for a more complete sample that returns a disconnected recordset, click the article number below to view the article in the Microsoft Knowledge Base:

200122 Sample: ATL2ADO.exe Returns Disconnected ADO Recordset

MORE INFORMATION

The following file is available for download from the Microsoft Download Center:
For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file. You must register the ATL COM objects before you can use the Visual Basic project. The objects automatically register when built. There are three Visual C++ 6.0 projects: RkADO, VC_ClientTest, and RdADO2. When building these projects, you may need to remove the _ATL_MIN_CRT preprocessor from the setting for the release builds. To do this, from the Project menu, click 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 click 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 that 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 library information needed to marshal ADO interfaces. Compare the Rdado2.idl file (which requires no modification) to the Rkado.idl file, which requires that you 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.

To use ADO smart pointers in the COM object implementation, you must add the following code to the Stdafx.h files:
      #import <msado15.dll>     no_namespace
				
NOTE: In your downloaded sample code you will not have to make these changes. Above suggested changes to the files have already been made in the sample. You will need to take following actions before compiling sample code:
  1. In RdADO2 project, open stdafx.h file. Right before #import <msado15.dll>, include this statement -- #undef EOF
  2. Click Toolsitem on top menu bar. Choose Options | Directories| Executable Files. Then add an entry for a location of msado15.dll. It is usually found in C:\Program Files\Common Files\System\ado\msado15.dll
  3. Copy the Adostudent.mdb file 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.
  4. If used with VC 6.0, it might ask you to convert the projects. Click Yes.
You get the following compiler warning, which can safely be ignored:
...ado2atl\debug\msado15.tlh(409) : warning C4146: unary minus operator applied to unsigned type, result still unsigned


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 (Get Record Set via IDispatch) buttons.

There is minimal error checking in the Visual Basic sample. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

167957 INFO: Extracting Error Information from ADO in VB

CAUTION: The Rkado.idl file uses the importlib directive to bring in the ADO typelib 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 must 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, resolves this issue.

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

200122 SAMPLE: ATL2ADO.exe Returns Disconnected ADO Recordset

181733 FILE: Adovcbtd.exe #import Using UpdateBatch and CancelBatch

182389 FILE: Adovcbm.exe ADO 1.5 with #import and Getrows/Bookmarks

172403 FILE: Adovb.exe Demonstrates How to Use ADO with Visual

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by %2, Microsoft Corporation.


Modification Type:MinorLast Reviewed:8/10/2004
Keywords:kbdownload kbDatabase kbfile kbinfo kbSample KB186387 kbAudDeveloper