_com_util::ConvertBSTRToString BUG解决方案(转载)
来源:互联网 发布:滴滴架构 知乎 编辑:程序博客网 时间:2024/05/29 10:01
最近做了一个COM组件负责socket通讯,发现一个无法解决的问题,当socket发送较慢(当连一个代理服务器发送时通常会慢一些),COM组件提供的接口函数中将BSTR转换为char*时会造成程序崩溃,具体异常是操作了受保护的内存之类,跟踪代码发现出问题的代码为:_com_util::ConvertBSTRToString,这是微软提供标准转换代码,心灰意冷,以为这个问题绕不过去了。
后来无意间google到下面这篇文章,原来别人也发现了微软该函数的bug,就是函数内内存申请的问题,作者修复完的函数如下:
- //implement our own conversion functions
- //------------------------//
- // Convert char * to BSTR //
- //------------------------//
- inline BSTR ConvertStringToBSTR(const char* pSrc)
- {
- if(!pSrc) return NULL;
- DWORD cwch;
- BSTR wsOut(NULL);
- if(cwch = ::MultiByteToWideChar(CP_ACP, 0, pSrc,
- -1, NULL, 0))//get size minus NULL terminator
- {
- cwch--;
- wsOut = ::SysAllocStringLen(NULL, cwch);
- if(wsOut)
- {
- if(!::MultiByteToWideChar(CP_ACP,
- 0, pSrc, -1, wsOut, cwch))
- {
- if(ERROR_INSUFFICIENT_BUFFER == ::GetLastError())
- return wsOut;
- ::SysFreeString(wsOut);//must clean up
- wsOut = NULL;
- }
- }
- };
- return wsOut;
- };
- //------------------------//
- // Convert BSTR to char * //
- //------------------------//
- inline char* ConvertBSTRToString(BSTR pSrc)
- {
- if(!pSrc) return NULL;
- //convert even embeded NULL
- DWORD cb,cwch = ::SysStringLen(pSrc);
- char *szOut = NULL;
- if(cb = ::WideCharToMultiByte(CP_ACP, 0,
- pSrc, cwch + 1, NULL, 0, 0, 0))
- {
- szOut = new char[cb];
- if(szOut)
- {
- szOut[cb - 1] = '\0';
- if(!::WideCharToMultiByte(CP_ACP, 0,
- pSrc, cwch + 1, szOut, cb, 0, 0))
- {
- delete []szOut;//clean up if failed;
- szOut = NULL;
- }
- }
- }
- return szOut;
- };
原文链接:
http://www.codeproject.com/Articles/1969/BUG-in-_com_util-ConvertStringToBSTR-and-_com_util
0 0
- _com_util::ConvertBSTRToString BUG解决方案(转载)
- _com_util::ConvertBSTRToString BUG解决方案(转载)
- _com_util::ConvertBSTRToString
- _com_util::ConvertBSTRToString 连接错误
- _com_util::ConvertBSTRToString如何使用
- _com_util::ConvertStringToBSTR和_com_util::ConvertBSTRToString修正版
- _com_util::ConvertBSTRToString 方法要delete
- error LNK2019: 无法解析的外部符号 "char * __stdcall _com_util::ConvertBSTRToString(转)
- 关于_com_util::ConvertBSTRToString”: 编译错误问题
- _com_util::ConvertBSTRToString 方法导致的内存泄漏
- 防止 _com_util::ConvertBSTRToString 引发的内存泄漏
- 关于_com_util::ConvertBSTRToString”: 编译错误问题
- 关于_com_util::ConvertBSTRToString”: 编译错误问题
- 防止 _com_util::ConvertBSTRToString 引发的内存泄漏
- error LNK2001: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *
- error LNK2001: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToString(wchar_t
- MinGW undefined reference to `_com_util::ConvertBSTRToString(wchar_t*)@4'
- ATL 中调用ConvertBSTRToString报错无法解析的外部符号 "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)
- flex socket.readObject() //flex读取java服务器端发送过来的对象
- Windbg内核调试下调试应用层
- 资源Android目录
- awk以列为关键词统计出现次数
- warning:dereferencing type-punned pointer will break strict-aliasing rules
- _com_util::ConvertBSTRToString BUG解决方案(转载)
- 打印
- NSArray,NSSet等集合类的使用
- Nano-X图形库
- Redis配置文件中文详解
- snprintf函数用法
- openGl API
- 遍历Map的四种方法
- Linux系统监控工具之vmstat详解