MessageBox((char *)bstr);
改成MessageBox(CString(bstr));

解决方案 »

  1.   

    多谢白菜!这个毛病讷昨天解决了..          USES_CONVERSION;
    char *pfileName;
    pfileName = W2T(bstr);
    MessageBox(pfileName);可惜显示的不系曹连接,系"object"
    好象好需要item方法把连接一个一个举出来..
    讷不会呀,白菜能不能给讷指条路饿?讷去加50分再...到系候一起给你..(白菜昨天多了八千多分...感觉一顶很好饿)
      

  2.   

    呵呵……那些分算是一笔意外之财了,可惜不能换可用分了:(
    我把以前的一个程序稍微改了改,可以实现你要的功能
    void CSendMsgDlg::OnDownloadCompleteExplorer() 
    {
    // TODO: Add your control notification handler code here
    IHTMLElementCollection *objCollection=NULL;
    CComQIPtr<IHTMLElement, &IID_IHTMLElement>objElement;
    IHTMLDocument2 *objDocument=NULL;
    long i,lCount;
    CComPtr<IDispatch>pDisp;
    CString strUrl;
    CString strLinks;
    BSTR bstrTemp;
    strUrl=m_ctrlWeb.GetLocationURL();
    if(strUrl.IsEmpty())
    return;
    objDocument=(IHTMLDocument2 *)m_ctrlWeb.GetDocument();
    objDocument->get_links(&objCollection);
    objCollection->get_length(&lCount);
    for(i=0;i<lCount;i++)
    {
    objCollection->item(COleVariant(i),COleVariant((long)0),&pDisp);
    objElement=pDisp;
    if(objElement==NULL)
    {
    AfxDebugBreak();
    }
    objElement->toString(&bstrTemp);
    strLinks+=bstrTemp;
    strLinks+="\r\n";
    pDisp=NULL;
    }
    AfxMessageBox(strLinks);  //临时用AfxMessageBox显示一下,东西好多啊
    }
      

  3.   

     白菜,下面这个怎么不行啊??
        IHTMLDocument2 *pDocument;
        LPDISPATCH disp;
        IHTMLElementCollection  *pConn;
        IHTMLLinkElement *pElem;
    HRESULT hr;
    CString strLinks;
    long p,i;
    disp = GetHtmlDocument();
        hr = disp->QueryInterface(IID_IHTMLDocument2, (LPVOID *) &pDocument);
      disp->Release();
    if(FAILED(hr))
        {
            MessageBox("document error!");
            return;
        } hr = pDocument->get_links(&pConn);
        if(FAILED(hr))
        {
            MessageBox("links error!");
            return;
        }
    //pConn->
    pConn->get_length(&p); disp->QueryInterface(IID_IHTMLLinkElement ,(LPVOID*)&pElem);
    BSTR bstr;
    for( i =0 ; i < p ; i++)
        {    pConn->item(COleVariant(i),COleVariant((long)0),&disp);
       pElem ->get_href(&bstr);
           strLinks+=bstr;
           strLinks+="\r\n";
        }
    disp->Release();    
    AfxMessageBox(strLinks);  //这个怎么不行啊??怎么strlinks是空的?
      

  4.   

    开始的时候我也是用IHTMLLinkElement ,发现用get_links返回的集合中查不到这样的接口,刚才又试了一下,用IHTMLAnchorElement就可以了,不过你用的方法可能不正确,item得到新的disp接口,pElem能跟着改变吗?这个我不清楚。
    还有,接口指针定义用CComQIPtr可能会好些新的程序改成这样:
    IHTMLElementCollection *objCollection=NULL;
    CComQIPtr<IHTMLAnchorElement, &IID_IHTMLAnchorElement >objElement;
    IHTMLDocument2 *objDocument=NULL;
    long i,lCount;
    CComPtr<IDispatch>pDisp;
    CString strUrl;
    CString strLinks;
    BSTR bstrTemp;
    strUrl=m_ctrlWeb.GetLocationURL();
    if(strUrl.IsEmpty())
    return;
    objDocument=(IHTMLDocument2 *)m_ctrlWeb.GetDocument();
    objDocument->get_links(&objCollection);
    objCollection->get_length(&lCount);
    for(i=0;i<lCount;i++)
    {
    objCollection->item(COleVariant(i),COleVariant((long)0),&pDisp);
    objElement=pDisp;
    if(objElement==NULL)
    {
    AfxDebugBreak();
    }
    objElement->get_href(&bstrTemp);
    strLinks+=bstrTemp;
    strLinks+="\r\n";
    pDisp=NULL;
    }
    AfxMessageBox(strLinks);
      

  5.   

    开始的时候我也是用IHTMLLinkElement ,发现用get_links返回的集合中查不到这样的接口,刚才又试了一下,用IHTMLAnchorElement就可以了,不过你用的方法可能不正确,item得到新的disp接口,pElem能跟着改变吗?这个我不清楚。
    还有,接口指针定义用CComQIPtr可能会好些新的程序改成这样:
    IHTMLElementCollection *objCollection=NULL;
    CComQIPtr<IHTMLAnchorElement, &IID_IHTMLAnchorElement >objElement;
    IHTMLDocument2 *objDocument=NULL;
    long i,lCount;
    CComPtr<IDispatch>pDisp;
    CString strUrl;
    CString strLinks;
    BSTR bstrTemp;
    strUrl=m_ctrlWeb.GetLocationURL();
    if(strUrl.IsEmpty())
    return;
    objDocument=(IHTMLDocument2 *)m_ctrlWeb.GetDocument();
    objDocument->get_links(&objCollection);
    objCollection->get_length(&lCount);
    for(i=0;i<lCount;i++)
    {
    objCollection->item(COleVariant(i),COleVariant((long)0),&pDisp);
    objElement=pDisp;
    if(objElement==NULL)
    {
    AfxDebugBreak();
    }
    objElement->get_href(&bstrTemp);
    strLinks+=bstrTemp;
    strLinks+="\r\n";
    pDisp=NULL;
    }
    AfxMessageBox(strLinks);
      

  6.   

    to 111222:你的程序有几下几点问题:
    1.disp->QueryInterface(IID_IHTMLLinkElement ,(LPVOID*)&pElem);应该在
    pConn->item(COleVariant(i),COleVariant((long)0),&disp);下面。2.记得pElem->Release;3.记得检查接口的返回值,基实你的程序到disp->QueryInterface(IID_IHTMLLinkElement ,(LPVOID*)&pElem);已失败了。我也是刚学COM,水平跟你一样,也不是高手,你学的比我快,我现在才学ATL,其它你的水平比我高都了,我应该向你学习,不是拍你的马屁。这里有点资料:网上有本电子书《Program Internet Explorer 5》在pcbook.51soft.com上。我对COM都非常有兴趣,你呢?,有的话你做个朋友吗?等你的来信:[email protected]
      

  7.   

    白菜:    真实莫名其妙啊,我用 IHTMLAnchorElement代替link、、就好使了,莫名其妙。vcmfc:    我把QueryInterface放到了item下面。再根据白菜的提示把IHTMLLinkElement换成IHTMLAnchorElement就可以了。
        但是不能pElem->Release;//这样做的结果就是非法操作。   
    代码如下:    IHTMLDocument2 *pDocument;
        LPDISPATCH disp;
        IHTMLElementCollection  *pConn;
        IHTMLAnchorElement *pElem;
    HRESULT hr;
    CString strLinks;
    long p,i;
    disp = GetHtmlDocument();
        hr = disp->QueryInterface(IID_IHTMLDocument2, (LPVOID *) &pDocument);
      disp->Release();
    if(FAILED(hr))
        {
            MessageBox("document error!");
            return;
        } hr = pDocument->get_links(&pConn);
        if(FAILED(hr))
        {
            MessageBox("links error!");
            return;
        }

    pConn->get_length(&p);// disp->QueryInterface(IID_IHTMLLinkElement ,(LPVOID*)&pElem);
    BSTR bstr;
    for( i =0 ; i < p ; i++)
        {    pConn->item(COleVariant(i),COleVariant((long)0),&disp);
       disp->QueryInterface(IID_IHTMLAnchorElement ,(LPVOID*)&pElem);
       pElem ->get_href(&bstr);
           strLinks+=bstr;
           strLinks+="\r\n";
       disp->Release();
        }
        
             AfxMessageBox(strLinks);  
    // pElem ->Release();//这行不去掉的话就非法,我机器有毛病不能debug,有问题只能猜,这里猜不出来
    SysFreeString(bstr);
             pConn->Release();