ActiveX Data Objects (ADO) In Visual C++ - An ADO Application in VC (Page 3 of 5 )
Create a new ATL COM AppWizard project. Make a Dynamic Link Library, not an Executable. Once the project is created, choose New ATL object from the Insert menu. Choose Simple Object from the list. You an provide any name for this object that you desire.
Select the interface name in the Class View. Right click and choose Add Method. Enter the method name as getRecordSet. In the parameters text field enter the following string:
[in]BSTR tablename,[out,retval] _Recordset **ptr
This denotes that the first parameter to be passed is the table name. It is of BSTR type (which is a type of String). The second one is an output parameter. It denotes that this method will return a Smart Pointer of _Recordset type.
Our next step is to import the ADO library into our project. After all, we are going to use ADO to access our data source (which is in our case an MS Access database). We enter the following lines of code in the StdAfx.h file:
#import "c:\program files\common files\system\ado\msado15.dll" rename_namespace("USEADO") rename("EOF","EndOfFile") using namespace USEADO;
In the first line we use the import keyword to import msado15.dll. This is the ADO DLL, which we have to import if we wish to use any of the ADO methods. Whenever we wish to use a DLL in our project -- apart from importing it -- we need to specify its namespace name. Since we didn't create the msado15.dll file, we wouldn't know its namespace name, thus we use the rename_namespace function to rename the ADO DLL's namespace to "USEADO".
Again, there are a few syntax's or keywords that Visual C++ doesn’t recognize that are a part of the DLL. One of them is the EOF keyword, which denotes to the program that the database has no more records. Visual C++ doesn't recognize EOF, thus we rename it to something that Visual C++ recognizes. We use the rename function to rename "EOF" to "EndOfFile", which is the syntax that Visual C++ recognizes. In the second line we specify the project that we are using the USEADO namespace.
After all the editing, our StdAfx.h file would look like this:
// stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, // but are changed infrequently
Next we have to make a major change in the IDL file. You will find an IDL file in the list of source files of your project. Double click on this file and you will see the entire IDL code. The IDL file has the declaration of your method. The problem is that this method returns a smart pointer of _Recordset type. The IDL file needs to understand what a _Recordset object is, and thus we need to import the msado15.dll file into here too.
Inside the library declaration we need to add this line: