Interesting behavior of String^ RRS feed

  • Question

  • Consider this snippet:

    Platform::String^ str2tchar(std::string str)

        Platform::String^ res = ""; // 1
        res += (wchar_t) (str.at(0)); //2
        return res;

    It crashes at 2.  The debugger shows, res is a null pointer. Had I had res = "x" at line 1, then at line 2, it is not null and it does not crash.

    Furthermore, at line 2, if I have, res += "xx", (with res = "" at 1), it does not crash. Having that cast (wchar_t) "+=" got fooled on what object is being added.

    Yes, I am using UNICODE mode.

    Baffled that at line 1, res = "" shows the res as nullptr, even though I assign an empty string to res, (not a nullptr).

    Has anyone seen this behavior?

    The debugger takes you to Object.cpp:434

        CPPCLI_FUNC Platform::String^ __stdcall __abi_ObjectToString(Platform::Object^ o, bool useIPrintable)
            __abi_IUnknown* pUnk = reinterpret_cast<__abi_IUnknown*>(o);

            HRESULT hr;

            if (useIPrintable)
                Platform::Details::IPrintable^ printable;
                ::Platform::Guid gdIPrintable(__uuidof(Platform::Details::IPrintable^));
    434:     hr = pUnk->__abi_QueryInterface(gdIPrintable, (void**)&printable);
                if (SUCCEEDED(hr))
                    return printable->ToString();


    Wednesday, June 6, 2012 11:42 PM


All replies

  • Yes this behavior was discussed some at:


     It looks like you might be trying to convert from std::string to Platform::String.  Perhaps the Code below will help, although it will only work for single character ascii.

    std::wstring to_wstring( const std::string & txt )
    	std::wstring wstr;
    	wstr.resize( txt.size() + 1 );	// Allocate space for trailing 0
    	size_t charsConverted;	// Convert mult-byte-str to wide-char-string
    	errno_t err= ::mbstowcs_s( &charsConverted, (wchar_t * )wstr.data(), wstr.size(), txt.data(), txt.size() );
    	wstr.pop_back();	// Discard trailing \0
    	return wstr;
    //	std::string		kansas( "kansas" );
    //	MyTextBlock->Text= to_PlatString( kansas );
    Platform::String^ to_PlatString( std::string stdstr ) 
    	std::wstring	stdwstr= to_wstring( stdstr );
    	return ref new String( stdwstr.c_str() );
    You would think that there was a built in conversion for std::string to Platform::String available in WinRT. And perhaps there is, but I don't know where.
    Thursday, June 7, 2012 2:20 AM
  • Could you please file a Visual Studio Connect bug on this issue for us to investigate? The += operator you are using isn't documented for the Platform::String.


    David Lamb

    • Marked as answer by Shafiq2012 Saturday, June 9, 2012 8:34 PM
    Friday, June 8, 2012 11:20 PM
  • Ah. That was the subtle reason that exposed it. Thanks.


    Saturday, June 9, 2012 8:34 PM
  • This behavior is seen in Build 8400 in my environment, and was not seen in Build 8250.

    Somewhere after 8250, it was introduced.


    Sunday, June 10, 2012 12:19 AM