سؤال

I have successfully compiled and registered a direct show filter. Now I want to use it in my code. But the call to COCreateInstance returns errorcode E_NOINTERFACE.

Here is the registration code of my filter

 #include "MyFilter.h"
#include <aviriff.h>
static WCHAR g_wszName[] = L"MyFilter";
CFactoryTemplate g_Templates[] = 
{
  { 
    g_wszName,
        &CLSID_MyFilter,
        MyFilter::CreateInstance,
    NULL,
    NULL
  }
};

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


// Declare media type information.
FOURCCMap fccMap = FCC('MRLE'); 
REGPINTYPES sudInputTypes = { &MEDIATYPE_Video, &GUID_NULL };
REGPINTYPES sudOutputTypes = { &MEDIATYPE_Video, (GUID*)&fccMap };

// Declare pin information.
REGFILTERPINS sudPinReg[] = {
    // Input pin.
    { TEXT("PinInput0"), FALSE, // Rendered?
         FALSE, // Output?
         FALSE, // Zero?
         FALSE, // Many?
         0, 0, 
         1, &sudInputTypes  // Media types.
    },
    { TEXT("PinInput1"), FALSE,
        FALSE,
        FALSE,
        FALSE,
        0, 0, 1, &sudInputTypes
    },
    {
        TEXT("PinInput2"), FALSE,
            FALSE,
            FALSE,
            FALSE,
            0, 0, 1, &sudInputTypes
    },
    // Output pin.
    { 0, FALSE, // Rendered?
         TRUE, // Output?
         FALSE, // Zero?
         FALSE, // Many?
         0, 0, 
         1, &sudOutputTypes      // Media types.
    }
};

// Declare filter information.
REGFILTER2 rf2FilterReg = {
    1,                // Version number.
    MERIT_DO_NOT_USE, // Merit.
    4,                // Number of pins.
    sudPinReg         // Pointer to pin information.
};

STDAPI DllRegisterServer(void)
{
    HRESULT hr = AMovieDllRegisterServer2(TRUE);
    if (FAILED(hr))
    {
        return hr;
    }
    IFilterMapper2 *pFM2 = NULL;
    hr = CoCreateInstance(CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
            IID_IFilterMapper2, (void **)&pFM2);
    if (SUCCEEDED(hr))
    {
        hr = pFM2->RegisterFilter(
            CLSID_MyFilter,                // Filter CLSID. 
            g_wszName,                       // Filter name.
            NULL,                            // Device moniker. 
            &CLSID_VideoCompressorCategory,  // Video compressor category.
            g_wszName,                       // Instance data.
            &rf2FilterReg                    // Filter information.
            );
        pFM2->Release();
    }
    return hr;
}

STDAPI DllUnregisterServer()
{
    HRESULT hr = AMovieDllRegisterServer2(FALSE);
    if (FAILED(hr))
    {
        return hr;
    }
    IFilterMapper2 *pFM2 = NULL;
    hr = CoCreateInstance(CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
            IID_IFilterMapper2, (void **)&pFM2);
    if (SUCCEEDED(hr))
    {
        hr = pFM2->UnregisterFilter(&CLSID_VideoCompressorCategory, 
            g_wszName, CLSID_MyFilter);
        pFM2->Release();
    }
    return hr;
}


extern "C" BOOL WINAPI DllEntryPoint(HINSTANCE, ULONG, LPVOID);
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
    return DllEntryPoint((HINSTANCE)(hModule), dwReason, lpReserved);
}

And here the code that uses the filter

#include <DShow.h>
#include "MyFilter.h"
#include <streams.h>


IPin *GetPin(IBaseFilter *pFilter, PIN_DIRECTION PinDir)
{
    BOOL       bFound = FALSE;
    IEnumPins  *pEnum;
    IPin       *pPin;

    pFilter->EnumPins(&pEnum);
    while(pEnum->Next(1, &pPin, 0) == S_OK)
    {
        PIN_DIRECTION PinDirThis;
        pPin->QueryDirection(&PinDirThis);
        if (bFound = (PinDir == PinDirThis))
            break;
        pPin->Release();
    }
    pEnum->Release();
    return (bFound ? pPin : 0);  
}


int CALLBACK WinMain(
  _In_  HINSTANCE hInstance,
  _In_  HINSTANCE hPrevInstance,
  _In_  LPSTR lpCmdLine,
  _In_  int nCmdShow
)
{

    CoInitialize(NULL);
    IGraphBuilder* pGraph = NULL;
    IMediaControl* pMediaControl = NULL;
    IMediaEvent* pMediaEvent = NULL;

    HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_ALL, IID_IFilterGraph, (void **) &pGraph);

    if(hr < 0)
    {
        return -1;
    }
    IBaseFilter* pSource = NULL;
    pGraph->QueryInterface(IID_IMediaControl, (void **) pMediaControl);
    pGraph->QueryInterface(IID_IMediaEvent, (void **) pMediaEvent);
    pGraph->AddSourceFilter(TEXT("C:\\TEMP\\video1.avi"), 0, &pSource);

    IPin* pSourceOut = GetPin(pSource, PINDIR_OUTPUT);
    IBaseFilter* pAVISplitter = NULL;
    CoCreateInstance(CLSID_AviSplitter, NULL,   
                             CLSCTX_INPROC_SERVER,
                             IID_IBaseFilter,
              (void**)&pAVISplitter);

    IPin* pAvIIn = GetPin(pAVISplitter, PINDIR_INPUT);

    pGraph->AddFilter(pAVISplitter, L"Splitter");
    pGraph->Connect(pSourceOut, pAvIIn);

    IPin* pAVIOut = GetPin(pAVISplitter, PINDIR_OUTPUT);


    MyFilter* myfilter;
    hr = CoCreateInstance(CLSID_MyFilter, NULL, CLSCTX_INPROC_SERVER, IID_MyFilter, (void **)& myfilter);
    if(hr < 0)
    {
        return -1;
    }
    IPin* myfilterIn = myfilter->GetPin(0);
    IPin* myFilterOut = myfilter->GetPin(3);
    pGraph->Connect(pAVIOut, myfilterIn);

    pGraph->Render(myFilterOut);

    CoUninitialize();


    return 0;

}

Simply calls the base class:

STDMETHODIMP MyFilter::NonDelegatingQueryInterface(REFIID riid, void **ppv)
{

    return CBaseFilter::NonDelegatingQueryInterface(riid, ppv);
}

Also note that I can import my filter in graphedit

هل كانت مفيدة؟

المحلول

MyFilter::NonDelegatingQueryInterface is where you should look at

Simply calls the base class:

Compare to EzRGB24 sample, which - similarly to yours - adds custom interface. You need to update your project respectively.

//
// NonDelegatingQueryInterface
//
// Reveals IIPEffect and ISpecifyPropertyPages
//
STDMETHODIMP CEZrgb24::NonDelegatingQueryInterface(REFIID riid, void **ppv)
{
    CheckPointer(ppv,E_POINTER);

    if (riid == IID_IIPEffect) {
        return GetInterface((IIPEffect *) this, ppv);

    } else if (riid == IID_ISpecifyPropertyPages) {
        return GetInterface((ISpecifyPropertyPages *) this, ppv);

    } else {
        return CTransformFilter::NonDelegatingQueryInterface(riid, ppv);
    }

}

نصائح أخرى

See this example Add Support for COM, when NonDelegatingQueryInterface requests IID_MyFilter, you have to return a pointer to that interface.

If your filter can be added in graphedit but not your application, maybe you have a 32/64 bit problem. The filter dll/ax needs to be compiled with the same bit width as your app, this applies to any dll not just Directshow. Here's some other tips for Directshow programming in C++:

  1. Use GraphStudioNext as you can compile it with debug symbols and 32/64 bit versions
  2. Use ATL COM Smart Pointers to avoid memory leaks and messy code eg CComPtr pSmartFilter

    CComPtr<IPin> myfilterIn; myfilterIn.Attach(myfilter->GetPin(0)); // No need to Release myfilterIn as CComPtr will do it for you when it goes out of scope

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top