[英]Conversion from _bstr_t to std::string
I am simply trying to convert _bstr_t into std::string.. 我只是想将_bstr_t转换为std :: string。
I found a post on this issue ( LINK ), and did exactly as described, but it is raising exception.. 我发现了一个有关此问题的帖子( LINK ),并且完全按照说明进行操作,但是它引发了异常。
This is what I did: 这是我所做的:
_bstr_t BString;
std::string STDString((char *) BString);
And I get this error: 我得到这个错误:
0CxC00000005: Access violation reading location 0x00000000
Specifically, after looking at the call stack, error in iosfwd.h: 具体来说,查看调用堆栈后,iosfwd.h中出现错误:
static size_t __CLRCALL_OR_CDECL length(const _Elem *_First)
{ // find length of null-terminated string
return (*_First == 0 ? 0
: _CSTD strlen(_First));
}
What is the right way to convert _bstr_t to std::string..? 将_bstr_t转换为std :: string ..的正确方法是什么?
Thanks 谢谢
BString
can hold a null
value, wheras std::string
cannot, so you'll have to account for this. BString
可以保留一个null
值,而std::string
不能,因此您必须解决这个问题。
const char* buf = BString;
int bstrlen = BString.length();
std::string STDString( buf?buf:"", bstrlen);
Note that BString probably uses UTF16 internally, and so the char* overload will use the current locale to choose a byte encoding, probably CP-1252, resulting in some characters being unable to be converted properly. 请注意,BString可能在内部使用UTF16,因此char *重载将使用当前语言环境选择字节编码,可能为CP-1252,从而导致某些字符无法正确转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.