CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 25

Hybrid View

  1. #1
    Join Date
    Mar 2002
    Posts
    290

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: ConvertStringToBSTR lost char

    Please, post an example that can be compiled and tested.
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2002
    Posts
    290

    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.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    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

  5. #5
    Join Date
    Mar 2002
    Posts
    290

    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]

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: ConvertStringToBSTR lost char

    See below in red ...
    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured