DirectShow BindToObject返回无效句柄

时间:2012-08-19 22:47:49

标签: c++ directshow

修改

刚刚测试了playcap.cpp,我也得到了同样的错误,所以我知道这不是我的代码的错误。

-

编辑2

编辑我的代码以保留goodMoniker指针。但是,同样的错误。

+if(pMoniker != goodMoniker)
+{
    pMoniker->Release();
+}

-

让我的网络摄像头与DirectShow一起使用时遇到问题。这一行:

hr = goodMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)(&pCap));

从MSVC ++ 2010 EE返回以下错误:

  

WebcamControlTest.exe中0x777ff9d2处的第一次机会异常:0xC0000008:指定了无效句柄。

这里的完整代码(几乎完全从MSDN翻录):

#include <DShow.h>
#include <iostream>

int main(void)
{
    IGraphBuilder* pGraph = NULL;
    ICaptureGraphBuilder2* pBuild = NULL;

    HRESULT hr;

    //Initialize pGraph
    hr = CoInitialize(NULL);
    hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void**) &pBuild);
    if(FAILED(hr))
    {
        printf("ERROR - Could not initialize COM library");
        return 1;
    }

    //Initialize pBuild
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **) &pGraph);
    if(FAILED(hr))
    {
        printf("ERROR - Could not create the Filter Graph Manager.");
        return 2;
    }
    pBuild->SetFiltergraph(pGraph);

    //Initialize pCap
    ICreateDevEnum* pDevEnum = NULL;
    IEnumMoniker* pEnum = NULL;

    hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, reinterpret_cast<void**>(&pDevEnum));
    if(SUCCEEDED(hr))
    {
        hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnum, 0);
    }

    IMoniker* goodMoniker = NULL; 

    HWND hList;
    IMoniker *pMoniker = NULL;
    while(pEnum->Next(1, &pMoniker, NULL) == S_OK)
    {
        IPropertyBag* pPropBag;
        hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)(&pPropBag));
        if(FAILED(hr))
        {
            pMoniker->Release();
            continue;
        }

        VARIANT varName;
        VariantInit(&varName);
        hr = pPropBag->Read(L"Description", &varName, 0);
        if(FAILED(hr))
        {
            hr = pPropBag->Read(L"FriendlyName", &varName, 0);
        }
        if(SUCCEEDED(hr))
        {
            for(int i=0;i<8;i++)
            {
                std::cout<<(char)*(varName.bstrVal + i);
            }

            char yn;
            std::cin>>yn;

            if(yn=='Y')
            {
                std::cout<<"SUCCESSFUL"<<std::endl;
                goodMoniker = pMoniker;
                VariantClear(&varName);
            }
        }
        pPropBag->Release();
        if(pMoniker != goodMoniker)
        {
            pMoniker->Release();
        }
    }

    IBaseFilter* pCap;
    hr = goodMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)(&pCap));
    if(SUCCEEDED(hr))
    {
        hr = pGraph->AddFilter(pCap, L"Capture Filter");
    }

    hr = pBuild->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pCap, NULL, NULL);
    while(SUCCEEDED(hr));

    pGraph->Release();
    pBuild->Release();
    pCap->Release();
}

这可能是一个驱动程序问题,因为有一个设备可以工作(虚拟驱动程序用于屏幕截图,而不是实际的网络摄像头输入),但我已经更新,卸载并重新安装,没有运气。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在goodMoniker中保存指针后,将对象释放几行(pMoniker-&gt; Release())。现在goodMoniker指向一个已发布的对象。你应该增加它的引用次数。