C++ 如何将 _bstr_t 转换为 CString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1685819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:53:59  来源:igfitidea点击:

How to convert _bstr_t to CString

c++commfcvisual-c++bstr

提问by subbu

I have a _bstr_tvariable bstrErrand I am having a CStringvariable csError. How do I set the value which come in bstrErrto csError?

我有一个_bstr_t变量bstrErr,我有一个CString变量csError。如何设置该进来的价值bstrErrcsError

回答by Andrew

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.

我认为当项目是 Unicode 时,这应该有效。

回答by fredr

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.

CString 具有 LPCSTR 和 LPCWSTR 的构造函数和赋值运算符,因此永远不需要调用 WideCharToMultiByte,并且您不能在 unicode 或非 unicode 模式下错误地进行转换。

You can just assign the string this way:

您可以这样分配字符串:

csError = bstrErr.GetBSTR();

csError = bstrErr.GetBSTR();

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

或者使用构造函数 CString csError( bstrErr.GetBSTR() );

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

我正在使用 GetBSTR。这与使用 (LPCWSTR) 转换 bstrErr 相同,但我更喜欢它的易读性。

回答by sharptooth

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.

如果您为 Unicode 编译 - 只需将封装的 BSTR 分配给 CString。如果您为 ANSI 编译,则必须使用 WideCharToMultiByte() 进行转换。

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.

还要注意封装的 BSTR 可以为 null,它对应于一个空字符串。如果您不注意这一点,您的程序将遇到未定义的行为。

回答by user3707738

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();

回答by Dietrich Baumgarten

CStringT,CString, CStringA, and CStringW:

CStringTCStringCStringA,和CStringW

  • CStringTis a complicated class template based on an arbitrary character type and helper class templates for managing the storage and the features.
  • The class CStringis a typedef of the template class that uses the TCHARcharacter type. TCHARis a generic type that resolves to wcharif the macro UNICODEis set, else to char.
  • The class CStringAis a typedef of the template class that uses internally the narrow character type char.
  • The class CStringWis a typedef of the template class that uses internally the wide character type wchar_t.
  • CStringT是一个基于任意字符类型的复杂类模板和用于管理存储和特征的辅助类模板。
  • 该类CString是使用TCHAR字符类型的模板类的 typedef 。TCHAR是一个泛型类型,wchar如果UNICODE设置了宏,则解析为,否则解析为char
  • 该类CStringA是内部使用窄字符类型的模板类的 typedef char
  • 该类CStringW是内部使用宽字符类型的模板类的 typedef wchar_t

I never use CStringin code, instead I always use the explicit classes CStringAor CStringW. The classes CString*have constructors that accept narrow and wide strings. The same is true for _bstr_t. Strings of type BSTRmust be allocated by the function SysAllocString()that expects an OLECHARstring, hence in Win32/64 a wide string. If you want to copy a _bstr_tthat contains Unicode to a CStringAyou must convert it to UTF8. I use the classes CW2Aand CA2Wfor conversion.

我从不CString在代码中使用,而是总是使用显式类CStringACStringW. 这些类CString*具有接受窄字符串和宽字符串的构造函数。对于 也是如此_bstr_t。类型的字符串BSTR必须由SysAllocString()需要OLECHAR字符串的函数分配 ,因此在 Win32/64 中为宽字符串。如果要将_bstr_t包含 Unicode 的 a复制到 a CStringA,则必须将其转换为 UTF8。我使用类CW2ACA2W进行转换。

In the following event function of a Word add-in, I show the use of these types:

在 Word 插件的以下事件函数中,我展示了这些类型的使用:

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;
}