Implementing with Visual C++

Implementing your component with Microsoft Visual C++ provides you with a great deal of flexibility. In addition to this flexibility, Visual C++ provides you with more direct access to the mechanisms used by COM for creating and using object instances. The mechanism for creating an instance of a COM object, for example, is largely hidden from Visual Basic developers. If you are developing COM components with C++ you will need a more complete understanding of COM. See COM Concepts for additional resources on COM.

The following code snippet demonstrates accessing the Response object from within a C++ component.

STDMETHODIMP CHelloWorld::GetResponse()
{
    // Get the Object Context
    CComPtr<IObjectContext> pObjContext;
    HRESULT hr = ::GetObjectContext(&pObjContext);

    if (SUCCEEDED(hr))
    {   
        // Get the Properties interface
        CComPtr<IGetContextProperties> pProps;

        hr = pObjContext->QueryInterface(IID_IGetContextProperties,
                                         (void**) &pProps);
        if (SUCCEEDED(hr))
        {
            // Get the ASP Response object
            CComBSTR    bstrResponse("Response");
            CComVariant vt;

            hr = pProps->GetProperty(bstrResponse, &vt);
            if (SUCCEEDED(hr))
            {
                // Convert the IDispatch pointer to an IResponse pointer
                if (V_VT(&vt) == VT_DISPATCH )
                {
                    CComPtr<IResponse> piResponse;
                    IDispatch*         pDispatch = V_DISPATCH(&vt);

                    if (pDispatch != NULL)
                        hr = pDispatch->QueryInterface(IID_IResponse,
                        (void**) &piResponse);
                    if (SUCCEEDED(hr))
                        piResponse->Write(CComVariant(
                        OLESTR("Hello, World!")));
                }
            }
        }
    }

    return hr;
}

Visual C++ provides the Active Template Library (ATL), a tool 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 will generate 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/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 IObjectContext to access the built-in objects, you should choose MS Transaction Server from the initial screen.

Please note that if you are developing with Visual Studio 5.0 you may need to modify your build environment before using ObjectContext to access the ASP built-in objects. For example, you may need to copy the Component Services headers and library files. You may also need to copy asptlb.h from inetpub\iissamples\sdk\include to your development tool's include directory. Finally, the .cpp file that contains the #include command for asptlb.h must also contain #include <initguid.h> and the file must be linked with mtxguid.lib.

For complete sample components that use ObjectContext to access the ASP built-in objects, see Developer Samples.

For further information on using ATL see "Creating an Active Server Component in Visual Studio 97" and "Developing Active Server Components with ATL" on msdn.microsoft.com/workshop/.