Microsoft KB Archive/175383

From BetaArchive Wiki
Knowledge Base

Article ID: 175383

Article Last Modified on 7/13/2004


  • Microsoft Transaction Services 1.0
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++ 6.0 Standard Edition
  • Microsoft ActiveX Data Objects 1.0
  • Microsoft ActiveX Data Objects 1.5
  • Microsoft ActiveX Data Objects 2.0
  • Microsoft ActiveX Data Objects 2.1

This article was previously published under Q175383


This article explains how to create an Active Template Library (ATL) component which runs under Microsoft Transaction Server (MTS) and uses ActiveX Data Objects (ADO) to manipulate data from SQL Server.


To create an ATL component which runs under MTS and uses ADO to access SQL Server, do the following:

  1. Use the ATL COM AppWizard to create a new DLL.
  2. Insert a new ATL COM object. Select the MTS type. Click the MTX tab, and then enable the following choices:

    • Support IObjectControl
    • Can be Pooled
    For this example, name the object "ADOComponent."
  3. In ClassView, right-click the object's interface and choose "Add Method." Type the method name, and in the Parameters field type:

          [in, out] VARIANT * returnval

    In this specific example, this argument is not necessary except to demonstrate how you might return data to the client.

  4. Use ClassView to open the .cpp file for your method. In this case, the file is named ADOComponent.cpp.
  5. Ensure that the includes listed below are in the ADOComponent.cpp file.

          #import "C:\Program Files\Common Files\SYSTEM\ADO\MSADO15.DLL" no_namespace rename("EOF", "adoEOF" )

    If you plan to reference ADO Objects as arguments to a method that is declared in ADOComponent.h, you must move the #import directive to that header file.

  6. On the Build menu, click "Set Active Configuration" and then choose the "Win32 Unicode Release MinSize" configuration.

    Other configurations will run. However, this configuration is ideal for MTS on Windows NT. If you plan to deploy this component on Windows 95 or Windows 98 (using Microsoft Transaction Server 2.0) you should choose a non-Unicode configuration.
  7. Under Project Settings, click the C/C++ tab and add the /GX flag to enable C++ exception handling. Also delete the following directive:

          /D "_ATL_MIN_CRT"

    C++ exception handling is required by ATL smart pointers.

  8. If you use Visual C++ version 5.0 then the ATL wizard inserts the following erroneous line into the Deactivate method:


    Replace this code with the following line:

  9. Using ODBC administrator, create a DSN on your computer named PUBS.

    This DSN should point to the pubs database on a SQL Server. Alternatively, you could change the SQL text in the Open statement to process data from another database.

    You may want to make this a System DSN.
  10. Insert the following code in your method and then compile:

    Note You must change UID=<username> and PWD= to the correct values before you run this code. Make sure that UID has the appropriate permissions to perform this operation on the database.

          _RecordsetPtr adoRs = NULL;
             _variant_t InVar;
             adoRs->Open( "select au_fname from authors where "
                          "au_lname = 'White'",
                          "DSN=PUBS;UID=<username>;PWD=<strong password>;",
                          adOpenForwardOnly, adLockReadOnly, adCmdText );
             InVar = adoRs->Fields->GetItem("au_fname")->Value;
             VariantCopy(returnval, &(InVar.Detach()));
                adoRs = NULL;
             return z.Error();
          return S_OK;


For more information about Microsoft Transaction Server or ADO, please consult the following web sites:

For more information about exception handling, including #import's _com_error exception, please see the following article in the Microsoft Knowledge Base:

167802 SAMPLE: EXCEPTEX Traps MFC and Win32 Structured Exceptions

Keywords: kbhowto kbdatabase kbpending KB175383