IWebBrowser2 Crashes when migrating to Amazon Japan

I tried to fix a problem related to IWebBrowser2 and the Amazon Japan website ( http://www.amazon.co.jp ). Every time I visit this site, my application using a web browser crashes.

This has not happened with this particular site. In fact, he worked for several months until the last week, when he simply crashed. I could load other sites perfectly besides Amazon Japan.

In the debug window, I noticed that before the application froze, I got a few exceptions from the first chance.

Debug Output 1 (Amazon Japan)

First-chance exception at 0x763c4b32 in IWebBrowser2.exe: Microsoft C++ exception: Js::JavascriptExceptionObject at memory location 0x002fd2ac..
First-chance exception at 0x763c4b32 in IWebBrowser2.exe: Microsoft C++ exception: Js::JavascriptExceptionObject at memory location 0x002fd30c..
First-chance exception at 0x763c4b32 in IWebBrowser2.exe: Microsoft C++ exception: Js::JavascriptExceptionObject at memory location 0x002fc844..

But I do not think this is the cause of the problem, as I get these exceptions when loading Amazon US (www.amazon.com). In fact, this is even more, but my hosting application does not crash.

Debug Output 2 (Amazon US)

First-chance exception at 0x763c4b32 in IWebBrowser2.exe: Microsoft C++ exception: Js::JavascriptExceptionObject at memory location 0x00f6cfec..
First-chance exception at 0x763c4b32 in IWebBrowser2.exe: Microsoft C++ exception: Js::JavascriptExceptionObject at memory location 0x00f6d04c..
First-chance exception at 0x763c4b32 in IWebBrowser2.exe: Microsoft C++ exception: Js::JavascriptExceptionObject at memory location 0x00f6c584..
First-chance exception at 0x763c4b32 in IWebBrowser2.exe: Microsoft C++ exception: Js::JavascriptExceptionObject at memory location 0x00f6dd8c..
First-chance exception at 0x763c4b32 in IWebBrowser2.exe: Microsoft C++ exception: Js::JavascriptExceptionObject at memory location 0x00f6db9c..
First-chance exception at 0x763c4b32 in IWebBrowser2.exe: Microsoft C++ exception: Js::JavascriptExceptionObject at memory location 0x00f6e3cc..
First-chance exception at 0x763c4b32 in IWebBrowser2.exe: Microsoft C++ exception: Js::JavascriptExceptionObject at memory location 0x00f6db9c..
First-chance exception at 0x763c4b32 in IWebBrowser2.exe: 0x80040155: Interface not registered.
First-chance exception at 0x763c4b32 in IWebBrowser2.exe: 0x80040155: Interface not registered.

Yahoo.com and some of the other sites I used for testing also loaded normally, despite these exceptions. Also note that the second debug output above shows two exceptional exceptions with "Interface not registered." I have not received this for yahoo.com and other sites.

My attempts to fix the problem

When I first came across this, I thought it was just a scripting error on the site. However, I did not see any Javascript error boxes from IWebBrowser2, but to be sure, I called the put_silent (...) method of the IWebBrowser2 object.

m_pWebBrowser->put_silent(VARIANT_TRUE);

. " script (Internet Explorer)" " script ()" Internet Explorer. .

, , , IWebBrowser2 IE8 IE Amazon Japan. , ,

MSG msg = { 0 };
while(GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg); // <--- Js::JavascriptExceptionObject exception was caught here.
}

. Stackoverflow , , .

" ", . , . . - . . , , . , .

, .

main.h

#include <string>
#include <windows.h>
#include "WebBrowser.h"

int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);

main.cpp

