定义了一个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();