HOWTO: Handle Script Errors as a WebBrowser Control Host
PSS ID Number: 261003
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 "An error has occurred on this page" error message.
IDocHostShowUI::ShowMessage
Return S_OK to disable WebBrowser Control messages. Any other return value, like S_FALSE or E_NOTIMPL, allows the WebBrowser Control to display with its message box.
One nice thing you can do with this method is customize the message box captions for your application so they don't read "Microsoft Internet Explorer." You can do this by comparing the caption string in lpstrCaption with the string resource Internet Explorer uses, which is stored in Shdoclc.dll. It is identified by the symbol IDS_MESSAGE_BOX_TITLE, whose value is 2213. The following code snippet shows how you might do this.
// resource identifier for window caption "Microsoft Internet Explorer"
#define IDS_MESSAGE_BOX_TITLE 2213
// Load Shdoclc.dll and the IE message box title string
HINSTANCE hinstSHDOCLC = LoadLibrary(TEXT("SHDOCLC.DLL"));
LoadString(hinstSHDOCLC, IDS_MESSAGE_BOX_TITLE, pBuffer, 50);
// Compare the IE message box title string with lpstrCaption
// If they're the same, substitute your own Caption
if (_tcscmp(OLE2T(lpstrCaption), pBuffer) == 0)
lpstrCaption = L"Custom Caption";
// Create your own message box and display it
*plResult = MessageBox(OLE2T(lpstrText), OLE2T(lpstrCaption), dwType);
// Unload Shdoclc.dll and return
FreeLibrary(hinstSHDOCLC);
return S_OK;
}