为什么从IHTMLWindow2调用get_Document()的返回值是一个很小的负数??是失败的值呢?
希望大家多多 指教阿,困扰我很久了。
void CAutoRefreshDlg::OnButton1()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
BOOL ret=FALSE;
if (m_spSHWinds == NULL)
{
if (m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) != S_OK)
{
MessageBox("Failed");
CoUninitialize();
EndDialog(1);
}
}
HRESULT hr;
CComPtr<IDispatch> spDispatch;
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
CComPtr<IHTMLElement> pElement;
CComPtr<IHTMLElementCollection> pElementCol;
CComPtr<IHTMLFormElement> pFormElement;
CComPtr<IHTMLInputTextElement> pInputElement;
CComPtr<IHTMLFramesCollection2> pFramesCol;
CComPtr<IHTMLWindow2> pFrameWindow;
SHDocVw::IWebBrowser2Ptr myWebBrowserPtr=0;
m_ctrlIE.DeleteAllItems();
if (m_spSHWinds)
{
int n = m_spSHWinds->GetCount();
for (int i = 0; i < n; i++)
{
_variant_t v = (long)i;
IDispatchPtr spDisp = m_spSHWinds->Item(v);
//create a ie auto pointer
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
if (spBrowser)
{
long Hwnd=0;;
if(SUCCEEDED(spBrowser->get_HWND(&Hwnd)))
{
//get the ie window tilte
char title[128]={0};
::SendMessage((HWND)Hwnd,WM_GETTEXT,(WPARAM)128,(LPARAM)title);
//if(m_title==title)
{
spDispatch=0;
if (SUCCEEDED(spBrowser->get_Document(&spDispatch)))
if(pDoc2 = spDispatch)
{
pFramesCol=0;
long len;
hr=pDoc2->get_frames(&pFramesCol);
if (SUCCEEDED(hr))
{
VARIANT frameOut;
VARIANT frameRequested;
hr=pFramesCol->get_length(&len);
if(SUCCEEDED(hr)&&len!=0)
{
GetDocumentFromFrames(pFramesCol,len);
}else ret=checkFromIterDocument(pDoc2);//only single frame
}
}
}
}
}
}
}
if (m_spSHWinds)
{
m_spSHWinds.Release();
m_spSHWinds = 0;
}
}
BOOL CAutoRefreshDlg::GetDocumentFromFrames(IHTMLFramesCollection2 *pFrame,long len)
{
BOOL ret=FALSE;
CComPtr<IHTMLFramesCollection2> pFramesCol;
CComPtr<IHTMLWindow2> pFrameWindow;
CComPtr<IHTMLDocument2> pDoc;
HRESULT hr;
VARIANT frameOut;
VARIANT frameRequested;
if(len>0.0001)
{
for(long i=0;i<len;i++)
{
V_VT(&frameRequested) = VT_I4;
V_I4(&frameRequested) = i;
pFrameWindow=NULL;
hr = pFrame->item(&frameRequested, &frameOut);
if(SUCCEEDED(hr))
{
hr = frameOut.pdispVal->QueryInterface(IID_IHTMLWindow2, (void**)&pFrameWindow);
if(SUCCEEDED(hr))
{
long num=0;
pFramesCol=0;
hr=pFrameWindow->get_frames(&pFramesCol);
hr=pFramesCol->get_length(&num);
if(SUCCEEDED(hr))
{
if(num>0.0001)
{
GetDocumentFromFrames(pFramesCol,num);
}
else
{
pDoc=0;
hr=pFrameWindow->get_document(&pDoc);
if(SUCCEEDED(hr))
{
//the default case is that there is no frame set in the window
if(ret=checkFromIterDocument(pDoc))
{
return ret;
}
}else AfxMessageBox("Get Doc Error");
}
}
}
}
}
}
return ret;
}
相关推荐
iframe读取document出现拒绝访问
iframe.document出现拒绝访问。 threw an exception of type 'System.UnauthorizedAccessException' dynamic {System.UnauthorizedAccessException}
webBrowser 禁用消息框alert 并且获得内容
webBrowser 禁用消息框 alert 并且获得内容。
winform执行javascrip源代码
string scriptline01 = @"function ShowPageInfo() {"; string scriptline02 = @" var numLinks = document.links.length; "; string scriptline03 = @" var numForms = document.forms.length; "; string scriptline04 = @" var numImages = document.images.length; "; string scriptline05 = @" var numScripts = document.scripts.length; "; string scriptline06 = @" alert(‘网页的统计结果:\r\n链接数:‘ + numLinks + "; string scriptline07 = @" ‘\r\n表单数:‘ + numForms + "; string scriptline08 = @" ‘\r\n图像数:‘ + numImages + "; string scriptline09 = @" ‘\r\n脚本数:‘ + numScripts);}"; string scriptline10 = @"ShowPageInfo();"; string strScript = scriptline01 + scriptline02 + scriptline03 + scriptline04 + scriptline05 + scriptline06 + scriptline07 + scriptline08 + scriptline09 + scriptline10; IHTMLWindow2 win = (IHTMLWindow2)webBrowser.Document.Window.DomWindow; win.execScript(strScript, "Javascript");
VC6中用DOM遍历网页中的元素.pdf
VC6中用DOM遍历网页中的元素.pdf
使用C#处理WebBrowser控件在不同域名中的跨域问题
我们在做web测试时,经常会使用WebBrowser来进行一些自动化的任务而有些网页上面会用IFrame去嵌套别的页面,这些页面可能不是在相同域名下的,这时就会出现跨域问题,无法直接在WebBrowser中获取到IFrame中的元素,接下来介绍如何解决此问题,需要了解的朋友可以参考下


HTML/XML
