定义了一个ATL简单对校CLine和接口ILine
在ILine添加方法
[id(1), helpstring("方法AddItem")] HRESULT AddItem([in] BSTR bstrItem);
[id(2), helpstring("方法GetItem")] HRESULT GetItem([in] SHORT index, [out,retval] BSTR* bstrItem);
服务器端:
STDMETHODIMP CLine::AddItem(BSTR bstrItem)
{
m_bstrs.push_back(bstrItem);//m_bstrs std::vector<BSTR> m_bstrs
return S_OK;
}STDMETHODIMP CLine::GetItem(SHORT index, BSTR* bstrItem)
{
if (index>m_bstrs.size() ||index<0)
return -1;
CComBSTR temp(m_bstrs[index]);
*bstrItem=temp.Detach();
return S_OK;
}
在客户端测试的时候,老是提示CComBSTR析构的时候错误。想知道错的原因,应该在CComBSTR的使用上,(我觉得)
测试代码如下:
::CoInitialize(NULL);
ILine *pline=NULL;
HRESULT hr;
hr=::CoCreateInstance(CLSID_Line,NULL,CLSCTX_ALL,IID_ILine,(void**)&pline);
if (SUCCEEDED(hr))
{
MessageBox(L"Get Line ojb success",L"caption");
}
CComBSTR bstr("Hello World");
pline->AddItem(bstr.Copy());
CComBSTR bstr1("Welcom to C++");
pline->AddItem(bstr1.Copy());
CComBSTR bstr2;
pline->GetItem(0,&bstr2);
MessageBox(bstr2);
::SysFreeString(bstr2);
pline->Release();
::CoUninitialize();
在ILine添加方法
[id(1), helpstring("方法AddItem")] HRESULT AddItem([in] BSTR bstrItem);
[id(2), helpstring("方法GetItem")] HRESULT GetItem([in] SHORT index, [out,retval] BSTR* bstrItem);
服务器端:
STDMETHODIMP CLine::AddItem(BSTR bstrItem)
{
m_bstrs.push_back(bstrItem);//m_bstrs std::vector<BSTR> m_bstrs
return S_OK;
}STDMETHODIMP CLine::GetItem(SHORT index, BSTR* bstrItem)
{
if (index>m_bstrs.size() ||index<0)
return -1;
CComBSTR temp(m_bstrs[index]);
*bstrItem=temp.Detach();
return S_OK;
}
在客户端测试的时候,老是提示CComBSTR析构的时候错误。想知道错的原因,应该在CComBSTR的使用上,(我觉得)
测试代码如下:
::CoInitialize(NULL);
ILine *pline=NULL;
HRESULT hr;
hr=::CoCreateInstance(CLSID_Line,NULL,CLSCTX_ALL,IID_ILine,(void**)&pline);
if (SUCCEEDED(hr))
{
MessageBox(L"Get Line ojb success",L"caption");
}
CComBSTR bstr("Hello World");
pline->AddItem(bstr.Copy());
CComBSTR bstr1("Welcom to C++");
pline->AddItem(bstr1.Copy());
CComBSTR bstr2;
pline->GetItem(0,&bstr2);
MessageBox(bstr2);
::SysFreeString(bstr2);
pline->Release();
::CoUninitialize();
解决方案 »
- 在vC++2010里,右击菜单后添加事件处理程序那个选项怎么没了?
- 基于对话框的串口编程
- Visual C++重装系统后还能用伐?
- CView继承类获得文档类指针出错
- 高手请进,在客户区实现两个子窗口的连线?急!在线等!
- 为什么加载的位图没显示呢???????????
- 请问y=lgx这个函数的曲线怎么画,急!!!在线等
- 请教:为什么OnReceive(int nErrorCode)不能触发?
- 调查一下,大家都用VC做什么样的软件?up有分
- 在ole db中的CColumnsInfo问题?
- 面向连接的套接字连接失败,不懂!请大哥大姐们指点!在线等!
- 打算用C系语言做一个性能高一点的服务器端,大家给点建议,多谢!
*bstrItem = A2BSTR("ccccc");