views:

63

answers:

2

In our COM project, we need to choose between best string class implementation so that BSTR (used for COM interfaces) and elegant string class like CString provides many string manipulation APIs.

Are there any better way to handle the strings and string operations so that it can be BSTR complaints as well as we can have naive CString operations?

A: 

CString has AllocSysString function: http://msdn.microsoft.com/en-us/library/za1865s1%28VS.80%29.aspx

You can use it before calling COM methods.

You can use _bstr_t::Attach to create _bstr_t instance from CString::AllocSysString call, in this case you don't need to care about BSTR release.

Alex Farber
+1  A: 

Unortunately nothing realy elegant here. The best you can do is to use CString::AllocSysString() and you better use a BSTR wrapper like CComBSTR or _bstr_t to manage the resulting BSTR lifetime. See this question for how it usually done.

sharptooth