IWebBrowser2::getHWND() succeeds in de{*word*81} but fails when running stand-alone

I am developing an EditDesigner DLL for IE5.5. At one stage I try to
acquire the window handle of the webbrowser that is hosting my dll.
When I run my code from the de{*word*81} everything works as advertised, but
when I run it in the wild, the get_HWND() method returns E_FAIL.

Here is the relevant code:

// In header file
IHTMLDocument2Ptr m_pDoc;
IHTMLWindow2Ptr m_pWindow;
IWebBrowser2* m_pWebBrowser;

// In cpp file
STDMETHODIMP TEditHelperImpl::Connect(IHTMLWindow2Ptr pWindow)
    m_pWindow = pWindow;
    m_pDoc = pWindow->document;

    // Acquire IServiceProvider from IHTMLDocument2
    CComPtr<IServiceProvider> pServiceProvider;
    m_pDoc->QueryInterface(IID_IServiceProvider, (void**)

    // Get IWebBrowser2 from IServiceProvider
                       (void**) &m_pWebBrowser);

    if (!m_pWebBrowser)
      ShowMessage("IWebBrowser2 == NULL");

    // Get handle of browser window
    // this succeeds when running from IDE/de{*word*81},
    // but returns E_FAIL when running stand-alone

    long hwndBrowser = 0;
    HRESULT hr = m_pWebBrowser->get_HWND(&hwndBrowser);

    if (!SUCCEEDED(hr))
      char msg[32];
      wsprintf(msg, "%X", hr);



I am using CBuilder5 with Windows 2000.
Is there anyone who can explain what essential difference between the
two ways of running my code might cause this behaviour?

Thanks in advance,
Willem Jongman
Akris BV.