#include "Main.H"

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR pszCmdLine, int nCmdShow)
{
    OleInitialize(NULL);

    std::wstring classname = L"IWebBrowser2 Crash";

    WNDCLASS wc      = { 0 };
    wc.hInstance     = hInstance;
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = WindowProc;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpszClassName = classname.c_str();
    if(!RegisterClass(&wc))
        return 0;

    SIZE size = { 800, 600 };

    HWND hWindow = ::CreateWindow(classname.c_str(), classname.c_str(), WS_OVERLAPPEDWINDOW, 0,     
                                    0, size.cx, size.cy, NULL, NULL, hInstance, 0);
    if(!hWindow)
        return 0;

    ShowWindow(hWindow, SW_SHOW);
    UpdateWindow(hWindow);

    MSG msg = { 0 };
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static WebBrowser web;

    switch(uMsg)
    {
    case WM_CREATE:
        {
            RECT r = { 0 };
            ::GetClientRect(hWnd, &r);
            if(web.Create(hWnd, r.right, r.bottom, (HINSTANCE)GetModuleHandle(NULL)))
            {
                web.Show();
                // web.Navigate(L"www.yahoo.com");
                web.Navigate(L"www.amazon.co.jp");
            }
        }
        return 0;    

    case WM_CLOSE:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

WEBBROWSER.H

#include <exdisp.h>
#include <MSHTML.H>
#include <initguid.h>

#include <oleidl.h>
#include <ole2.h>
#pragma comment(lib, "ole32.lib")

//
#include <comip.h>
#include <comdef.h>
#include <comutil.h>
#ifdef _DEBUG
    #pragma comment(lib, "comsuppwd.lib")
#else
    #pragma comment(lib, "comsuppw.lib")
#endif

class WebBrowser: public IOleClientSite, public IOleInPlaceSite, public IOleInPlaceFrame, 
                  public IStorage
{
    HWND            m_hWindow;
    IOleObject *    m_ptrOleObj;
    ULONG           m_refcount;
    IWebBrowser2 *  m_pWebBrowser;

    //
    // IUnknown
    //

    STDMETHODIMP QueryInterface(REFIID riid, void ** ppvObject)
    {   
        if(riid == IID_IUnknown)
            *ppvObject = (LPUNKNOWN)(LPVOID)this;
        else if(riid == IID_IOleClientSite)
            *ppvObject = (LPOLECLIENTSITE)this;
        else if(riid == IID_IOleInPlaceSite)
            *ppvObject = (LPOLEINPLACESITE)this;
        else if(riid == IID_IOleInPlaceFrame)
            *ppvObject = (LPOLEINPLACEFRAME)this;
        else if(riid == IID_IStorage)
            *ppvObject = (LPSTORAGE)this;
        else 
        {   
            *ppvObject = NULL;
            return E_NOINTERFACE;
        }   

        // 
        ((LPUNKNOWN)*ppvObject)->AddRef();
        return S_OK;
    }

    STDMETHODIMP_(ULONG) AddRef(void)
    {   
        return ++m_refcount;
    }

    STDMETHODIMP_(ULONG) STDMETHODCALLTYPE Release(void)
    {   
        if(--m_refcount <= 0)
            m_refcount = 0;    

        return m_refcount;
    }    

    //
    // IOleWindow 
    //    

    STDMETHODIMP GetWindow(HWND * phWindow)
    {   
        *phWindow = m_hWindow;
        return S_OK;
    }

    STDMETHODIMP ContextSensitiveHelp(BOOL fEnterMode)
    {   
        return E_NOTIMPL;
    }    

    //
    // IOleInPlaceUIWindow
    //        

    STDMETHODIMP GetBorder(LPRECT lpRectBorder)
    {   return E_NOTIMPL;
    }
    STDMETHODIMP RequestBorderSpace(LPCBORDERWIDTHS pborderwidths)
    {   return E_NOTIMPL;
    }
    STDMETHODIMP SetActiveObject(IOleInPlaceActiveObject *pActiveObject,  LPCOLESTR pszObjName)
    {   return E_NOTIMPL;
    }
    STDMETHODIMP SetBorderSpace(LPCBORDERWIDTHS pborderwidths)
    {   return E_NOTIMPL;
    }    

    //
    // IOleInPlaceFrame
    //

    STDMETHODIMP EnableModeless(BOOL fEnable)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP InsertMenus(HMENU hmenuShared, LPOLEMENUGROUPWIDTHS lpMenuWidths)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP RemoveMenus(HMENU hmenuShared)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP SetMenu(HMENU hmenuShared, HOLEMENU holemenu, HWND hwndActiveObject)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP SetStatusText(LPCOLESTR pszStatusText)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP TranslateAccelerator(LPMSG lpmsg,  WORD wID)
    {   return E_NOTIMPL;
    }    

    // 
    // IOleClientSite
    //     

    STDMETHODIMP SaveObject()
    {   return E_NOTIMPL;
    }

    STDMETHODIMP GetMoniker(DWORD dwAssign, DWORD dwWhichMoniker, IMoniker ** ppmk)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP GetContainer(LPOLECONTAINER FAR* ppContainer)  
    {   
        * ppContainer = NULL;
        return E_NOINTERFACE;
    }

    STDMETHODIMP ShowObject()
    {   return S_OK;
    }

    STDMETHODIMP OnShowWindow(BOOL fShow)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP RequestNewObjectLayout()
    {   return E_NOTIMPL;
    }       

    //
    // IOleInPlaceSite
    //    

    STDMETHODIMP CanInPlaceActivate()
    {   return S_OK;
    }

    STDMETHODIMP DeactivateAndUndo()
    {   return E_NOTIMPL;   
    }

    STDMETHODIMP DiscardUndoState()
    {   return E_NOTIMPL;   
    }

    STDMETHODIMP GetWindowContext(IOleInPlaceFrame **ppFrame, IOleInPlaceUIWindow **ppDoc, 
                  LPRECT  prcPosRect, LPRECT prcClipRect, LPOLEINPLACEFRAMEINFO lpFrameInfo)
    {
        if(m_hWindow)
        {
            RECT rSize = { 0 };
            GetClientRect(m_hWindow, &rSize);        

            //
            *ppFrame                    = (LPOLEINPLACEFRAME)this;
            *ppDoc                      = NULL;
            *prcPosRect                 = rSize;
            *prcClipRect                = rSize;
            lpFrameInfo->fMDIApp        = FALSE;
            lpFrameInfo->hwndFrame      = m_hWindow;
            lpFrameInfo->haccel         = NULL;
            lpFrameInfo->cAccelEntries  = 0;
        }

        return S_OK;
    }

    STDMETHODIMP OnInPlaceActivate()
    {   return S_OK;    
    }

    STDMETHODIMP OnInPlaceDeactivate()
    {   return S_OK;
    }

    STDMETHODIMP OnPosRectChange(LPCRECT prcPosRect)
    {   
        return S_OK;
    }

    STDMETHODIMP OnUIActivate()
    {   return S_OK;
    }

    STDMETHODIMP OnUIDeactivate(BOOL fUndoable)
    {   return S_OK;
    }

    STDMETHODIMP Scroll(SIZE scrollExtent)
    {   return E_NOTIMPL;
    }

    //
    // IStorage
    //    

    STDMETHODIMP CreateStream(const WCHAR * pwcsName, DWORD dwMode, DWORD dwReserved1, 
                              DWORD dwReserved2, LPSTREAM * ppstm)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP OpenStream(const WCHAR * pwcsName, void * pvReserved1, DWORD dwMode, 
                            DWORD dwReserved2, LPSTREAM * ppstm)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP CreateStorage(const WCHAR * pwcsName, DWORD dwMode, DWORD dwReserved1, 
                               DWORD dwReserved2, LPSTORAGE * ppstg)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP OpenStorage(const WCHAR * pwcsName, LPSTORAGE pstgPriority, DWORD dwMode, 
                             SNB snbExclude, DWORD dwReserved, LPSTORAGE * ppstg)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP CopyTo(DWORD ciidExclude, IID const * rgiidExclude, SNB snbExclude, 
                        LPSTORAGE pstgDest)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP MoveElementTo(const WCHAR * pwcsName, LPSTORAGE pstgDest, LPCWSTR pwcsNewName, 
                               DWORD dwFlags)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP Commit(DWORD dwCommitFlags)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP Revert()
    {   return E_NOTIMPL;
    }

    STDMETHODIMP EnumElements(DWORD dwReserved1, VOID * pvReserved2, DWORD dwReserved3,
                              IEnumSTATSTG ** ppenum)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP DestroyElement(const WCHAR * pwcsName)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP RenameElement(const WCHAR * pwcsOldName, const WCHAR * pwcsNewName)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP SetElementTimes(const WCHAR * pwcsName, FILETIME const * pctime, 
                             FILETIME const * patime, FILETIME const * pmtime)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP SetStateBits(DWORD dwStateBits, DWORD dwMask)
    {   return E_NOTIMPL;
    }

    STDMETHODIMP Stat(STATSTG * pstatstg, DWORD dwStatFlag)
    {   return E_NOTIMPL;
    }   

    STDMETHODIMP SetClass(REFCLSID clsid)
    {   return S_OK;
    }

public:

    WebBrowser() : m_hWindow(NULL), m_refcount(0), m_ptrOleObj(NULL), m_pWebBrowser(NULL) {}

    static LRESULT CALLBACK EventHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }

    bool Create(HWND hParent, int width, int height, HINSTANCE hInstance)
    {
        WNDCLASS wc = { 0 };
        wc.hInstance = hInstance;
        wc.style = CS_VREDRAW | CS_HREDRAW;
        wc.lpszClassName = L"WebBrowser";
        wc.lpfnWndProc = EventHandler;
        wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
        if(!::RegisterClass(&wc))
            return false;

        m_hWindow = CreateWindow(L"WebBrowser", NULL, WS_BORDER | WS_CHILD, 0, 0, width, height,
                                 hParent, NULL, hInstance, 0);
        if(!m_hWindow)
            return false;

        RECT rSize = { 0, 0, width, height };

        // Create the ActiveX WebBrowser control. 
        HRESULT hres = OleCreate(CLSID_WebBrowser, IID_IOleObject, OLERENDER_DRAW, NULL, this,
                                 (LPSTORAGE)this, (LPVOID*)&m_ptrOleObj);
        if(FAILED(hres))
            return false;

        // Inform the object that it is being embedded in an OLE container. 
        if(FAILED(OleSetContainedObject(m_ptrOleObj, TRUE)))
            return false;

        // Show our web browser control. 
        if(FAILED(m_ptrOleObj->DoVerb(OLEIVERB_SHOW, NULL, this, -1, m_hWindow, &rSize)))
            return false;

        // Obtain a pointer to an IWebBrowser interface so we can control the browser. 
        if(FAILED(m_ptrOleObj->QueryInterface(IID_IWebBrowser2, (LPVOID*)&m_pWebBrowser)))
            return false;

        // Suppress scripting error box
        m_pWebBrowser->put_Silent(VARIANT_TRUE);

        return true;
    }

    void Navigate(LPCWSTR pszURL)
    {
        _ASSERTE(m_pWebBrowser != NULL);

        _bstr_t url = pszURL;
        variant_t empty;

        m_pWebBrowser->Navigate(url, &empty.GetVARIANT(), &empty.GetVARIANT(), 
                                &empty.GetVARIANT(), &empty.GetVARIANT());
    }

    void Show()
    {
        ShowWindow(m_hWindow, SW_SHOW);
    }
};

: 29 2014 . .

, - , . , .

, , IWebBrowser2, OLE Automation Internet Explorer, .

, - . . Vista, Windows 7 8. Windows Vista Internet Explorer , , , WS_CHILD . IWebBrowser2:: put_Visible (VARIANT_FALSE), -, Vista, IE , CoCreateInstance. , MSDN IWebBrowser2.

" InternetExplorer , . IWebBrowser2:: Visible VARIANT_TRUE ."

, , Vista, , . , , , Windows 7 8.

, . . , , - .

WEBBROWSER.H

#include <shlguid.h>

//
#include <comutil.h>
#ifdef _DEBUG
    #pragma comment(lib, "comsuppwd.lib")
#else
    #pragma comment(lib, "comsuppw.lib")
#endif

class WebBrowser
{
    HWND            m_hBrowser;
    IWebBrowser2 *  m_pWebBrowser;

public:

    WebBrowser() : m_pWebBrowser(NULL) {}

    bool Create(HWND hParent, int width, int height, HINSTANCE hInstance)
    {
        // Use OLE Automation to control Internet Explorer.
        if(FAILED(CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_SERVER, IID_IWebBrowser2,     
                    (LPVOID*)&m_pWebBrowser)))
            return false;

        IServiceProvider* pServiceProvider = NULL;
        if (SUCCEEDED(m_pWebBrowser->QueryInterface(IID_IServiceProvider, 
            (void**)&pServiceProvider)))
        {
            IOleWindow* pWindow = NULL;
            if (SUCCEEDED(pServiceProvider->QueryService(SID_SShellBrowser, IID_IOleWindow,
                (void**)&pWindow)))
            {
                if (SUCCEEDED(pWindow->GetWindow(&m_hBrowser)))
                {
                    SetWindowLong(m_hBrowser, GWL_STYLE, WS_CHILD);
                    SetWindowLong(m_hBrowser, GWL_HWNDPARENT, (LONG)hParent);
                }
                else
                {
                    MessageBox(NULL, L"Unable to obtain window handle of the web browser.",     
                                L"IWebBrowser2", MB_OK);
                }

                pWindow->Release();
            }

            pServiceProvider->Release();
        } 

        return true;
    }

    void Navigate(LPCWSTR pszURL)
    {
        _bstr_t url = pszURL;
        variant_t empty;
        empty.scode = DISP_E_PARAMNOTFOUND;
        empty.vt = VT_ERROR;

        m_pWebBrowser->Navigate(url, &empty.GetVARIANT(), &empty.GetVARIANT(), 
                                &empty.GetVARIANT(), &empty.GetVARIANT());
    }

    void Show()
    {
        ShowWindow(m_hBrowser, SW_SHOW);
    }

    void SetSize(long width, long height)
    {
        SetWindowPos(m_hBrowser, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOREDRAW);
        SetWindowPos(m_hBrowser, 0, 0, 0, width, height, SWP_NOZORDER | SWP_NOMOVE | 
                     SWP_NOREDRAW);
    }
};
+4

All Articles