使用与automation兼容的数据类型。将IUnknown *p 改成IDispatch*;同时保证IMyList和IStudent都从IDispatch 继承。
- 微软全球技术中心 VC技术支持 本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款 
(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。 
为了为您创建更好的讨论环境,请参加我们的用户满意度调查 
(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。

解决方案 »

  1.   

    我现在就用IUnknown已经测试通过了,数据已经传进去了,链表也建立了,但就是,
    COM将每个接口对象传出来时没传出来,
    STDMETHODIMP CMyList::First(IStudent **pVal)
    {
    AFX_MANAGE_STATE(AfxGetStaticModuleState()) // TODO: Add your implementation code here
           m_MyList.ToHead(); 
     
      pVal = m_MyList.pGetCur( );
     IStudent *f = NULL;
     
         BSTR s;
     
     if(*pVal != NULL)
        (*pVal)->get_name(&s);
     
         CString cs = s;
     AfxMessageBox(cs);   
    return S_OK;
    }
    VB调用
    Dim m_List As New MyList
           
    Dim stu As New Student
       
    stu.Name = "lijiang"
    m_List.Add stu
    Dim abc As New Student
    abc.Name = "xiaolin"
    m_List.Add abc
    Dim h As New Student
           
    m_List.First h
             
    Label1.Caption = h.Name
    IStudent可以通过m_List.Add(IStudent对象)添加成功
    结果vc中的对话框弹出的Student结果正确,但未将这个IStudent传到VB中就没
    传进去,结果为空,是为什么
      

  2.   

    补充:IStudent就是从IDispath派生的
    我的Add(IUnknowm *p)都测试通过了
    但我怀疑VB中IUnknown无法强转换成IStudent,就干脆
    将整个链表做成了每个节点都是IStudent * 的,可还是不行
      

  3.   

    明显是你组件内部处理错了,把下面这段代码编译试试STDMETHODIMP CMyList::First(IStudent **pVal) 

    HRESULT hr = S_OK;if(pVal == NULL)
        return E_INVALIDARG;
    *pVal = NULL;m_MyList.ToHead(); 
    //注意下面一句
    hr = m_MyList.pGetCur()->QueryInterface(__uuidof(IStudent), pVal);
    if(FAILED(hr))
        return hr;BSTR s = NULL; 
    hr = (*pVal)->get_name(&s); CString cs = s; 
    AfxMessageBox(cs); ::SysFreeString(s);
    return hr; 
    } 试试吧,我是直接敲出来的,如果有错误再改改。
      

  4.   

    VB是按引用传递参数,“IStudent **pVal”的方式恐怕VB无法识别,
    这样试试:STDMETHODIMP CMyList::First(IStudent *pVal) 

    AFX_MANAGE_STATE(AfxGetStaticModuleState()) // TODO: Add your implementation code here 
    m_MyList.ToHead(); pVal = *(m_MyList.pGetCur()); 
    IStudent *f = NULL; CComBSTR s; if(pVal != NULL) 
    {
     pVal->AddRef();
     pVal->get_name(&s); 
    }
    CString cs = s; 
    AfxMessageBox(cs); 
    return S_OK; 

      

  5.   

    我试了添了
    pVal = m_MyList.pGetCur();
    hr = (*pVal)->QueryInterface(__uuidof(IStudent *),(void**)pVal);
    还是要出错
    在校验FAILED(hr)时就出错了
      

  6.   

    按 lwg7603(刑满释放人员) 的办法将IStudent **改为IStudent *也不成功
      

  7.   

    sorry,上面的我说错了!IStudent **是可以的!你的链表是怎么实现的?
    确认它存储的是IStudent **类型吗?我对VB不太熟,不过好象在ASP中不是这样调用的

    Dim h As New Student 
    m_List.First h 
    改为
    set h=m_List.First()
    试试?
      

  8.   

    注意,是 __uuidof(IStudent)
    另外,你往m_MyList中添加元素的时候,有没有调用 AddRef()?
      

  9.   

    Dim h As IStudent 
    set h=m_List.First() 
      

  10.   

    添了AddRef()一样,VB一样取不到数据啊
    STDMETHODIMP CMyList::Add(IStudent *value)
    {
    AFX_MANAGE_STATE(AfxGetStaticModuleState()) // TODO: Add your implementation code here
        if(value != NULL) 
    {
    value->AddRef();
    m_MyList.Add( value);
    return S_OK;
    }
    else return S_FALSE;
    }STDMETHODIMP CMyList::First(IStudent **p)
    {
    AFX_MANAGE_STATE(AfxGetStaticModuleState())    HRESULT hr = S_OK;
           m_MyList.ToHead(); 
          p = NULL;
      struct MStudent **t = NULL;
      p = m_MyList.pGetCur();
      hr = (*p)->QueryInterface(__uuidof(IStudent),(void**)p);   if(FAILED(hr)) 
      {
      AfxMessageBox("qi fail");
                        return hr; 
      }
           AfxMessageBox("query ok");
     IStudent *f = NULL;
     
         BSTR s;
     
     if(*p != NULL)
        (*p)->get_name(&s);
     
         CString cs = s;
     AfxMessageBox(cs);   
     ::SysFreeString(s); 
         return hr;
    }
      

  11.   

    修改First方法
    ..................
    ..............
    *p = NULL; 
    struct MStudent **t = NULL; 
    *p = m_MyList.pGetCur(); 
    .................
    ...............
      

  12.   

    pGetCur()方法返回的是什么值?如果返回的是当前元素的地址值(&IStudent )。那肯定是不对的!只有返回当前元素值才对(IStudent)!
      

  13.   

    链表是如下定义的  MList是我的链表模板
    MList<IStudent *> m_MyListm_MyList.pGetCur()返回的是当前的IStudent **
      

  14.   

    这样肯定是不对的!应该调用返回IStudent *的函数!
    并修改First方法 
    .................. 
    .............. 
    *p = NULL; 
    struct MStudent **t = NULL; 
    *p = m_MyList.YourFunc(...); //YourFunc返回一个IStudent *
    (*p)->AddRef(); 
    ................. 
    ...............!
      

  15.   

    终于搞定了,就是*p = m_MyList.YourFunc(...); //YourFunc返回一个IStudent * 就搞定了