描述:
如上:
CComBSTR comStr;
BSTR bstr;
char p[]="testtest" ;
comStr.Append(p);
bstr=comStr.Copy();
.....
.................
BSTR是要SysAllocString()才可以用得
我这样它也可以用,是不是 comStr.Copy()是调用了 SysAllocString()?
请大家指教
解决方案1:
已经分配好了
解决方案2:分配了
解决方案3: class CComBSTR
{
public:
BSTR m_str;
CComBSTR()
{
m_str = NULL;
}
/*explicit*/ CComBSTR(int nSize)
{
m_str = ::SysAllocStringLen(NULL, nSize);
}
/*explicit*/ CComBSTR(int nSize, LPCOLESTR sz)
{
m_str = ::SysAllocStringLen(sz, nSize);
}
/*explicit*/ CComBSTR(LPCOLESTR pSrc)
{
m_str = ::SysAllocString(pSrc);
}
/*explicit*/ CComBSTR(const CComBSTR& src)
{
m_str = src.Copy();
}
/*explicit*/ CComBSTR(REFGUID src)
{
LPOLESTR szGuid;
StringFromCLSID(src, &szGuid);
m_str = ::SysAllocString(szGuid);
CoTaskMemFree(szGuid);
}
CComBSTR& operator=(const CComBSTR& src)
{
if (m_str != src.m_str)
{
if (m_str)
::SysFreeString(m_str);
m_str = src.Copy();
}
return *this;
}
CComBSTR& operator=(LPCOLESTR pSrc)
{
::SysFreeString(m_str);
m_str = ::SysAllocString(pSrc);
return *this;
}
~CComBSTR()
{
::SysFreeString(m_str);
}
unsigned int Length() const
{
return (m_str == NULL)? 0 : SysStringLen(m_str);
}
operator BSTR() const
{
return m_str;
}
BSTR* operator&()
{
return &m_str;
}
BSTR Copy() const
{
return ::SysAllocStringLen(m_str, ::SysStringLen(m_str));
}
HRESULT CopyTo(BSTR* pbstr)
{
ATLASSERT(pbstr != NULL);
if (pbstr == NULL)
return E_POINTER;
*pbstr = ::SysAllocStringLen(m_str, ::SysStringLen(m_str));
if (*pbstr == NULL)
return E_OUTOFMEMORY;
return S_OK;
}
void Attach(BSTR src)
{
ATLASSERT(m_str == NULL);
m_str = src;
}
BSTR Detach()
{
BSTR s = m_str;
m_str = NULL;
return s;
}
void Empty()
{
::SysFreeString(m_str);
m_str = NULL;
}
bool operator!() const
{
return (m_str == NULL);
}
HRESULT Append(const CComBSTR& bstrSrc)
{
return Append(bstrSrc.m_str, SysStringLen(bstrSrc.m_str));
}
HRESULT Append(LPCOLESTR lpsz)
{
return Append(lpsz, ocslen(lpsz));
}
// a BSTR is just a LPCOLESTR so we need a special version to signify
// that we are appending a BSTR
HRESULT AppendBSTR(BSTR p)
{
return Append(p, SysStringLen(p));
}
HRESULT Append(LPCOLESTR lpsz, int nLen)
{
int n1 = Length();
BSTR b;
b = ::SysAllocStringLen(NULL, n1+nLen);
if (b == NULL)
return E_OUTOFMEMORY;
memcpy(b, m_str, n1*sizeof(OLECHAR));
memcpy(b+n1, lpsz, nLen*sizeof(OLECHAR));
b[n1+nLen] = NULL;
SysFreeString(m_str);
m_str = b;
return S_OK;
}
HRESULT ToLower()
{
USES_CONVERSION;
if (m_str != NULL)
{
LPTSTR psz = CharLower(OLE2T(m_str));
if (psz == NULL)
return E_OUTOFMEMORY;
BSTR b = T2BSTR(psz);
if (psz == NULL)
return E_OUTOFMEMORY;
SysFreeString(m_str);
m_str = b;
}
return S_OK;
}
HRESULT ToUpper()
{
USES_CONVERSION;
if (m_str != NULL)
{
LPTSTR psz = CharUpper(OLE2T(m_str));
if (psz == NULL)
return E_OUTOFMEMORY;
BSTR b = T2BSTR(psz);
if (psz == NULL)
return E_OUTOFMEMORY;
SysFreeString(m_str);
m_str = b;
}
return S_OK;
}
bool LoadString(HINSTANCE hInst, UINT nID)
{
USES_CONVERSION;
TCHAR sz[512];
UINT nLen = ::LoadString(hInst, nID, sz, 512);
ATLASSERT(nLen < 511);
SysFreeString(m_str);
m_str = (nLen != 0) ? SysAllocString(T2OLE(sz)) : NULL;
return (nLen != 0);
}
bool LoadString(UINT nID)
{
return LoadString(_pModule->m_hInstResource, nID);
}
CComBSTR& operator+=(const CComBSTR& bstrSrc)
{
AppendBSTR(bstrSrc.m_str);
return *this;
}
bool operator<(BSTR bstrSrc) const
{
if (bstrSrc == NULL && m_str == NULL)
return false;
if (bstrSrc != NULL && m_str != NULL)
return wcscmp(m_str, bstrSrc) < 0;
return m_str == NULL;
}
bool operator==(BSTR bstrSrc) const
{
if (bstrSrc == NULL && m_str == NULL)
return true;
if (bstrSrc != NULL && m_str != NULL)
return wcscmp(m_str, bstrSrc) == 0;
return false;
}
bool operator<(LPCSTR pszSrc) const
{
if (pszSrc == NULL && m_str == NULL)
return false;
USES_CONVERSION;
if (pszSrc != NULL && m_str != NULL)
return wcscmp(m_str, A2W(pszSrc)) < 0;
return m_str == NULL;
}
bool operator==(LPCSTR pszSrc) const
{
if (pszSrc == NULL && m_str == NULL)
return true;
USES_CONVERSION;
if (pszSrc != NULL && m_str != NULL)
return wcscmp(m_str, A2W(pszSrc)) == 0;
return false;
}
#ifndef OLE2ANSI
CComBSTR(LPCSTR pSrc)
{
m_str = A2WBSTR(pSrc);
}
CComBSTR(int nSize, LPCSTR sz)
{