title: What's wrong with this GetDefaultAudioEndpoint program?
body: <p>Here's a very simple program using the function:</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;tchar.h&gt;
  #include &lt;atlstr.h&gt;
  #include &lt;mmdeviceapi.h&gt;
  #include &lt;devicetopology.h&gt;
  #include &lt;functiondiscoverykeys.h&gt;
  
  #include &lt;iostream&gt;
  
  using namespace std;
  
  int main()
  {
      HRESULT hr;
      CComPtr&lt;IMMDeviceEnumerator&gt; pMMDeviceEnumerator;
      pMMDeviceEnumerator-&gt;GetDefaultAudioEndpoint(eCapture, eMultimedia, 0);
      //cout &lt;&lt; hr;
      return 0;
  }
  </code></pre>
  
  <p>When I try to run this, I get the following error: </p>
  
  <pre><code>Debug Assertion Failed!
  
  Program: ...
  File: c:\program files\microsoft visual studio 8\vc\atlmfc\include\atlcomcli.h
  Line: 154
  
  Expression: p!=0
  </code></pre>
  
  <p>What's wrong with this?  I'm just now trying to learn how to use this function.  Thanks!</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>I've changed the program to this:</p>
  
  <pre><code>//#include &lt;windows.h&gt;
  //#include &lt;tchar.h&gt;
  
  #include &lt;atlstr.h&gt;
  #include &lt;mmdeviceapi.h&gt;
  //#include &lt;devicetopology.h&gt;
  //#include &lt;functiondiscoverykeys.h&gt;
  
  #include &lt;iostream&gt;
  
  using namespace std;
  
  // helper class to CoInitialize/CoUninitialize
  class CCoInitialize {
  private:
      HRESULT m_hr;
  public:
      CCoInitialize(PVOID pReserved, HRESULT &amp;hr)
          : m_hr(E_UNEXPECTED) { hr = m_hr = CoInitialize(pReserved); }
      ~CCoInitialize() { if (SUCCEEDED(m_hr)) { CoUninitialize(); } }
  };
  
  int main()
  {
  
  
      CComPtr&lt;IMMDeviceEnumerator&gt; pMMDeviceEnumerator;
  HRESULT hr = pMMDeviceEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator));
  if (FAILED(hr)) {
      cout &lt;&lt; "failed" &lt;&lt; endl;
      return __LINE__;
      }
  
  CCoInitialize ci(NULL, hr);
  pMMDeviceEnumerator-&gt;GetDefaultAudioEndpoint(eCapture, eMultimedia, 0);
  //cout &lt;&lt; hr;
  return 0;
  }
  </code></pre>
  
  <p>When I run it, I get the output of "failed".  What's happening?</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>Alright, now I've changed the code enough to get it running all the way through without any failures.  i.e., </p>
  
  <pre><code>HRESULT hr = S_OK;  
  cout &lt;&lt; hr;
  // initialize COM
  CCoInitialize ci(NULL, hr);
  if (FAILED(hr)) {
      cout &lt;&lt; "failed1" &lt;&lt; endl;
      return __LINE__;
  }
  cout &lt;&lt; hr;
  // get enumerator
  CComPtr&lt;IMMDeviceEnumerator&gt; pMMDeviceEnumerator;
  hr = pMMDeviceEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator));
  if (FAILED(hr)) {
      cout &lt;&lt; "failed2" &lt;&lt; endl;
      return __LINE__;
  }
  cout &lt;&lt; hr;
  // get default render/capture endpoints
  CComPtr&lt;IMMDevice&gt; pRenderEndpoint;
  hr = pMMDeviceEnumerator-&gt;GetDefaultAudioEndpoint(eRender, eConsole, &amp;pRenderEndpoint);
  if (FAILED(hr)) {
      cout &lt;&lt; "failed3" &lt;&lt; endl;
      return __LINE__;
  }
  
  cout &lt;&lt; hr;
  return 0;
  </code></pre>
  
  <p>Some of the trouble I was having earlier with this example (see comments on the answers) was fixed just by removing some of the code.  But as I run this new body of the main() function, I get the output "0000", meaning that cout &lt;&lt; hr always evaluates to "0".  Is this a good thing?  What info can I get about the default device now?  hr. and hr-> don't really bring up any menus, so I'm kind of in the dark.  Thanks!</p>