jAndersen
jAndersen

Reputation: 125

C++ showing caught exception in MessageDialog

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.

Upvotes: 1

Views: 103

Answers (1)

Daniel Trugman
Daniel Trugman

Reputation: 8501

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();
}

Upvotes: 2

Related Questions