从 BSTR tp std::string 转换大字符串
Converting large strings from BSTR tp std::string?
我在BSTR中有字符串,我想使用W2CA(WideCharToMultiByte)转换它:
USES_CONVERSION;
std::string myMBS = W2CA(myBSTR); // myBSTR is BSTR
但是当字符串非常大时 - 它会在此行上抛出异常"StackOverFlowException"。
但是当我使用它时:
std::wstring myWide(myBSTR);
std::string myMBS(myWide.begin(), myWide.end());
我工作正常。任何人都可以帮助这种行为吗?
更新:对于大字符串,我的意思是大约 10MB 的字符串。
从atlconv.h
看W2CA
的实际定义:
#define W2CA(lpw) ((LPCSTR)W2A(lpw))
现在看看W2A
的定义:
#define W2A(lpw) (
((_lpw = lpw) == NULL) ? NULL : (
(_convert = (lstrlenW(_lpw)+1),
(_convert>INT_MAX/2) ? NULL :
ATLW2AHELPER((LPSTR) alloca(_convert*sizeof(WCHAR)), _lpw,
_convert*sizeof(WCHAR), _acp))))
它调用 alloca
,在堆栈上分配内存。 因此,如果字符串很长,自然会有耗尽可用堆栈空间的风险。
相关文章:
- wcstombs_s(),转换字符串的长度
- 使用 CStringW/CStringA 和 CT2W/CT2A 转换字符串有什么区别?
- 无法转换字符串插入函数的参数
- 转换字符串向量:基于先前的值跳过元素
- 从中间器转换字符串不起作用,出了什么问题?
- 失败的证明转换字符串到枚举类
- 如何使用正则表达式和提升转换迭代器标记和转换 c 字符串
- 使用java jni时转换字符串类型
- 如何避免在转换字符串令牌流时重复istringstream构造
- 阿托伊未正确转换字符串
- C++,转换字符串,使连续下划线序列变为单个下划线
- 使用长算术转换字符串
- 转换字符串十进制数时的精度
- 将词法转换字符串提升为双精度
- 使用marshal_as函数转换字符串的性能
- C++不会从数据转换字符串
- 无论如何,在c++ /Qt中转换字符串到结构中的字段
- C++ 隐式类型转换字符串 -> int?
- 转换字符串到浮点数,c++实现
- 用于转换字符串大小写的函数