1
votes

I'm trying to use C++ and DirectShow to display my webcam... but I'm having some troubles. The following code gives me a segmentation fault on:

m_pDF->EnumPins(&pinEnum);

my cpp code:

#include <tchar.h>
#include <strsafe.h>
#include <dshow.h>
#include <atlbase.h>
#include <d3d9.h>
#include <vmr9.h>

#pragma comment(lib,"Strmiids.lib")

#define SAFE_RELEASE(x) { if (x) x->Release(); x = NULL; }

HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppEnum)
{
    // Create the System Device Enumerator.
    ICreateDevEnum *pDevEnum;   // Video and Audio interface object
    HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum)); // Creates the system device enumerator

    if (SUCCEEDED(hr))
    {
        // Create an enumerator for the category.
        hr = pDevEnum->CreateClassEnumerator(category, ppEnum, 0);  // Enumeration of 'category' objects
        if (hr == S_FALSE)
        {
            hr = VFW_E_NOT_FOUND;  // The category is empty. Treat as an error.
        }
        pDevEnum->Release(); // Deletes Enumeration object
    }
    return hr;
}

void main(void)
{
    IGraphBuilder *pGraph = NULL;
    IMediaControl *pControl = NULL; // contains methods for stopping and starting the graph
    IMediaEvent   *pEvent = NULL;   // methods for getting events from the Filter Graph Manager

    IPin *m_pCamOutPin;
    IBaseFilter *m_pDF=NULL;
    IMoniker *pM;
    IEnumMoniker *pEnum; // Enumerator object

    // Initialize the COM library.
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        printf("ERROR - Could not initialize COM library");
        return;
    }

    // Create the filter graph manager and query for interfaces.
    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;
    }

    // Bind
    hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnum);
    pEnum->Next(1, &pM, NULL);
    pM->BindToObject(0, 0, IID_IBaseFilter, (void**)m_pDF);
    pM->Release();

    hr=pGraph->AddFilter(m_pDF, L"Video Capture");

    CComPtr<IEnumPins> pinEnum;
    m_pDF->EnumPins(&pinEnum);

    hr = pinEnum->Reset();
    hr = pinEnum->Next(1, &m_pCamOutPin, NULL); 

    if (FAILED(hr))
    return;

    // control
    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

    // Build the graph. IMPORTANT: Change this string to a file on your system.
    hr = pGraph->Render(m_pCamOutPin);
    if (SUCCEEDED(hr))
    {
        // Run the graph.
        hr = pControl->Run();
        if (SUCCEEDED(hr))
        {
            // Wait for completion.
            long evCode;
            pEvent->WaitForCompletion(INFINITE, &evCode);

            // Note: Do not use INFINITE in a real application, because it
            // can block indefinitely.
        }
    }
    SAFE_RELEASE(pControl);
    SAFE_RELEASE(pEvent);
    SAFE_RELEASE(pGraph);
    CoUninitialize();
}

Any ideas on what am I doing wrong and what do I have to do to get this to work?

2
Check every HRESULT you get, there should be some failure code before you reach the crash point.Roman R.

2 Answers

1
votes

m_pDF is a pointer to IBaseFilter:

IBaseFilter *m_pDF=NULL;

BindToObject last parameter is a pointer to a pointer. Which means this functioncall is wrong:

pM->BindToObject(0, 0, IID_IBaseFilter, (void**)m_pDF);

Instead you need to pass the address of m_pDF to get a pointer to a pointer:

pM->BindToObject(0, 0, IID_IBaseFilter, (void**)&m_pDF);

It is not a problem to cast IBaseFilter** to void**, but you can't cast IBaseFilter* to void**.

0
votes

The crash should be related m_pDF, it not be a valid pointer.

you should add check for pM->BindToObject(0, 0, IID_IBaseFilter, (void**)m_pDF); to know that m_pDF is initialize sucess or not.

and you should also check hr=pGraph->AddFilter(m_pDF, L"Video Capture"); hr is ok or not.

if fail, you can get the error code know the reason.