使用与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))。
- 微软全球技术中心 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))。
解决方案 »
- ################第一次发帖散分加赚分再加跟大家分享点资源################
- 组态软件的设备驱动是怎么实现的?
- 做一个编辑器,在编辑的时候想实现类似Word"视图"菜单中的"页面"功能。大家给点思路!!
- 有段代码看不明白,谁能给解释清楚,谢谢
- SetForegroundWindow(HWND hwnd)不起作用是怎么回事
- 能不能将char buffer[100]里的东西付给CString?
- COM的简单问题。
- 操作注册表的问题(读取和写入DWORD类型)
- 助我
- UDP和TCP的面试题
- 与鼠标相关的SetCapture()函数到底是做什么的?到底怎么用?(100分哦)
- 如何使用CreateWindow()函数?
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中就没
传进去,结果为空,是为什么
我的Add(IUnknowm *p)都测试通过了
但我怀疑VB中IUnknown无法强转换成IStudent,就干脆
将整个链表做成了每个节点都是IStudent * 的,可还是不行
{
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;
} 试试吧,我是直接敲出来的,如果有错误再改改。
这样试试: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;
}
pVal = m_MyList.pGetCur();
hr = (*pVal)->QueryInterface(__uuidof(IStudent *),(void**)pVal);
还是要出错
在校验FAILED(hr)时就出错了
确认它存储的是IStudent **类型吗?我对VB不太熟,不过好象在ASP中不是这样调用的
把
Dim h As New Student
m_List.First h
改为
set h=m_List.First()
试试?
另外,你往m_MyList中添加元素的时候,有没有调用 AddRef()?
set h=m_List.First()
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;
}
..................
..............
*p = NULL;
struct MStudent **t = NULL;
*p = m_MyList.pGetCur();
.................
...............
MList<IStudent *> m_MyListm_MyList.pGetCur()返回的是当前的IStudent **
并修改First方法
..................
..............
*p = NULL;
struct MStudent **t = NULL;
*p = m_MyList.YourFunc(...); //YourFunc返回一个IStudent *
(*p)->AddRef();
.................
...............!