Instantiate the Filter

All filters must add code to let the base classes instantiate the filter. To instantiate a filter, you must include two pieces of code in your filter: a static CreateInstance member function in the derived filter class, and a means of informing the class factory in the base classes how to access this function.

Typically, the CreateInstance member function calls the constructor for the derived filter class. The following examples show how to implement the CreateInstance member function.

CUnknown *CGargle::CreateInstance(LPUNKNOWN punk, HRESULT *phr) {

    CGargle *pNewObject = new CGargle(NAME("Gargle Filter"), punk, phr);
    if (pNewObject == NULL) {
        *phr = E_OUTOFMEMORY;
    }

    return pNewObject;
} // CreateInstance

To communicate with the class factory, declare a global array of CFactoryTemplate objects as g_Templates and provide the name of your filter, the class identifier (CLSID) of your filter, and a pointer to the static CreateInstance member function that creates your filter object.

// Needed for the CreateInstance mechanism
CFactoryTemplate g_Templates[2]=
    { { L"Gargle filter"              , &CLSID_Gargle , 
Gargle::CreateInstance          }
    , { L"Gargle filter Property Page", &CLSID_GargProp, 
GargleProperties::CreateInstance}
    };

int g_cTemplates = sizeof(g_Templates)/sizeof(g_Templates[0]);

You can add additional parameters to the CFactoryTemplate templates if you want your filter to be self-registering. For more information on this, see Registering DirectShow Filters.

Finally, link your filter to Strmbase.lib and export DllGetClassObject and DllCanUnloadNow using a .def file.

See Also

Write a Transform Filter

Last updated on Wednesday, April 13, 2005

© 2005 Microsoft Corporation. All rights reserved.