title: fails to set default audio device using Windows Media Encoder
body: <p>I am refering the following document section "Windows Media API for Capturing the Screen" to capture screen and the original code works ok. When I am adding additional feature (I just added several lines to make it record from default audio device), it fails at thew following line (I debugged and looks like it fails at COM layer?), any hints? I posted both the original source codes and my modified source codes.</p>
  
  <p>"if(FAILED(hr=pSrcGrp->put_Profile(variant_t(pProfile))))"</p>
  
  <p><a href="http://www.geocities.com/krishnapg/screencap.html" rel="nofollow noreferrer">http://www.geocities.com/krishnapg/screencap.html</a></p>
  
  <p>Original Source Code:</p>
  
  <p><a href="http://www.geocities.com/krishnapg/WMEncScrnCap.zip" rel="nofollow noreferrer">http://www.geocities.com/krishnapg/WMEncScrnCap.zip</a></p>
  
  <p>My modified source code (I only modified function InitEncoder)</p>
  
  <pre><code>HRESULT InitEncoder(LPCTSTR szOutputFileName)
  {
      HRESULT hr = E_FAIL;
      CComVariant varValue;
      IWMEncSourceGroupCollection*    pSrcGrpCollection=NULL;
      IWMEncSourceGroup*  pSrcGrp=NULL;
      IWMEncSource*   pSrc=NULL;
      IWMEncSource*   paSrc=NULL;
      IPropertyBag*   pPropertyBag=NULL;
      IWMEncVideoSource2* pSrcVid=NULL;
      IWMEncAudioSource*  pSrcAud=NULL;
      IWMEncFile* pOutFile=NULL;
      IWMEncProfile*  pProfile=NULL;
  
      if(FAILED(CoCreateInstance(CLSID_WMEncoder,NULL,CLSCTX_INPROC_SERVER,IID_IWMEncoder2,(void**)&amp;g_pEncoder)))
      {
          ErrorMessage("Unable to Create Encoder Object");
          return E_FAIL;
      }
      if(FAILED(g_pEncoder-&gt;get_SourceGroupCollection(&amp;pSrcGrpCollection)))       //Retrieve the Source Group Collection  - One Application can Have many Source Groups - We need to add as many as we want
      {
          ErrorMessage("Unable to Get Source Group Collection");
          return E_FAIL;
      }
  
      do
      {
          if(FAILED(hr=pSrcGrpCollection-&gt;Add(CComBSTR("SourceGroup1"),&amp;pSrcGrp)))//Add a Source Group to the Collection - Each Source can have one video one audio source input
          {
              ErrorMessage("Unable to Add A Source Group to the Collection");
              break;
          }
          if(FAILED(hr=pSrcGrp-&gt;AddSource(WMENC_VIDEO,&amp;pSrc)))                    //Add a Video Source to the Group
          {
              ErrorMessage("Unable to Add A Source to the Source Group");
              break;
          }
          if(FAILED(hr=pSrcGrp-&gt;AddSource(WMENC_AUDIO,&amp;paSrc)))                   //Add an Audio Source to the Group
          {
              ErrorMessage("Unable to Add A Source to the Source Group");
              break;
          }
          if(FAILED(hr=pSrc-&gt;QueryInterface(IID_IWMEncVideoSource2,(void**)&amp;pSrcVid)))
          {
              ErrorMessage("Unable to Query interface for Video Source");
              break;
          }
          if(FAILED(hr=paSrc-&gt;QueryInterface(IID_IWMEncAudioSource,(void**)&amp;pSrcAud)))
          {
              ErrorMessage("Unable to Query interface for Audio Source");
              break;
          }
          if(FAILED(hr=pSrcVid-&gt;SetInput(CComBSTR("ScreenCap://ScreenCapture1"))))//The Video Input Source Device - Should be "ScreenCap" Device
          {
              ErrorMessage("Unable to Set Video Input Source");
              break;
          }
          if(FAILED(hr=pSrcAud-&gt;SetInput(CComBSTR("Device://Default_Audio_Device"))))//The Video Input Source Device - Should be "Default_Audio_Device" Device
          {
              ErrorMessage("Unable to Set Audio Input Source");
              break;
          }
          if(FAILED(hr=pSrcVid-&gt;QueryInterface(IID_IPropertyBag,(void**)&amp;pPropertyBag)))
          {
              ErrorMessage("Unable to Query Interface for Propery bag");
              break;
          }
  
          varValue = CAPTURE_FULLSCREEN;
          if(FAILED(hr=pPropertyBag-&gt;Write(WMSCRNCAP_ENTIRESCREEN,&amp;varValue)))    //Set Full Screen Property true/false
          {
              ErrorMessage("Unable to Set Capture Screen Property");
              break;
          }
          //int nLeft, nRight, nTop, nBottom;                                 //Set Capture Area - when not in full screen mode
          //                                                                  // Initialize the capture area. The size must be even.
          //  varValue = false;
          //  if ( SUCCEEDED( hr ) )
          //  {
          //      hr = pPropertyBag-&gt;Write( WMSCRNCAP_ENTIRESCREEN, &amp;varValue );
          //  }
          //  varValue = nLeft;
          //  if ( SUCCEEDED( hr ) )
          //  {
          //      hr = pPropertyBag-&gt;Write( WMSCRNCAP_WINDOWLEFT, &amp;varValue );
          //  }
          //  varValue = nRight;
          //  if ( SUCCEEDED( hr ) )
          //  {
          //      hr = pPropertyBag-&gt;Write( WMSCRNCAP_WINDOWRIGHT, &amp;varValue );
          //  }
          //  varValue = nTop;
          //  if ( SUCCEEDED( hr ) )
          //  {
          //      hr = pPropertyBag-&gt;Write( WMSCRNCAP_WINDOWTOP, &amp;varValue );
          //  }
          //  varValue = nBottom;
          //  if ( SUCCEEDED( hr ) )
          //  {
          //      hr = pPropertyBag-&gt;Write( WMSCRNCAP_WINDOWBOTTOM, &amp;varValue );
          //  }
          //  varValue = true;
          //  if ( SUCCEEDED( hr ) )
          //  {
          //      hr = pPropertyBag-&gt;Write( WMSCRNCAP_FLASHRECT, &amp;varValue );
          //  }
  
          if(FAILED(hr=SetupScreenCaptureProfile()))                                  //Setup the Custom Profile
          {
              break;
          }
          if(FAILED(hr=g_pProfile-&gt;QueryInterface(IID_IWMEncProfile,(void**)&amp;pProfile)))
          {
              ErrorMessage("Unable to Query Interface For Profile");
              break;
          }
          if(FAILED(hr=pSrcGrp-&gt;put_Profile(variant_t(pProfile))))                    //Select the Custom Profile into the Encoder    
          {
              ErrorMessage("Unable to Set Profile For Source Group");
              break;
          }
          if(FAILED(hr=g_pEncoder-&gt;get_File(&amp;pOutFile)))
          {
              ErrorMessage("Unable to Get Encoder Output File Object");
              break;
          }
          if(FAILED(hr=pOutFile-&gt;put_LocalFileName(CComBSTR(szOutputFileName))))      //Set the Target Output Filename
          {
              ErrorMessage("Unable to Set Output File Name");
              break;
          }
          if(FAILED(hr=g_pEncoder-&gt;PrepareToEncode(VARIANT_TRUE)))                    //Using Prepare optimizes startig latency
          {
              ErrorMessage("Unable to Prepare for Encoding");
              break;
          }
      }while(false);
  
      if(pProfile)
      {
          pProfile-&gt;Release();
          pProfile=NULL;
      }
      if(pOutFile)
      {
          pOutFile-&gt;Release();
          pOutFile = NULL;
      }
      if(pPropertyBag)
      {
          pPropertyBag-&gt;Release();
          pPropertyBag = NULL;
      }
      if(pSrcVid)
      {
          pSrcVid-&gt;Release();
          pSrcVid = NULL;
      }
      if(pSrc)
      {
          pSrc-&gt;Release();
          pSrc = NULL;
      }
      if(pSrcGrp)
      {
          pSrcGrp-&gt;Release();
          pSrcGrp = NULL;
      }
      if(pSrcGrpCollection)
      {
          pSrcGrpCollection-&gt;Release();
          pSrcGrpCollection = NULL;
      }
      return hr;
  }
  </code></pre>