描述:
BSTR怎么转化为unsigned char*?
用了很多网上的方法都不行。
诚恳求教,
请高手点拨~
解决方案1:
CComBSTR bstrText(_T("aaaaaaaa"));
unsigned char *nChar;
CString str(bstrText);
nChar = (unsigned char *)str.GetBuffer(str.GetLength());
AfxMessageBox((char *)nChar);
CComBSTR的头文件 #include <atlbase.h>
//将宽字符转换成单字节字符
BOOL slTransToMultiByteChar(LPCWSTR wStr, char *dst, int maxLenI)
{
BOOL bOK = FALSE;
memset(dst, 0, maxLenI);
int nLen = WideCharToMultiByte(CP_ACP, 0, wStr, -1, NULL, 0, NULL, NULL);
if( nLen < maxLenI)
{
nLen = WideCharToMultiByte(CP_ACP, 0, wStr, -1, dst, nLen, NULL, NULL);
bOK = (nLen > 0);
}
return bOK;
}
_bstr_t
http://msdn.microsoft.com/en-us/library/zthfhkd6(VS.71).aspx
首先得搞懂这个BSTR里面到底是什么类型的数据。BSTR里可以存放任意类型的数据,不一定是UNICODE字符串,可以是二进制数据。
解决方案5:解决方案6:
unsinged char szConvert string[64];
WideCharToMultiByte(CP_ACP, 0, bstr, SysStringLen(bstr), string, 64, NULL, NULL);
char* lpsz = _com_util::ConvertBSTRToString(bstr);
如果这个不行
就要看你的具体问题是什么,可放出关键代码review一下
如果不是标准的字符串,那你就得先把 bstrTemp向前移四位,可以看看BSTR的结构是怎样的
http://www.vckbase.com/document/viewdoc/?id=1488
BSTR的前四位表示长度的,如果是一个标准的字符串,而BSTR是一个宽字节字符串来的,可以用如下的方法
unsigned char byTemp[100];
BSTR bstrTemp;
memcpy(byTemp,byTemp,(wcslen(bstrTemp)+1)*2);
http://www.docin.com/p-2645487.html
这个+1
http://www.docin.com/p-2645487.html