C++
  Home arrow C++ arrow Page 3 - ActiveX Data Objects (ADO) In Visual C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

ActiveX Data Objects (ADO) In Visual C++
By: Neville Mehta
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 33
    2002-10-27

    Table of Contents:
  • ActiveX Data Objects (ADO) In Visual C++
  • The Myriad of Data Access Technologies
  • An ADO Application in VC
  • Writing the Function
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    #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
    #endif
    #define _ATL_APARTMENT_THREADED

    #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;

    //{{AFX_INSERT_LOCATION}}
    // 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";
    [
    uuid(4F895F80-E87C-11D6-BCCE-0040F42DD16F),
    version(1.0),
    helpstring("AdoAccess 1.0 Type Library")
    ]

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

    [
    object,
    uuid(4F895F8D-E87C-11D6-BCCE-0040F42DD16F),
    dual,
    helpstring("IadoNow Interface"),
    pointer_default(unique)
    ]
    interface IadoNow : IDispatch
    {
    [id(1), helpstring("method getRecordSet")] HRESULT getRecordSet([in]BSTR tablename,[out,retval] _RecordSet **ptr);
    };

    [
    uuid(4F895F8E-E87C-11D6-BCCE-0040F42DD16F),
    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.

    More C++ Articles
    More By Neville Mehta


     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek