请问有关IUnknown *类型的参数传递的问题

kukafei 2002-03-11 04:56:25
我在VC中做的COM 对象IMyList有个方法为Add( [in] IUnknown * p)
为了测试 IMyList ,定义了 IStudent
当在VB中调用时应该怎么调用
Dim temp As New MyList
Dim stu As New Student
stu.Name = "hello world"
temp.Add (stu)

编译不能通过,
改成 Dim temp As New IMyList
Dim stu As New IStudent
stu.Name = "hello world"
temp.Add (stu)
也不行,请问是为什么?
请问是为什么?
...全文
163 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lwg7603 2002-03-13
  • 打赏
  • 举报
回复
sorry,上面的我说错了!IStudent **是可以的!你的链表是怎么实现的?
确认它存储的是IStudent **类型吗?

我对VB不太熟,不过好象在ASP中不是这样调用的

Dim h As New Student
m_List.First h
改为
set h=m_List.First()
试试?
kukafei 2002-03-13
  • 打赏
  • 举报
回复
按 lwg7603(刑满释放人员) 的办法将IStudent **改为IStudent *也不成功
kukafei 2002-03-13
  • 打赏
  • 举报
回复
我试了添了
pVal = m_MyList.pGetCur();
hr = (*pVal)->QueryInterface(__uuidof(IStudent *),(void**)pVal);
还是要出错
在校验FAILED(hr)时就出错了
lwg7603 2002-03-13
  • 打赏
  • 举报
回复
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;
}
kukafei 2002-03-13
  • 打赏
  • 举报
回复
终于搞定了,就是*p = m_MyList.YourFunc(...); //YourFunc返回一个IStudent * 就搞定了
lwg7603 2002-03-13
  • 打赏
  • 举报
回复
这样肯定是不对的!应该调用返回IStudent *的函数!
并修改First方法
..................
..............
*p = NULL;
struct MStudent **t = NULL;
*p = m_MyList.YourFunc(...); //YourFunc返回一个IStudent *
(*p)->AddRef();
.................
...............!
kukafei 2002-03-13
  • 打赏
  • 举报
回复
链表是如下定义的 MList是我的链表模板
MList<IStudent *> m_MyList

m_MyList.pGetCur()返回的是当前的IStudent **
lwg7603 2002-03-13
  • 打赏
  • 举报
回复
pGetCur()方法返回的是什么值?如果返回的是当前元素的地址值(&IStudent )。那肯定是不对的!只有返回当前元素值才对(IStudent)!
lwg7603 2002-03-13
  • 打赏
  • 举报
回复
修改First方法
..................
..............
*p = NULL;
struct MStudent **t = NULL;
*p = m_MyList.pGetCur();
.................
...............
kukafei 2002-03-13
  • 打赏
  • 举报
回复
添了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;
}
lwg7603 2002-03-13
  • 打赏
  • 举报
回复
Dim h As IStudent
set h=m_List.First()
pihy 2002-03-13
  • 打赏
  • 举报
回复
注意,是 __uuidof(IStudent)
另外,你往m_MyList中添加元素的时候,有没有调用 AddRef()?
pihy 2002-03-12
  • 打赏
  • 举报
回复
明显是你组件内部处理错了,把下面这段代码编译试试

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;
}

试试吧,我是直接敲出来的,如果有错误再改改。
kukafei 2002-03-12
  • 打赏
  • 举报
回复
补充:IStudent就是从IDispath派生的
我的Add(IUnknowm *p)都测试通过了
但我怀疑VB中IUnknown无法强转换成IStudent,就干脆
将整个链表做成了每个节点都是IStudent * 的,可还是不行
kukafei 2002-03-12
  • 打赏
  • 举报
回复
我现在就用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中就没
传进去,结果为空,是为什么
acptvc 2002-03-12
  • 打赏
  • 举报
回复
使用与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))。

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