Share via


Add the Web Filter to Forefront TMG

The implementations of the DllRegisterServer and DllUnregisterServer functions in the Web Response Modifier sample both call RegisterWebFilter. RegisterWebFilter receives a Boolean value. True indicates that the filter should be added to Forefront TMG, and false indicates that it should be removed. RegisterWebFilter uses properties and methods of Forefront TMG administration COM objects.

The following RegisterWebFilter function code is based on the file WebFltrRegister.cpp, included in the Webresponsemodifier folder:

// Define the filter GUID and the filter's path relative to 
// the Forefront TMG installation folder.
const char StrGuid[] = "{87F18571-C71D-4a2f-9111-9E0927A00B51}";
const char FilterRelativePath[] = "Webresponsemodifier.dll";
HRESULT RegisterWebFilter(bool fRegister)
{
    HRESULT hr = S_OK;
    // Get instance of filter admin object.
    hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        return hr;
    }
    else
    {
        ISALIB::IFPCPtr pIISA;
        hr = CoCreateInstance(
            ISALIB::CLSID_FPC,
            NULL,
            CLSCTX_INPROC_SERVER,
            ISALIB::IID_IFPC,
            (void**)&pIISA);
        if (FAILED(hr) || (pIISA == NULL))
        {
            CoUninitialize( );
            return hr;
        }
        ISALIB::IFPCArrayPtr pIISAArr;      
        ISALIB::IFPCExtensionsPtr pIExtensions;
        ISALIB::IFPCWebFiltersPtr pWebFilters;   
        ISALIB::IFPCWebFilterPtr pFilter;
        try
        {
            pIISAArr = pIISA->GetContainingArray();      
            pIExtensions = pIISAArr->Extensions;
            pWebFilters = pIExtensions->WebFilters;   
        }
        catch(_com_error& err)
        {
            hr = err.Error();
            CoUninitialize( );
            return hr;
        }
        if (fRegister)
        {
            try
            {
                pFilter = pWebFilters->Add(StrGuid,"Web Response modifier",FilterRelativePath,ISALIB::fpcFilterPriority_Medium,ISALIB::fpcFilterDirectionBoth);
                pFilter->PutDescription("Modifies the web servers' response");
                pFilter->PutVendor("Microsoft (R) Corporation");
                pFilter->PutVersion("4.0");                          
                pFilter->Save(VARIANT_FALSE, VARIANT_TRUE);              
            }
            catch(_com_error& err)
            {
                if(err.Error() != HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS))
                {
                    hr = err.Error();
                }
            }
        else
        {
            try
            {
                pWebFilters->Remove(StrGuid);
                pWebFilters->Save(VARIANT_FALSE, VARIANT_TRUE);
                pWebFilters->Refresh();       
            }
            catch(_com_error& err)
            {
                if(err.Error() != HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
                {
                    hr = err.Error();
                }
            }
        }       
    }
    CoUninitialize( );
    return hr;
}

The call to the IFPCWebFilters::Add method also automatically creates a reference to the new FPCWebFilter object in the FPCRefs collection stored in the InstalledWebFilters property of the local Forefront TMG computer. Similarly, the call to the Remove method also automatically removes the reference to the FPCWebFilter object representing the Web filter from the FPCRefs collection stored in the InstalledWebFilters property of the local Forefront TMG computer.

Send comments about this topic to Microsoft

Build date: 6/30/2010