Microsoft KB Archive/261003

= How to handle script errors as a WebBrowser control host =

Article ID: 261003

Article Last Modified on 4/21/2006

-

APPLIES TO


 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Explorer 5.01
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q261003



SUMMARY
The WebBrowser control notifies its host of an unhandled script error through the IOleCommandTarget interface. The host can then retrieve the information about the error, display (or suppress) an error message to the user, and choose whether to run scripts on the page.



MORE INFORMATION
When one of the script engines encounters an unhandled error, it forwards the error to the WebBrowser control, which then queries its container to see if the container has implemented IOleCommandTarget. If the container has implemented IOleCommandTarget, the WebBrowser control calls the IOleCommandTarget::Exec method with the command group ID of CGID_DocHostCommandHandler (which is defined in the Mshtmhst.h file) and a command ID of OLECMDID_SHOWSCRIPTERROR. If the host does not return S_OK, MSHTML displays the default &quot;An error has occurred on this page&quot; error message.

The following code illustrates how to implement a handler for this command ID that retrieves the error information from the document object model. This code does not illustrate error handling.

NOTE: This method will not be invoked if the user has cleared the Disable Script Debugging check box in the Advanced tab under Internet Options. STDMETHODIMP CMyBrowser::Exec( const GUID* pguidCmdGroup, DWORD nCmdID,     DWORD nCmdexecopt, VARIANTARG* pvaIn, VARIANTARG* pvaOut ) {

HRESULT hr = S_OK;

if (pguidCmdGroup && IsEqualGUID(*pguidCmdGroup, CGID_DocHostCommandHandler)) {        switch (nCmdID) {        case OLECMDID_SHOWSCRIPTERROR: {           IHTMLDocument2*             pDoc = NULL; IHTMLWindow2*              pWindow = NULL; IHTMLEventObj*             pEventObj = NULL; BSTR                       rgwszNames[5] = {                                           SysAllocString(L&quot;errorLine&quot;), SysAllocString(L&quot;errorCharacter&quot;), SysAllocString(L&quot;errorCode&quot;), SysAllocString(L&quot;errorMessage&quot;), SysAllocString(L&quot;errorUrl&quot;) };           DISPID                      rgDispIDs[5]; VARIANT                    rgvaEventInfo[5]; DISPPARAMS                 params; BOOL                       fContinueRunningScripts = true; int                        i;

params.cArgs = 0; params.cNamedArgs = 0;

// Get the document that is currently being viewed. hr = pvaIn->punkVal->QueryInterface(IID_IHTMLDocument2, (void **) &pDoc); // Get document.parentWindow. hr = pDoc->get_parentWindow(&pWindow); pDoc->Release; // Get the window.event object. hr = pWindow->get_event(&pEventObj); // Get the error info from the window.event object. for (i = 0; i < 5; i++) {                // Get the property's dispID. hr = pEventObj->GetIDsOfNames(IID_NULL, &rgwszNames[i], 1,                       LOCALE_SYSTEM_DEFAULT, &rgDispIDs[i]); // Get the value of the property. hr = pEventObj->Invoke(rgDispIDs[i], IID_NULL, LOCALE_SYSTEM_DEFAULT,                      DISPATCH_PROPERTYGET, &params, &rgvaEventInfo[i], NULL, NULL); SysFreeString(rgwszNames[i]); }

// At this point, you would normally alert the user with // the information about the error, which is now contained // in rgvaEventInfo[]. Or, you could just exit silently.

(*pvaOut).vt = VT_BOOL; if (fContinueRunningScripts) {              // Continue running scripts on the page. (*pvaOut).boolVal = VARIANT_TRUE; }           else {              // Stop running scripts on the page. (*pvaOut).boolVal = VARIANT_FALSE; }            break; }        default: hr = OLECMDERR_E_NOTSUPPORTED; break; }     }      else {        hr = OLECMDERR_E_UNKNOWNGROUP; }     return (hr); }

