VC中BSTR和CString的使用

BSTR

在头文件中的定义如下:

typedef WCHAR OLECHAR;

typedef OLECHAR *BSTR;

所以BSTR的实际类型其实就是WCHAR*。

在MSDN中的Allocating and Releasing Memory for a BSTR一文中,强调了在使用这个类型时,要在内存上小心对待,以避免发生内存泄露。对于从函数中传出的BSTR,接受的对象要负责对这个BSTR的内存进行管理。在该文中列举了三种情况:

  • 当在一个函数中使用BSTR时,你要负责分配和销毁它,代码如下:

// shows using the Win32 function

// to allocate memory for the string:

BSTR bstrStatus = ::SysAllocString( L"Some text" );

if (bstrStatus == NULL)

   return E_OUTOFMEMORY;

 

pBrowser->put_StatusText( bstrStatus );

// Free the string:

::SysFreeString( bstrStatus );

//...

 

  • 对于从函数中返回的BSTR,要在程序中进行释放

//...

BSTR bstrStatus;

pBrowser->get_StatusText( &bstrStatus );

 

// shows using the Win32 function

// to freee the memory for the string:

::SysFreeString( bstrStatus );

 

如果我们自己定义的函数返回一个BSTR,那么这个BSTR的接收函数应该释放内存。

// Example shows using MFC's

// CString::AllocSysString

 

//...

HRESULT CMyClass::get_StatusText( BSTR * pbstr )

{

   try

   {

      //m_str is a CString in your class

      *pbstr = m_str.AllocSysString( );

      }

   catch (...)

   {

      return E_OUTOFMEMORY;

   }

// The client is now responsible for freeing pbstr.

return( S_OK );

}

//...

 

CComBSTR

这一块儿得内容原来看过inside ATL时有提过,但是好像有点忘,有点似是而非。所以在自己确定一下。

    CComBSTR() throw()

    {

       m_str = NULL;

    }

如上面代码所示,对于一个CComBSTR对象,刚创建时会默认初始化的。除了这个构造函数外,还有几个构造函数

CComBSTR( int nSize)  用来给CComBSTR初始化分配nSize个对象的空间

CComBSTR(int nSize, LPCOLESTR sz) 从sz中拷贝nSize个字符到CComBSTR中,并且在末尾添加一个null,sz可以包含null字符。

CComBSTR(LPCOLESTR pSrc) 用来拷贝一个null结尾的字符串。

CComBSTR(REFGUID guid) 将guid转为BSTR存储在CComBSTR中。

CComBSTR中对赋值运算符进行了重定义,代码如下:

         CComBSTR& operator=(__in const CComBSTR& src)

         {

                 if (m_str != src.m_str)

                 {

                          ::SysFreeString(m_str);

                          m_str = src.Copy();

                          if (!!src && !*this)

            {

                                   AtlThrow(E_OUTOFMEMORY);

            }

                 }

                 return *this;

         }

从定义的这个函数可以看出,在进行赋值时,CComBSTR自己负责释放之前的内存。在使用CComBSTR.Attach时,也会进行相应的操作。

通过CComBSTR的Empty函数可以进行内存的释放。

 

CString

这个类的详细介绍我从网上找了一篇博客,挺好的。

http://blog.csdn.net/laiyiling/article/details/125216

posted on 2011-07-15 21:05  一颗麦粒  阅读(25144)  评论(0编辑  收藏  举报

导航