Microsoft KB Archive/239445

= How To Obtain ObjectContext with ObjectControl Inside Visual C++ COM DLL from ASP and MTS =

Article ID: 239445

Article Last Modified on 7/2/2004

-

APPLIES TO


 * Microsoft Active Server Pages 4.0
 * Microsoft Transaction Services 2.0
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * Microsoft Internet Information Server 4.0

-



This article was previously published under Q239445



SUMMARY
This article covers the implementation of the Microsoft Transaction Server (MTS) ObjectContext object through the ObjectControl object inside a Visual C++ component under MTS control for Active Server Pages (ASP).

This implementation is extremely useful if you would like to have direct access to the ASP intrinsic objects (for example, Application, Session, Response, Request, Server).

Visual C++ provides the Active Template Library (ATL) to assist you in developing COM components. Using the ATL COM AppWizard you can very quickly create the basic framework of your component. For example, the AppWizard generates all of the code that is necessary for exposing your component as a dual interface. In addition, ATL provides a wizard for adding objects to an ATL application. The ATL Object Wizard supports both methods of accessing the ASP built-in objects. If you want your component to use the page-level event methods (OnStartPage and OnEndPage) to access the built-in objects, you should pick ActiveX Server Component as the type from the wizard's opening screen. If you want your component to use the IObjectContext interface to access the built-in objects, you should choose Microsoft Transaction Server from the initial screen.

You should now use the ObjectContext object to access the built-in objects. ObjectContext makes Internet Information Server (IIS) applications better suited for large-scale scenarios and provides support for transaction processing. It is highly recommended that you convert your existing ASP-based applications to this new approach if they are high-volume applications. This conversion is required if your applications support transaction processing.

In the following sample you take advantage of an existing class that was distributed with one of the Visual C++ samples as part of the Windows NT 4.0 Option Pack IIS SDK Documentation. The IIS Software Developer Kit programmer documentation and samples portion of the Windows NT 4.0 Option Pack must first be installed before you can complete steps in this article.

These files are available in the ...\Iissamples\Sdk\Components\C++\Intermediate subdirectory of the Internet services directory.



MORE INFORMATION
 Open Visual C++ 6.0, create a new project, and select ATL COM AppWizard. Name the project ObjectContext and click OK. Select server type Dynamic link Library and Supports MTS, click Finish, and click OK. From the Insert menu select New ATL Object. Select MS Transaction Server from the Object category, and click Next. Enter MTSObject for the short name field. Select the MTS tab, select the MTS Support IObjectControl check box, and click OK.</li> From the ClassView tab select and right-click IMTSObject interface from the ObjectContext Classes and select Add Method from the menu.</li> Enter HelloWorld for the Method Name and click OK.</li> From Windows Explorer copy and paste the following two files from the IIS SDK Documentation to your project folder:

C:\Inetpub\Iissamples\Sdk\Components\C++\Intermediate\Context.cpp

C:\Inetpub\Iissamples\Sdk\Components\C++\Intermediate\Context.h

</li> Switch back to your Visual C++ project.</li> Click the FileView tab and expand the ObjectContext files. Select.</li> Right-click Source Files. From the menu select Add Files to Folder.</li> Select Context.cpp and click OK.</li> From the FileView tab, expand the Source Files folder and double-click MTSObject.cpp to open the MTSObject.cpp file</li>  Add the following two include lines in MTSObject.cpp under the line "#include "MTSObject.h":  Implement the following code for the HelloWorld method: STDMETHODIMP CMTSObject::HelloWorld {    // the Context class is an easy way to use IIS 4's new way    // of getting instrinic objects.  The new method may seem    // more complex, but it is more powerful since obects no    // longer have to be page/session level to get access to them.
 * 1) include <initguid.h>  // add this
 * 2) include "context.h"   // and add this </li>

CContext cxt;

// Obtain a pointer to the ASP Response object if ( FAILED( cxt.Init( CContext::get_Response ) ) ) {       return E_FAIL; }   // Write Hello World with the ASP Response object HRESULT hr = cxt.Response->Write(CComVariant(OLESTR("Hello World!"))); if (FAILED(hr)) return hr; return S_OK; }                       </li> Select the FileView tab and expand the ObjectContext files.</li> Select and right-click Header Files. From the menu, select Add Files to Folder.</li> Select Context.h and click OK.</li> From the FileView tab, expand the Header Files folder and double-click Context.h to open the Context.h file</li>  Add the following two include lines in the Context.h file under the line "#define _CONTEXT_H_": </li> <li>From the Build menu select Build ObjectContext.dll or press F7.</li> <li>Add this component (DLL) to an MTS Package.</li> <li> Copy and paste the following script into a new ASP file, and save the ASP file in a virtual directory: <%   Option Explicit Dim Obj Set Obj = Server.CreateObject("ObjectContext.MTSObject") Obj.HelloWorld Set Obj = Nothing %>                       </li> <li>View the ASP file in a browser, and you'll see the following:
 * 1) include <mtx.h>       // add this
 * 2) include <asptlb.h>    // and add this

Hello World!

</li></ol>

<div class="references_section">