编辑

刚刚测试了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();
}


这可能是驱动程序问题,因为只有一台设备可以运行(用于屏幕快照的虚拟驱动程序,而不是实际的网络摄像头输入),但是我已经更新,卸载并重新安装了,所以没有运气。有任何想法吗?

最佳答案

将指针保存在goodMoniker中后,请在下面几行中释放对象(pMoniker-> Release())。现在,goodMoniker指向一个释放的对象。您应该增加了其引用计数。

09-06 21:48