-
May 23rd, 2008, 02:29 AM
#1
ConvertStringToBSTR lost char
Dear friends:
I have some codes:
Code:
char szTemp[10000];//store E6 9D 8E E5 85 8B E5 8B A4 00 00
memset(szTemp, 0, sizeof(szTemp));
char szTemp1[10000];
*pVal = _com_util::ConvertStringToBSTR(szTemp);
strcpy(szTemp1, _com_util::ConvertBSTRToString(*pVal));
//szTemp1 store E6 9D 8E E5 85 8B E5 8B 00 00 00
so A4 is lost.
what's the problem?
I have tried several ways to convert and failed.
By the way I need to transfer szTemp to VB and found this problem.So I wrote this code to test the conversion.
thanks a lot
-
May 23rd, 2008, 03:06 AM
#2
Re: ConvertStringToBSTR lost char
Please, post an example that can be compiled and tested.
Victor Nijegorodov
-
May 23rd, 2008, 03:35 AM
#3
Re: ConvertStringToBSTR lost char
Code:
#include <comdef.h>
#include <comutil.h>
#pragma comment(lib, "comsupp.lib")
void main()
{
char szTemp[10000];//store E6 9D 8E E5 85 8B E5 8B A4 00 00
_bstr_t bstr = "李克勤";
memset(szTemp, 0, sizeof(szTemp));
int iOutputLength = WideCharToMultiByte(CP_UTF8,0,bstr,-1,NULL,0,NULL,NULL);
WideCharToMultiByte(CP_UTF8,0,bstr,-1,szTemp,iOutputLength,NULL,NULL);
char szTemp1[10000];
_bstr_t bstr1 = _com_util::ConvertStringToBSTR(szTemp);
strcpy(szTemp1, _com_util::ConvertBSTRToString(bstr1));
//szTemp1 store E6 9D 8E E5 85 8B E5 8B 00 00 00
}
Thank you.
-
May 23rd, 2008, 04:13 AM
#4
Re: ConvertStringToBSTR lost char
Well your last code snippet has nothing to do with the first one.
And you are doing a serious mistake: the second parameter of WideCharToMultiByte
must be LPCWSTR , not a _bstr_t.
It should be:
bstr.operator const wchar_t*( )
Besides, it is still not clear how you "store E6 9D 8E E5 85 8B E5 8B A4 00 00" in the char szTemp[10000] variable after you call
Code:
memset(szTemp, 0, sizeof(szTemp));
Victor Nijegorodov
-
May 23rd, 2008, 06:37 AM
#5
Re: ConvertStringToBSTR lost char
Sorry for putting the comment in wrong place.
[CODE]
#include <comdef.h> // MFC core and standard components
#include <comutil.h>
#pragma comment(lib, "comsupp.lib")
void main()
{
char szTemp[10000];
_bstr_t bstr = "李克勤";
memset(szTemp, 0, sizeof(szTemp));
int iOutputLength = WideCharToMultiByte(CP_UTF8,0,bstr,-1,NULL,0,NULL,NULL);
WideCharToMultiByte(CP_UTF8,0,bstr,-1,szTemp,iOutputLength,NULL,NULL);//store E6 9D 8E E5 85 8B E5 8B A4 00 00,saw this with Memory
char szTemp1[10000];
_bstr_t bstr1 = _com_util::ConvertStringToBSTR(szTemp);
strcpy(szTemp1, _com_util::ConvertBSTRToString(bstr1));
//store E6 9D 8E E5 85 8B E5 8B A4 00 00,saw this with Memory
}
[CODE]
-
May 23rd, 2008, 07:58 AM
#6
Re: ConvertStringToBSTR lost char
See below in red ...
Originally Posted by sm_ch
Sorry for putting the comment in wrong place.
Code:
#include <comdef.h> // MFC core and standard components
#include <comutil.h>
#pragma comment(lib, "comsupp.lib")
void main()
{
char szTemp[10000];
_bstr_t bstr = "李克勤";
//----------- what do these magic symbols mean?
memset(szTemp, 0, sizeof(szTemp));
int iOutputLength = WideCharToMultiByte(CP_UTF8,0,bstr,-1,NULL,0,NULL,NULL);
WideCharToMultiByte(CP_UTF8,0,bstr,-1,szTemp,iOutputLength,NULL,NULL);//store E6 9D 8E E5 85 8B E5 8B A4 00 00,saw this with Memory
char szTemp1[10000];
_bstr_t bstr1 = _com_util::ConvertStringToBSTR(szTemp);
strcpy(szTemp1, _com_util::ConvertBSTRToString(bstr1));
//store E6 9D 8E E5 85 8B E5 8B A4 00 00,saw this with Memory
//-------I don't see any difference! - :confused:
}
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|