请教IHTMLLinkElement接口传递的问题
在下学习CExplorer期间,想试着获取一个网页上的所有链接,并获得链接的相应属性,比如地址,名称之类,于是写了这样一个代码:
- C/C++ code
CComQIPtr<IHTMLDocument2> spDoc = m_Web.get_Document(); if (NULL == spDoc) { return; } HRESULT hRes; long nFormCount = 0; CComBSTR bstrTitle; CString BSLink; CComQIPtr<IHTMLElementCollection> spElementCollection; //取得文档标题 spDoc->get_title(&bstrTitle); //取得超链接集合 hRes = spDoc->get_links(&spElementCollection); if (FAILED(hRes)) { return; } //取得超链接数量 spElementCollection->get_length(&nFormCount); if (FAILED(hRes)) { return; } for (long i = 0; i < nFormCount; ++ i) { IDispatch *pDisp = NULL; //取得第 i 项 hRes = spElementCollection->item(CComVariant(i), CComVariant(), &pDisp); if (FAILED(hRes)) { continue; } //为什么这里指针传递不过来? CComQIPtr<IHTMLLinkElement> spLinkElement = pDisp; }结果发现pDisp的值明明是存在的,可CComQIPtr<IHTMLLinkElement> spLinkElement被赋值后却仍然为空,百思不得其解。
在另外的代码中,换一个变量类型就可以正常赋值,比如CComQIPtr<IHTMLFormElement> spFormElement = pDisp。
请问这到底是为什么?有什么方法解决吗?多谢各位!
这是完整的工程文件:
[解决办法]
This example uses the LINK element to apply an external style sheet, called styles.css, to the page.
<LINK REL=stylesheet HREF="styles.css" type="text/css">
http://msdn.microsoft.com/en-us/library/ms535848(v=vs.85).aspx
看看最下面的,所以应该不是得到你其它链接的,或者你应该得到整个网页源码进行解析
[解决办法]
CComQIPtr<IHTMLLinkElement> spLinkElement = pDisp;
pDisp没有实现IHTMLLinkElement接口,因为CComQIPtr相当于是调用pDisp的QueryInterface.
可以解释为:
- C/C++ code
CComPtr<IHTMLLinkElement> spLinkElement;pDisp->QueryInterface(__uuidof(IHTMLLinkElement), (void**)&spLinkElement);