title: How to write an avi file with DirectShow
body: <p>I'm trying to write an avi video file based on the streaming of my Decklink SDI card, after looking in the internet and here I wrote this code by my problem is that the program  doesn't write the file here 's the code: </p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;DShow.h&gt;
  #include &lt;iostream&gt;
  
  
   int main(void)
  {
  IGraphBuilder* pGraph = NULL;
  ICaptureGraphBuilder2* pBuild = NULL;
  IMediaControl* pControl = NULL;
  
  ICreateDevEnum* pDevEnum = NULL;
  IEnumMoniker* pEnum = NULL;
  IMoniker *pMoniker = NULL;
  IMoniker* goodMoniker = NULL;
  IBaseFilter* pCap;
  HRESULT hr;
  
  //Initialize pGraph
  hr = CoInitialize(NULL);
     hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **) &amp;pGraph);
  if(FAILED(hr))
  {
      printf("ERROR - Could not initialize COM library");
      return 1;
  }
  
  //Initialize pBuild
  
  
   hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void**) &amp;pBuild);
  if(FAILED(hr))
  {
      printf("ERROR - Could not create the Filter Graph Manager.");
      return 2;
  }
  pBuild-&gt;SetFiltergraph(pGraph);
  
  
  
  hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, reinterpret_cast&lt;void**&gt;(&amp;pDevEnum));
  if(SUCCEEDED(hr))
  {
      hr = pDevEnum-&gt;CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &amp;pEnum, 0);
  }
  
  
  HWND hList;
  
  while(pEnum-&gt;Next(1, &amp;pMoniker, NULL) == S_OK)
  {
      IPropertyBag* pPropBag;
      hr = pMoniker-&gt;BindToStorage(0, 0, IID_IPropertyBag, (void**)(&amp;pPropBag));
      if(FAILED(hr))
      {
          pMoniker-&gt;Release();
          continue;
      }
  
      VARIANT varName;
      VariantInit(&amp;varName);
      hr = pPropBag-&gt;Read(L"Description", &amp;varName, 0);
      if(FAILED(hr))
      {
          hr = pPropBag-&gt;Read(L"FriendlyName", &amp;varName, 0);
      }
      if(SUCCEEDED(hr))
      {
          for(int i=0;i&lt;20;i++)
          {
             std::cout&lt;&lt;(char)*(varName.bstrVal + i);
          }
  
          char yn;
          std::cin&gt;&gt;yn;
  
          if(yn=='Y')
          {
              std::cout&lt;&lt;"SUCCESSFUL"&lt;&lt;std::endl;
              goodMoniker = pMoniker;
              VariantClear(&amp;varName);
          }
      }
      pPropBag-&gt;Release();
      if(pMoniker != goodMoniker)
      {
          pMoniker-&gt;Release();
      }
  }
  
  
  hr = goodMoniker-&gt;BindToObject(0, 0, IID_IBaseFilter, (void**)(&amp;pCap));
  
  if(SUCCEEDED(hr))
  {
      hr = pGraph-&gt;AddFilter(pCap, L"Capture Filter");
  
  }
  
  IBaseFilter *pMux = NULL;
  
  //  hr = pBuild-&gt;RenderStream(&amp;PIN_CATEGORY_PREVIEW, &amp;MEDIATYPE_Video, pCap, NULL, NULL);
  
  hr = pBuild-&gt;SetOutputFileName(
  &amp;MEDIASUBTYPE_Avi,  // Specifies AVI for the target file.
  L"C:\\stuff.avi", // File name.
  &amp;pCap,              // Receives a pointer to the mux.
  NULL);              // (Optional) Receives a pointer to the file sink.
  
  hr =pBuild-&gt;RenderStream(
  &amp;PIN_CATEGORY_CAPTURE, // Pin category.
  &amp;MEDIATYPE_Video,      // Media type.
  pCap,                  // Capture filter.
  NULL,                  // Intermediate filter (optional).
  pMux);                 // Mux or file sink filter.  
  
  
  hr = pCap-&gt;QueryInterface(IID_IMediaControl,(void**) pControl);
  
  printf("START ");
  hr = pControl-&gt;Run();
  
  Sleep(100000);
  hr = pControl-&gt;Stop();
  CoUninitialize();
  
  
  pGraph-&gt;Release();
  pBuild-&gt;Release();
  pCap-&gt;Release();
  </code></pre>
  
  <p>}</p>
  
  <p>here is the error message that I get when I try to debbug it :</p>
  
  <pre><code>  Unhandled exception at 0x776015de in STREAMMMMM.exe: 0xC0000005: Access violation reading location 0x00000000.
  </code></pre>
  
  <p>the program crashes in this line :</p>
  
  <pre><code> r = pCap-&gt;QueryInterface(IID_IMediaControl,(void**) pControl);
  </code></pre>