title: How to get HWND of an embedded web browser control in MFC
body: <p>I'm using the embedded <a href="https://msdn.microsoft.com/en-us/library/aa752046(v=vs.85).aspx" rel="nofollow noreferrer">web browser control</a> in my dialog-based MFC window and I need to know the HWND of the web browser control in it. I was able to find the following code that claims to retrieve it:</p>
  
  <pre><code>HWND hWndWebBrowser = NULL;
  
  LPUNKNOWN unknown = m_browser.GetControlUnknown();
  
  IWebBrowser2* pWB = NULL;
  if(SUCCEEDED(unknown-&gt;QueryInterface(IID_IWebBrowser2,(void **)&amp;pWB)))
  {
      CComPtr&lt;IServiceProvider&gt; pServiceProvider;
      if (SUCCEEDED(pWB-&gt;QueryInterface(IID_IServiceProvider, (void**)&amp;pServiceProvider)))
      {
          CComPtr&lt;IOleWindow&gt; pWindow;
          if (SUCCEEDED(pServiceProvider-&gt;QueryService(SID_SShellBrowser, IID_IOleWindow, (void**)&amp;pWindow)))
          {
              SHANDLE_PTR hBrowser = 0;
              if (SUCCEEDED(pWindow-&gt;GetWindow(&amp;hBrowser)))
              {
                  hWndWebBrowser = (HWND)hBrowser;
              }
          }
      }
  }
  
  if(unknown)
  {
      unknown-&gt;Release();
  }
  </code></pre>
  
  <p>but the problem is that when it runs, it returns a handle, but not the one I would expect. The best way to illustrate it is with this Spy++ screenshot:</p>
  
  <p><img src="https://i.stack.imgur.com/e1BVW.png" alt="enter image description here"></p>
  
  <p>I understand that I can use <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms633494%28v=vs.85%29.aspx" rel="nofollow noreferrer">EnumChildWindows</a> and look for a window with the <code>Internet Explorer_Server</code> class, but I'm somewhat concerned about using this undocumented class name.</p>
  
  <p>Does anyone have a better way to retrieve that (web browser) window handle?</p>