1

I’m currently learning C++/CX, with Windows Universal App, and I want to show a caught exception message in a MessageDialog, however, C++/CX works in a way that I don’t understand, in that I can't convert a char* into a string type, which is what the MessageDialog expects as input.

catch (const std::invalid_argument ex)
{
   MessageDialog^ ErrorBox = ref new MessageDialog(ex.what());
   ErrorBox->ShowAsync();
}

I hope you can help me.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
jAndersen
  • 115
  • 3
  • 13

1 Answers1

2

The MessageDialog accepts a Platform::String.

The Platform::String accepts a char16* s

And you have a char*, so, you have to find a way to convert it to char16*, and this is how you do it:

wchar_t buffer[ MAX_BUFFER ];
mbstowcs( buffer, ex.what(), MAX_BUFFER );
platformString = ref new Platform::String( buffer );

This should work:

catch (const std::invalid_argument ex)
{
   wchar_t buffer[ MAX_BUFFER ];
   mbstowcs( buffer, ex.what(), MAX_BUFFER );
   platformString = ref new Platform::String( buffer );
   MessageDialog^ ErrorBox = ref new MessageDialog(platformString);
   ErrorBox->ShowAsync();
}
Daniel Trugman
  • 8,186
  • 20
  • 41
  • Thanks for the help! I had to change it a bit though, i thought MAX_BUFFER was a part of a library, but i couldnt make it work, so i gave it an integer instead. And instead of mbstowcs, i had to use mbstowcs_s, as the compiler complained that the former was insecure. Again, thanks :) – jAndersen Oct 24 '17 at 09:44