BSTR 到 std::string (std::wstring) ,反之亦然
在 C++ 中使用 COM 时,字符串通常是 BSTR
数据类型。有人可以使用 BSTR
包装器,例如 CComBSTR
或 MS 的 CString
。但是因为我无法在 MinGW 编译器中使用 ATL 或 MFC,所以是否有标准代码片段将 BSTR
转换为 std::string
(或 std::wstring
),反之亦然?
是否还有一些与 CComBSTR
类似的 BSTR
的非 MS 包装器?
更新
感谢所有以任何方式帮助我的人!正因为没有人解决 BSTR
和 std::string
之间的转换问题,我想在这里提供一些有关如何做到这一点的线索。
以下是我分别用于将 BSTR
转换为 std::string
和 std::string
到 BSTR
的函数:
std::string ConvertBSTRToMBS(BSTR bstr)
{
int wslen = ::SysStringLen(bstr);
return ConvertWCSToMBS((wchar_t*)bstr, wslen);
}
std::string ConvertWCSToMBS(const wchar_t* pstr, long wslen)
{
int len = ::WideCharToMultiByte(CP_ACP, 0, pstr, wslen, NULL, 0, NULL, NULL);
std::string dblstr(len, '\0');
len = ::WideCharToMultiByte(CP_ACP, 0 /* no flags */,
pstr, wslen /* not necessary NULL-terminated */,
&dblstr[0], len,
NULL, NULL /* no default char */);
return dblstr;
}
BSTR ConvertMBSToBSTR(const std::string& str)
{
int wslen = ::MultiByteToWideChar(CP_ACP, 0 /* no flags */,
str.data(), str.length(),
NULL, 0);
BSTR wsdata = ::SysAllocStringLen(NULL, wslen);
::MultiByteToWideChar(CP_ACP, 0 /* no flags */,
str.data(), str.length(),
wsdata, wslen);
return wsdata;
}
While working with COM in C++ the strings are usually of BSTR
data type. Someone can use BSTR
wrapper like CComBSTR
or MS's CString
. But because I can't use ATL or MFC in MinGW compiler, is there standard code snippet to convert BSTR
to std::string
(or std::wstring
) and vice versa?
Are there also some non-MS wrappers for BSTR
similar to CComBSTR
?
Update
Thanks to everyone who helped me out in any way! Just because no one has addressed the issue on conversion between BSTR
and std::string
, I would like to provide here some clues on how to do it.
Below are the functions I use to convert BSTR
to std::string
and std::string
to BSTR
respectively:
std::string ConvertBSTRToMBS(BSTR bstr)
{
int wslen = ::SysStringLen(bstr);
return ConvertWCSToMBS((wchar_t*)bstr, wslen);
}
std::string ConvertWCSToMBS(const wchar_t* pstr, long wslen)
{
int len = ::WideCharToMultiByte(CP_ACP, 0, pstr, wslen, NULL, 0, NULL, NULL);
std::string dblstr(len, '\0');
len = ::WideCharToMultiByte(CP_ACP, 0 /* no flags */,
pstr, wslen /* not necessary NULL-terminated */,
&dblstr[0], len,
NULL, NULL /* no default char */);
return dblstr;
}
BSTR ConvertMBSToBSTR(const std::string& str)
{
int wslen = ::MultiByteToWideChar(CP_ACP, 0 /* no flags */,
str.data(), str.length(),
NULL, 0);
BSTR wsdata = ::SysAllocStringLen(NULL, wslen);
::MultiByteToWideChar(CP_ACP, 0 /* no flags */,
str.data(), str.length(),
wsdata, wslen);
return wsdata;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
BSTR
到std::wstring
:std::wstring
到BSTR
:文档参考:
std::basic_string::basic_string(const CharT*, size_type)
std::basic_string>>::empty() const
< /a>std::basic_string>>::data() const
std::basic_string>>::size() const
SysStringLen()
SysAllocStringLen()
BSTR
tostd::wstring
:std::wstring
toBSTR
:Doc refs:
std::basic_string<typename CharT>::basic_string(const CharT*, size_type)
std::basic_string<>::empty() const
std::basic_string<>::data() const
std::basic_string<>::size() const
SysStringLen()
SysAllocStringLen()
有一个名为
_bstr_t
的 C++ 类。它具有有用的方法和一组重载运算符。例如,您可以轻松地从
const wchar_t *
或const char *
进行分配,只需执行_bstr_t bstr = L"My string";
然后您可以通过执行const wchar_t * s = bstr.operator const wchar_t *();
将其转换回来。您甚至可以将其转换回常规 charconst char * c = bstr.operator char *();
然后您可以只使用const wchar_t *
或>const char *
初始化一个新的std::wstring
oestd::string
。There is a c++ class called
_bstr_t
. It has useful methods and a collection of overloaded operators.For example, you can easily assign from a
const wchar_t *
or aconst char *
just doing_bstr_t bstr = L"My string";
Then you can convert it back doingconst wchar_t * s = bstr.operator const wchar_t *();
. You can even convert it back to a regular charconst char * c = bstr.operator char *();
You can then just use theconst wchar_t *
or theconst char *
to initialize a newstd::wstring
oestd::string
.如果您愿意,您也可以执行此操作
或 std::string
编辑:如果您的原始字符串包含多个嵌入的 \0,则此方法将不起作用。
You could also do this
or std::string if you prefer
EDIT: if your original string contains multiple embedded \0 this approach will not work.
只需将 BSTR 直接传递给 wstring 构造函数,它与 wchar_t* 兼容:
将 BSTR 转换为 std::string 需要首先转换为 char*。这是有损的,因为 BSTR 存储 utf-16 编码的 Unicode 字符串。除非你想用utf-8编码。您将在 ICU 库中找到执行此操作以及操作结果字符串的辅助方法。
Simply pass the BSTR directly to the wstring constructor, it is compatible with a wchar_t*:
Converting BSTR to std::string requires a conversion to char* first. That's lossy since BSTR stores a utf-16 encoded Unicode string. Unless you want to encode in utf-8. You'll find helper methods to do this, as well as manipulate the resulting string, in the ICU library.