How to convert _bstr_t to CString
Asked Answered
F

5

15

I have a _bstr_t variable bstrErr and I am having a CString variable csError. How do I set the value which come in bstrErr to csError?

Followup answered 6/11, 2009 at 6:35 Comment(0)
O
20

Is it not possible just to cast it:

_bstr_t b("Steve");
CString cs;
cs = (LPCTSTR) b;

I think this should work when the project is Unicode.

Oystercatcher answered 6/11, 2009 at 6:39 Comment(0)
U
8

CString has contructors and assignment operators for both LPCSTR and LPCWSTR, so there is never a need to call WideCharToMultiByte, and you can't get the casting wrong in unicode or non-unicode mode.

You can just assign the string this way:

csError = bstrErr.GetBSTR();

Or use the constructor CString csError( bstrErr.GetBSTR() );

I'm using GetBSTR. It's the same thing as casting bstrErr with (LPCWSTR), but I prefer it for legibility.

Unattached answered 17/11, 2009 at 4:49 Comment(0)
J
3

If you compile for Unicode - just assign the encapsulated BSTR to the CString. If you compile for ANSI you'll have to use WideCharToMultiByte() for conversion.

Also beware that the encapsulated BSTR can be null which corresponds to an empty string. If you don't take care of this your program will run into undefined behaviour.

Jillianjillie answered 6/11, 2009 at 6:43 Comment(0)
R
0
BSTR myBSTRVal;
CString BSTRasCString("")
char  szValue[MAX_PATH] = "";

// This will map the BSTR to a new character string (szValue)
WideCharToMultiByte(CP_ACP, 0, myBSTRVal, -1, szValue, sizeof(szValue), NULL, 
NULL);
BSTRasCString.Format("%s", szValue);                
BSTRasCString.TrimLeft();
BSTRasCString.TrimRight();
Raddled answered 15/8, 2017 at 15:53 Comment(1)
I could answer should include more than just code. What did you change/do? Why did you do it that way? - From Review โ€“ Carpio
S
0

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.
  • The class CString is a typedef of the template class that uses the TCHAR character type. TCHARis a generic type that resolves to wchar if the macro UNICODE is set, else to char.
  • The class CStringA is a typedef of the template class that uses internally the narrow character type char.
  • The class 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 ๐Ÿ˜Š in a row: ");
  _bstr_t ley = L"๐Ÿ˜Š\U0001F60A";

  /* Either using CStringA, UTF16 -> UTF8 conversion needed */
  CStringA smiley(CW2A(smi, CP_UTF8));
  smiley += CW2A(ley.GetBSTR(), CP_UTF8);
  MessageBoxW(NULL, CA2W(smiley, CP_UTF8), L"Example", MB_OK | MB_TASKMODAL);

  /* Or using CStringW, use ctor and += operator directly
  CStringW smiley = smi;
  smiley += ley.GetBSTR();
  MessageBoxW(NULL, smiley, L"Example", MB_OK | MB_TASKMODAL);
  */

  SysFreeString(smi);

  return S_OK;
}
Scolopendrid answered 9/4, 2019 at 9:16 Comment(0)

© 2022 - 2024 โ€” McMap. All rights reserved.