웹, HTML

IHTMLWindow2->get_document가 E_ACCESSDENIED를 뱉는 경우(아래와 다른 해결방법)

디버그정 2009. 9. 28. 13:22

IHTMLWindow2的get_document方法有时候会返回E_ACCESSDENIED

2009年2月20日 由 阿华 留言 »

IHTMLWindow2的get_document方法有时候会返回E_ACCESSDENIED
当一个page有多个sub frame的时候,如果frame的src在不同的domain上,就会返回这个错误,真是郁闷,在网上找了一些资料,没用太多有用的信息,最后还是MSDN解决问题,呵呵
解决的办法就是通过 IWebBrowser2 的 IOleContainer 接口访问所有的frame,代码如下

HRESULT CBhoOfPopBars::GetSelectionByOleContainer( CString& selText, CRect& selRect )
{
// Get the IDispatch of the main document
CComPtr pDisp;
m_pBrowser->get_Document( &pDisp );
if( pDisp == NULL )
return E_FAIL;

// Get the container
CComQIPtr pContainer = pDisp;
if( pContainer == NULL )
return E_FAIL;

// Get an enumerator for the frames
CComPtr pEnumerator;
HRESULT hr = pContainer->EnumObjects( OLECONTF_EMBEDDINGS, &pEnumerator );
if( FAILED(hr) )
return E_FAIL;

// Enumerate and refresh all the frames
CComPtr pUnk;
ULONG uFetched = 0;
for( UINT i = 0; S_OK == pEnumerator->Next(1, &pUnk, &uFetched); i++ )
{
CComQIPtr pSubBrowser = pUnk;
pUnk.Release();
if( pSubBrowser == NULL )
continue;

// get sub document in frame
CComQIPtr pSubDoc;
pDisp.Release();
pSubBrowser->get_Document( &pDisp );
if( pDisp )
pSubDoc = pDisp;
if( pSubDoc == NULL )
continue;

// get selection info
hr = GetSelectionFromSingleFrame( pSubDoc, selText, selRect );

// 如果成功找到,则返回,否则继续尝试下一个frame
if( SUCCEEDED( hr ) )
return hr;
}