Microsoft Windows CE 3.0  

Define and Instantiate Your Filter Class

Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The following steps show you how to define and instantiate your filter class.

Determine the base classes from which to derive your filter class (and pin classes, if necessary). Typically, your transform filter class derives from the CTransformFilter, CTransInPlaceFilter, or CVideoTransformFiltertransform base classes, or from the more generic CBaseFilterclass. If you want to transform video media (especially AVI data), derive from CVideoTransformFilter. If your filter must copy the input media samples, derive from CTransformFilter. If your filter can transform the sampled media in place, derive from CTransInPlaceFilter. If you don't want the simple transform filter support provided in the transform base classes, but prefer to implement your own member functions, derive from CBaseFilter. See Determine Which Base Classes to Usefor more information.

In the following example, the filter class derives from CTransInPlaceFilter.

class CMyFilter : public
CTransInPlaceFilter

Implement the IUnknowninterface for your object.

In the public section of your filter class definition, create an instance of CUnknown, and then call the DECLARE_IUNKNOWNmacro.

public: static CUnknown *WINAPI
CreateInstance(LPUNKNOWN punk, HRESULT *phr);
DECLARE_IUNKNOWN;

Define your constructor. Also, define your Transformand CheckInputTypefunctions (this does not apply if your filter class is derived from CBaseFilter).

In the private section of your filter class definition, define your constructor by calling the constructor of the transform filter class you derived from, and then add code to perform the transform and check the input type. For example:

// Define your constructor by calling the
constructor of // CTransInPlaceFilter. CMyFilter(TCHAR *tszName,
LPUNKNOWN punk, HRESULT *phr) : CTransInPlaceFilter (tszName, punk,
CLSID_MyFilter, phr) { } // Add the transform code. HRESULT
Transform(IMediaSample *pSample){ // Transform code here } // Add
code to check the input type. HRESULT CheckInputType(const
CMediaType* mtIn) { // Input checking code here }

Implement CreateInstancefor your filter object. Typically, CreateInstancecalls the constructor of your filter class. For example:

CUnknown * WINAPI
CMyFilter::CreateInstance(LPUNKNOWN punk, HRESULT *phr) { CMyFilter
*pNewObject = new CMyFilter(NAME("Description of My Filter"), punk,
phr ); if (pNewObject == NULL) { *phr = E_OUTOFMEMORY; } return
pNewObject; }

Declare a global array g_Templatesof CFactoryTemplateobjects to inform the default class factory code how to access the CreateInstancefunction:

CFactoryTemplate g_Templates[]= { { L"My Filter"
, &CLSID_MyFilter , CMyFilter::CreateInstance // function
called by class factory , NULL , &sudMyFilter } // address of
the AMOVIESETUP_FILTER structure, // or NULL if no structure exists
}; int g_cTemplates =
sizeof(g_Templates)/sizeof(g_Templates[0]);

The g_cTemplatesvariable defines the number of class factory templates for the filter. Each of these templates provides a link between COM and the filter and are used to create the base object for the filter. At a minimum, the filter has one template that provides the address of its own CreateInstancefunction, which, when called, creates the base object.

For information about using CFactoryTemplatein registration, see How to Register DirectShow Filters.

Generate a GUID for your filter object.

For information about generating GUIDs in general, see "GUID Creation and Optimizations" and "The uuidgen Utility" in the Platform SDK.

To generate a GUID in Microsoft Visual C++ 5. x, choose Create GUIDfrom the Toolsmenu. By default, the GUIDis in DEFINE_GUID format, which is the format you want. Click the Copybutton. Put the cursor in your source file beneath the include statements, and choose Pastefrom the Editmenu. The inserted code will look like the following example, except that it will have its own unique number and CLSID. Insert the code before your class definition in the header file or main file.

// {3FA5D260-AF2F-11d0-AE9C-00A0C91F0841}
DEFINE_GUID(CLSID_MyFilter, 0x3fa5d260, 0xaf2f, 0x11d0, 0xae, 0x9c,
0x0, 0xa0, 0xc9, 0x1f, 0x8, 0x41);


 Last updated on Tuesday, May 18, 2004

© 2004 Microsoft Corporation. All rights reserved.