CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 1999
    Posts
    3

    Converting Strings

    Hi,

    I'm having a problem concerning the conversion of strings.
    I need to convert a char* to a BSTR. I can't get this working correctly.
    can somebody please help me?

    Kind regards

    Ruben van Straten


  2. #2
    Join Date
    Jun 1999
    Location
    Washington
    Posts
    32

    Re: Converting Strings

    Try Using,

    int iwstrLen = (strlen(szAnsiString) + 1) * 2;
    WCHAR* pwString = new WCHAR[iwstrLen];

    MultiByteToWideChar(CP_ACP, NULL,
    szAnsiString, -1,
    pwString, iwstrLen,
    NULL, NULL);
    BSTR bstrString = SysAllocString(pwString);
    delete []pwString;




    You can find more information and examples by searching MSDN for :
    MutliByteToWideChar
    or
    WideCharToMultiByte


    -Erik


  3. #3
    Join Date
    May 1999
    Location
    Seattle, WA USA
    Posts
    423

    Re: Converting Strings

    there is a macro defined
    T2OLE
    which takes a char* as its parameter and returns a BSTR, something like:
    USES_CONVERSION; // defines some stuff that T2OLE needs
    char* pString = "Some string";
    BSTR bstrString = T2OLE(pString);

    HTH
    --michael


  4. #4
    Join Date
    Apr 1999
    Posts
    396

    Re: Converting Strings

    You're code is wrong there. When you calculate the size of the string you don't have to multiply it by 2. The new operator can figure out the size, so therefore you don't have to account that WCHAR is 2 times as large


  5. #5
    Join Date
    Jun 1999
    Location
    Washington
    Posts
    32

    Re: Converting Strings

    Yes. I realize that now. I should have had a can of Coke before I started replying to messages this morning. Over all it still works. It just has extra memory allocated that is not being used or needed for a short time.


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