ActiveX Data Objects (ADO) In Visual C++ - An ADO Application in VC

Started by ganeshbala, Apr 21, 2008, 06:25 PM

previous topic - next topic
Go Down


An ADO Application in VC

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

#if !defined(AFX_STDAFX_H__4F895F84_E87C_11D6_BCCE_0040F42DD16F__INCLUDED_)
#define AFX_STDAFX_H__4F895F84_E87C_11D6_BCCE_0040F42DD16F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define STRICT
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400

#include <afxwin.h>
#include <afxdisp.h>

#include <atlbase.h>
//You may derive a class from CComModule and use it if you want to override
//something, but do not change the name of _Module
extern CComModule _Module;
#include <atlcom.h>

#import "c:\program files\common files\system\ado\msado15.dll" rename_namespace("USEADO") rename("EOF","EndOfFile")
using namespace USEADO;

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__4F895F84_E87C_11D6_BCCE_0040F42DD16F__INCLUDED)

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:

importlib("c:\program files\common files\system\ado\msado15.dll");

We use the import lib statement to include the msado15.dll in our IDL file. We also cut and paste the interface definition from outside the library block into the library block.

After the copy paste operation the IDL would look like this:

// AdoAccess.idl : IDL source for AdoAccess.dll

// This file will be processed by the MIDL tool to
// produce the type library (AdoAccess.tlb) and marshalling code.

import "oaidl.idl";
import "ocidl.idl";
helpstring("AdoAccess 1.0 Type Library")

library ADOACCESSLib
importlib("c:\program files\common files\system\ado\msado15.dll");

helpstring("IadoNow Interface"),
interface IadoNow : IDispatch
[id(1), helpstring("method getRecordSet")] HRESULT getRecordSet([in]BSTR tablename,[out,retval] _RecordSet **ptr);

helpstring("adoNow Class")
coclass adoNow
[default] interface IadoNow;

After editing the IDL file, we need to move on to writing our actual function, which is what we will do next.

Go Up

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens Web Designing Chennai | GinGly :: Build your Personal Website | CineBuzz :: Cinema News | My Kids Diary :: Gift your Kids Memories :: Book Website @ 349 Rs monthly
Copyright 2005 - 2021 :: IT Acumens :: All Rights Reserved. :: XML Sitemap
ITAcumens Discussion Forum with 2 lakhs post running for 15 years - Powered by IT Acumens Pro Dedicated Server

My Kids Diary