C++/CX : Convert std::string to Platform::String^

Is there a simple way (one line of code would be cool) to convert à std::string to a Platform::String^ in C++/CX ?

I found how to do it the other way(String^ to string) but nothing for this one.

Something like :

std::string str = "Hello World";
Platform::String^ p_str = convertFromString(str);

(In the example it's pointless but when you were working with std::string in c++ and you want to send it to some C# code it makes more sense)

There is no direct std::string to std::wstring conversion currently possible.

However, we can do a roundabout conversion from std::string to std::wstring, then from std::wstring to Platform::String as follows:

#include <locale>
#include <codecvt>
#include <string>

Platform::String^ stringToPlatformString(std::string inputString) {
  std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
  std::wstring intermediateForm = converter.from_bytes(inputString);
  Platform::String^ retVal = ref new Platform::String(intermediateForm.c_str());

  return retVal;
}

See this question for more information on std::string to std::wstring conversion.

Is there a simple way (one line of code would be cool) to convert à std::string to a Platform::String^ in C++/CX ? I found how to do it the other way(String^ to  Strings (C++/CX) 01/22/2017; 5 minutes to read +1; In this article. Text in the Windows Runtime is represented in C++/CX by the Platform::String Class.Use the Platform::String Class when you pass strings back and forth to methods in Windows Runtime classes, or when you are interacting with other Windows Runtime components across the application binary interface (ABI) boundary.

The method works for me

Platform::String ^ convertFromString(const std::string & input)
{
    std::wstring w_str = std::wstring(input.begin(), input.end());
    const wchar_t* w_chars = w_str.c_str();

    return (ref new Platform::String(w_chars));
}

WinRT(C++/CX) type conversion between Platform::String^ and std::string. type _conversion_between_PlatformString_and_stdstring.cpp. #include <string>. Returns a pointer to the beginning of the object's data buffer as a C-style array of char16 (wchar_t) elements. Syntax const char16* Data(); Return Value. A pointer to the beginning of a const char16 array of Unicode characters (char16 is a typedef for wchar_t). Remarks. Use this method to convert from Platform::String^ to wchar_t*.

If you in the conversion does not like to be limited to Basic Latin (Unicode block), like in my case the need for æøå, this works:

#include <sstream>

Platform::String^ StdStringToPlatformString(std::string str)
{
   std::wstringstream wss;
   wss << str.c_str();
   return ref new Platform::String(wss.str().c_str());
}

Use the Platform::String Class when you pass strings back and forth to In your C++ module, use standard C++ string types such as wstring It's easy and efficient to convert between wstring or wchar_t* and Platform::String . It seems that C++/CX does not have a StringBuilder class, or equivalent, so I take it we are to be using STL for this? This is my very first C++/CX app. The user enters some text in a TextBox, hits the ENTER button, and the text is appended to a TextBlock 'console'.

So basically the answer is no : it's more complicated. You need to first convert the std::string in std::wstring and then use user1's answer (convert std::wstring to Platform::String^). For the string to wstring conversion you can check this other question (which doesn't work for me but whatever, I'll just have to go for a more in depth conversion).

(I had put some code but was told it was terrible to do it like this, since I just do it for debugging and wil delete it anyway I don't care but I don't want to give anti-advice so I deleted that code)

Platform:: String' s = L "Hello, world"; size t len = s->Length (); // 12 const wohar t “​pwch For example, this C++/CX Code uses std::wstring as an internal convert to a WinRT string on the way out return ref new Platform:: String (temp. data ()); WinRT(C++/CX) type conversion between Platform::String^ and std::string - type _conversion_between_PlatformString_and_stdstring.cpp

Or else leave the XAML project as C++/CX, create a new C++/WinRT component, find (and port) all of your dependencies on C++/CX, or you can turn support back on Next, ensure that project property General > Target Platform Version is set to using namespace Windows::Media::Playback; String^  cannot convert argument 1 from 'std::string' to 'Platform::String ^' MyTextBox->Text is of type String^ which is (I assume) a reference to Platform::String. If anyone can provide some insight on this matter I would be eternally grateful.

moment. I'm having issues with converting string/char or any other type for that matter to String^ which I need for output in my components. cannot convert argument 1 from 'std::string' to 'Platform::String ^' Strings (C++/CX) When it comes to implementing APIs that take or return strings, you typically change any C++/CX code that uses Platform::String^ to use winrt::hstring instead. Here's an example of a C++/CX API that takes a string. void LogWrapLine(Platform::String^ str); For C++/WinRT you could declare that API in MIDL 3.0 like this.

These language extensions are referred to as C++/CX, from C++ Component When you use C++/CX to work with WinRT objects, you can still use all of the standard C++ The Platform::Collections namespace includes container classes for WinRT You must use this string type to pass strings to WinRT components. They are easy to operate. ‘+’ operator for concatenation, ‘=’ for assignment, can be compared using regular operators. string::find () and many other functions can be implemented on std::string and not on C-Strings so this becomes handy. Iterators can be used in std::string and not in C-strings. And many more! Here is the code for it:-

Comments
  • You could always try and use a charArray to convert between them?
  • Sigh, 22 years of Unicode doesn't stop questions like this. You already know how to convert to std::wstring from that post. Then it is a one-liner, ref new String(wide.c_str())
  • I could make craft something else (with char * etc...), but I wanted to know if there was a good/simple way of doing it
  • Worked for me with slight change in return return (ref new Platform::String(w_chars, w_str.length()));