title: How to use DirectShow and webcam to preview an image
body: <p>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: </p>
  
  <pre><code>m_pDF-&gt;EnumPins(&amp;pinEnum);
  </code></pre>
  
  <p>my cpp code:</p>
  
  <pre><code>#include &lt;tchar.h&gt;
  #include &lt;strsafe.h&gt;
  #include &lt;dshow.h&gt;
  #include &lt;atlbase.h&gt;
  #include &lt;d3d9.h&gt;
  #include &lt;vmr9.h&gt;
  
  #pragma comment(lib,"Strmiids.lib")
  
  #define SAFE_RELEASE(x) { if (x) x-&gt;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(&amp;pDevEnum)); // Creates the system device enumerator
  
      if (SUCCEEDED(hr))
      {
          // Create an enumerator for the category.
          hr = pDevEnum-&gt;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-&gt;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 **)&amp;pGraph);
      if (FAILED(hr))
      {
          printf("ERROR - Could not create the Filter Graph Manager.");
          return;
      }
  
      // Bind
      hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &amp;pEnum);
      pEnum-&gt;Next(1, &amp;pM, NULL);
      pM-&gt;BindToObject(0, 0, IID_IBaseFilter, (void**)m_pDF);
      pM-&gt;Release();
  
      hr=pGraph-&gt;AddFilter(m_pDF, L"Video Capture");
  
      CComPtr&lt;IEnumPins&gt; pinEnum;
      m_pDF-&gt;EnumPins(&amp;pinEnum);
  
      hr = pinEnum-&gt;Reset();
      hr = pinEnum-&gt;Next(1, &amp;m_pCamOutPin, NULL); 
  
      if (FAILED(hr))
      return;
  
      // control
      hr = pGraph-&gt;QueryInterface(IID_IMediaControl, (void **)&amp;pControl);
      hr = pGraph-&gt;QueryInterface(IID_IMediaEvent, (void **)&amp;pEvent);
  
      // Build the graph. IMPORTANT: Change this string to a file on your system.
      hr = pGraph-&gt;Render(m_pCamOutPin);
      if (SUCCEEDED(hr))
      {
          // Run the graph.
          hr = pControl-&gt;Run();
          if (SUCCEEDED(hr))
          {
              // Wait for completion.
              long evCode;
              pEvent-&gt;WaitForCompletion(INFINITE, &amp;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();
  }
  </code></pre>
  
  <p>Any ideas on what am I doing wrong and what do I have to do to get this to work?</p>