自己实现control site, 处理 OLECMDID_SHOWSCRIPTERROR
HRESULT CMyOleControlSite::XOleCommandTarget::Exec
(const GUID* pguidCmdGroup, DWORD nCmdID,
DWORD nCmdexecopt, VARIANTARG* pvaIn, VARIANTARG* pvaOut )
{
HRESULT hr = OLECMDERR_E_NOTSUPPORTED;
if (nCmdID == OLECMDID_SHOWSCRIPTERROR)
{
// Don't show the error dialog, but
// continue running scripts on the page.
static DWORD s_last_output_time = 0;
static DWORD total_script_error = 0;
total_script_error++;
if(GetTickCount()-s_last_output_time > 1000*60*10)
{
s_last_output_time = GetTickCount();
CString smsg;
smsg.Format(_T("OLECMDID_SHOWSCRIPTERROR script error discard silently for %d times"),total_script_error);
OutputDebugString(smsg);
}
(*pvaOut).vt = VT_BOOL;
(*pvaOut).boolVal = VARIANT_TRUE;
return S_OK;
}
//return 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"errorLine"),
SysAllocString(L"errorCharacter"),
SysAllocString(L"errorCode"),
SysAllocString(L"errorMessage"),
SysAllocString(L"errorUrl")
};
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, ¶ms, &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;//AtlTrace(_T("%s[%d]"),__TFUNCTION__,__LINE__);
break;
}
}
else
{
hr = OLECMDERR_E_UNKNOWNGROUP; //AtlTrace(_T("%s[%d]"),__TFUNCTION__,__LINE__);
}
//AtlTrace(_T("%s return %08x"),__TFUNCTION__, hr);
return (hr);
}
2004-05-05 20:36:10
回复人: Onega
#include <afxocc.h>
#include <afxhtml.h>
class CHtmlControlSite : public COleControlSite
{
public:
CHtmlControlSite(COleControlContainer* pParentWnd);
~CHtmlControlSite();
CHtmlView* GetView() const;
BEGIN_INTERFACE_PART(DocHostUIHandler, IDocHostUIHandler)
STDMETHOD(ShowContextMenu)(DWORD, LPPOINT, LPUNKNOWN, LPDISPATCH);
STDMETHOD(GetHostInfo)(DOCHOSTUIINFO*);
STDMETHOD(ShowUI)(DWORD, LPOLEINPLACEACTIVEOBJECT,
LPOLECOMMANDTARGET, LPOLEINPLACEFRAME, LPOLEINPLACEUIWINDOW);
STDMETHOD(HideUI)(void);
STDMETHOD(UpdateUI)(void);
STDMETHOD(EnableModeless)(BOOL);
STDMETHOD(OnDocWindowActivate)(BOOL);
STDMETHOD(OnFrameWindowActivate)(BOOL);
STDMETHOD(ResizeBorder)(LPCRECT, LPOLEINPLACEUIWINDOW, BOOL);
STDMETHOD(TranslateAccelerator)(LPMSG, const GUID*, DWORD);
STDMETHOD(GetOptionKeyPath)(OLECHAR **, DWORD);
STDMETHOD(GetDropTarget)(LPDROPTARGET, LPDROPTARGET*);
STDMETHOD(GetExternal)(LPDISPATCH*);
STDMETHOD(TranslateUrl)(DWORD, OLECHAR*, OLECHAR **);
STDMETHOD(FilterDataObject)(LPDATAOBJECT , LPDATAOBJECT*);
END_INTERFACE_PART(DocHostUIHandler)
DECLARE_INTERFACE_MAP()
};
class CMyOleControlSite : public CHtmlControlSite
{
public:
CMyOleControlSite(COleControlContainer *pCnt = NULL):CHtmlControlSite(pCnt) {}
BEGIN_INTERFACE_PART(Authenticate, IAuthenticate)
STDMETHODIMP Authenticate(
HWND __RPC_FAR *phwnd,
LPWSTR __RPC_FAR *pszUsername,
LPWSTR __RPC_FAR *pszPassword);
END_INTERFACE_PART(Authenticate)
BEGIN_INTERFACE_PART(ServiceProvider, IServiceProvider)
STDMETHODIMP QueryService(REFGUID guid, REFIID iid, LPVOID * ppv);
END_INTERFACE_PART(ServiceProvider)
//BEGIN_INTERFACE_PART(ServiceProvider, IServiceProvider)
// STDMETHODIMP QueryService(REFGUID guid, REFIID iid, LPVOID * ppv);
//END_INTERFACE_PART(ServiceProvider)
BEGIN_INTERFACE_PART(OleCommandTarget,IOleCommandTarget)
STDMETHOD(QueryStatus)(
const GUID *pguidCmdGroup,
// Pointer to command group
ULONG cCmds, // Number of commands in prgCmds array
OLECMD prgCmds[ ], // Array of commands
OLECMDTEXT *pCmdText // Pointer to name or status of command
);
STDMETHOD(Exec)(const GUID* pguidCmdGroup, DWORD nCmdID,
DWORD nCmdexecopt, VARIANTARG* pvaIn, VARIANTARG* pvaOut);
END_INTERFACE_PART(OleCommandTarget)
DECLARE_INTERFACE_MAP();
};