converting stl string to BSTR and the opposite
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: converting stl string to BSTR and the opposite

  1. #1
    Join Date
    Sep 2003
    Posts
    815

    converting stl string to BSTR and the opposite

    Hello,

    I was wondering if anyone knows how to convert stl string to BSTR, and the other way around

    thanks
    avi

  2. #2
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    Yes, I know. Would you like me to tell you?

    Code:
    namespace convert
    {
    std::wstring str_to_wstr( const std::string& str )
    {
      std::wstring wstr( str.length()+1, 0 );
      
      MultiByteToWideChar( CP_ACP,
                 0,
                 str.c_str(),
                 str.length(),
                 &wstr[0],
                 str.length() );
      return wstr;
    }
    std::string wstr_to_str( const std::wstring& wstr )
    {
      size_t size = wstr.length();
      std::string str( size + 1, 0 );
      
      WideCharToMultiByte( CP_ACP,
                 0,
                 wstr.c_str(),
                 size,
                 &str[0],
                 size,
                 NULL,
                 NULL );
      return str;
    }
    }
    Code:
    BSTR b = SysAllocString( L"test" );
    std::string s = convert::wstr_to_str( b );
    SysFreeString( b );
    Code:
    std::string s = "test";
    BSTR b = SysAllocString( convert::str_to_wstr( s ).c_str() );
    SysFreeString( b );

  3. #3
    Join Date
    Sep 2003
    Posts
    815
    well thanks, yes I want you too...

    the problem is that I don't really know how to use it
    I have a function that one of it's out parameter is BSTR *
    and I have stl string with the value

    so actully what I need is conversion between My stl string and the BSTR*

    it's something like that:

    void MyFunc(int iInLen, int iInNumOfRecords, BSTR* pbOutString)
    {

    string sMyOutString;
    MyStringFunc(iInLen,iInNumOfRecords,&sMyOutString);

    //here I need to convert from sMyOutString to pbOutString
    //???

    }

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center