Freigeben über


Creating Visual C++ COM Components for ASP

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 Additional Resources on Creating Components 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 (NULL != pDispatch) 
                        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 ATL COM 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. Select the ActiveX Server Component type from the ATL Object Wizard's opening screen if you want your component to use the Page-level Event Methods (OnStartPage/OnEndPage), and select Microsoft COM+ Server if you want your component to use the Page-level Event Methods (OnStartPage/OnEndPage), and select Microsoft COM+ Server if you want your component to use IObjectContext to access the ASP built-in objects.

If you are developing with Visual Studio? 5.0, you may need to modify your build environment before using the COM+ ObjectContext object to access the ASP built-in objects. For example, you may need to add 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. These files are installed with the Software Developer Kit (SDK).

For information about debugging, see Debugging with Visual C++.

See Also

Other Resources

Build MTS Components with Visual Basic for Deployment in Your ASP-Based Apps

Developing Active Server Components with ATL