I have a _bstr_t
variable bstrErr
and I am having a CString
variable csError
. How do I set the value which come in
CStringT
,CString
, CStringA
, and CStringW
:
CStringT
is a complicated class template based on an arbitrary character type and helper class templates for managing the storage and the features.CString
is a typedef of the template class that uses the TCHAR
character type. TCHAR
is a generic type that resolves to wchar
if the macro UNICODE
is set, else to char
.CStringA
is a typedef of the template class that uses internally the narrow character type char
. CStringW
is a typedef of the template class that uses internally the wide character type wchar_t
. I never use CString
in code, instead I always use the explicit classes CStringA
or CStringW
.
The classes CString*
have constructors that accept narrow and wide strings. The same is true for _bstr_t
. Strings of type BSTR
must be allocated by the function SysAllocString()
that expects an OLECHAR
string, hence in Win32/64 a wide string. If you want to copy a _bstr_t
that contains Unicode to a CStringA
you must convert it to UTF8. I use the classes CW2A
and CA2W
for conversion.
In the following event function of a Word add-in, I show the use of these types:
STDMETHODIMP CConnect::TestButtonClicked(IDispatch* Command)
{
BSTR smi = SysAllocString(L"Two smileys