Microsoft KB Archive/239445

From BetaArchive Wiki
Knowledge Base


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

  1. Open Visual C++ 6.0, create a new project, and select ATL COM AppWizard.
  2. Name the project ObjectContext and click OK.
  3. Select server type Dynamic link Library and Supports MTS, click Finish, and click OK.
  4. From the Insert menu select New ATL Object.
  5. Select MS Transaction Server from the Object category, and click Next.
  6. Enter MTSObject for the short name field.
  7. Select the MTS tab, select the MTS Support IObjectControl check box, and click OK.
  8. From the ClassView tab select and right-click IMTSObject interface from the ObjectContext Classes and select Add Method from the menu.
  9. Enter HelloWorld for the Method Name and click OK.
  10. 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

  11. Switch back to your Visual C++ project.
  12. Click the FileView tab and expand the ObjectContext files. Select.
  13. Right-click Source Files. From the menu select Add Files to Folder.
  14. Select Context.cpp and click OK.
  15. From the FileView tab, expand the Source Files folder and double-click MTSObject.cpp to open the MTSObject.cpp file
  16. Add the following two include lines in MTSObject.cpp under the line "#include "MTSObject.h":

    #include <initguid.h>   // add this
    #include "context.h"    // and add this
  17. 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.
    
        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;
    }
                            
  18. Select the FileView tab and expand the ObjectContext files.
  19. Select and right-click Header Files. From the menu, select Add Files to Folder.
  20. Select Context.h and click OK.
  21. From the FileView tab, expand the Header Files folder and double-click Context.h to open the Context.h file
  22. Add the following two include lines in the Context.h file under the line "#define _CONTEXT_H_":

    #include <mtx.h>        // add this
    #include <asptlb.h>     // and add this
                            
  23. From the Build menu select Build ObjectContext.dll or press F7.
  24. Add this component (DLL) to an MTS Package.
  25. 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
    %>
                            
  26. View the ASP file in a browser, and you'll see the following:

    Hello World!


REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

Q223406 How To Create an Empty MTS Package to Add Components for ASP
Q238274 How To Obtain ObjectContext with ObjectControl Inside VB COM DLL from ASP and MTS


Keywords: kbhowto kbcodesnippet kbaspobj KB239445