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
How to convert _bstr_t to CString
提问by subbu
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
?
我有一个_bstr_t
变量bstrErr
,我有一个CString
变量csError
。如何设置该进来的价值bstrErr
来csError
?
回答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
:
CStringT
,CString
,CStringA
,和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 theTCHAR
character type.TCHAR
is a generic type that resolves towchar
if the macroUNICODE
is set, else tochar
. - The class
CStringA
is a typedef of the template class that uses internally the narrow character typechar
. - The class
CStringW
is a typedef of the template class that uses internally the wide character typewchar_t
.
CStringT
是一个基于任意字符类型的复杂类模板和用于管理存储和特征的辅助类模板。- 该类
CString
是使用TCHAR
字符类型的模板类的 typedef 。TCHAR
是一个泛型类型,wchar
如果UNICODE
设置了宏,则解析为,否则解析为char
。 - 该类
CStringA
是内部使用窄字符类型的模板类的 typedefchar
。 - 该类
CStringW
是内部使用宽字符类型的模板类的 typedefwchar_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.
我从不CString
在代码中使用,而是总是使用显式类CStringA
或CStringW
. 这些类CString*
具有接受窄字符串和宽字符串的构造函数。对于 也是如此_bstr_t
。类型的字符串BSTR
必须由SysAllocString()
需要OLECHAR
字符串的函数分配 ,因此在 Win32/64 中为宽字符串。如果要将_bstr_t
包含 Unicode 的 a复制到 a CStringA
,则必须将其转换为 UTF8。我使用类CW2A
和CA2W
进行转换。
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;
}