在下学习CExplorer期间,想试着获取一个网页上的所有链接,并获得链接的相应属性,比如地址,名称之类,于是写了这样一个代码: 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。
请问这到底是为什么?有什么方法解决吗?多谢各位!
这是完整的工程文件

解决方案 »

  1.   

    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
    看看最下面的,所以应该不是得到你其它链接的,或者你应该得到整个网页源码进行解析
      

  2.   

    CComQIPtr<IHTMLLinkElement> spLinkElement = pDisp;pDisp没有实现IHTMLLinkElement接口,因为CComQIPtr相当于是调用pDisp的QueryInterface.可以解释为:
    CComPtr<IHTMLLinkElement> spLinkElement;
    pDisp->QueryInterface(__uuidof(IHTMLLinkElement), (void**)&spLinkElement);
      

  3.   

    多谢楼上两位的指点,我尝试使用IHTMLElement果然成功取到了需要的数据 CComQIPtr<IHTMLDocument2> spDoc = m_Web.get_Document();
    if (NULL == spDoc)
    {
    return;
    } HRESULT hRes;
    long nFormCount = 0;
    CComBSTR bstrTitle;
    CString BSLink[10];
    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;
    CComQIPtr<IHTMLElement> spElement;
    //取得第 i 项
    hRes = spElementCollection->item(CComVariant(i), CComVariant(), &pDisp);
    if (FAILED(hRes))
    {
    continue;
    } pDisp->Release(); hRes = pDisp->QueryInterface(IID_IHTMLElement, (void**)&spElement);
    if (FAILED(hRes))
    {
    continue;
    } //++ m_Count;
    //if (m_Count < 20)
    //{
    // continue;
    //} spElement->get_innerText((BSTR*)&BSLink[0]);
    spElement->get_innerHTML((BSTR*)&BSLink[1]);
    spElement->get_outerText((BSTR*)&BSLink[2]);
    spElement->get_outerHTML((BSTR*)&BSLink[3]); spElement->click();
    break;
    }
    不过这个IHTMLElement貌似有点复杂……请问我如果想改变一个超级链接的属性,比如将它原本的从新窗口打开改为从原有窗口打开,该怎么做呢?
      

  4.   

    多谢楼上的提示,通过查找HTML网页代码,代码已经搞定了: BSTR BSLink[10];
    VARIANT VA_Attribute = {0};
    CString szTemp = "target";
    BSLink[4] = szTemp.AllocSysString();
    szTemp = _T("_self");
    BSTR BSTemp = szTemp.AllocSysString();
    VA_Attribute.vt = VT_BSTR;
    VA_Attribute.bstrVal = BSTemp;
    //spElement->get_innerText(&BSLink[0]);
    //spElement->get_innerHTML(&BSLink[1]);
    //spElement->get_outerText(&BSLink[2]);
    //spElement->get_outerHTML(&BSLink[3]); spElement->setAttribute(BSLink[4], VA_Attribute);
    spElement->click();