Jake
Jake

Reputation: 16887

C++/CLI help needed in MessageBox::Show

I am building a project in C++/CLI where in I have to show a message box in one of my forms.

The content has to be a combination of std::string and int.

But I am not able to get the right syntax.

I tried the following:

std::string stringPart = "ABC";
int intPart = 10;
MessageBox::Show("Message" + stringPart + intPart);

I also tried:

String^ msg = String::Concat("Message", stringPart);
msg = String::Concat(msg, intPart);
MessageBox::Show(msg);

Can someone please help me with the syntax.

Thanks.

Upvotes: 2

Views: 17899

Answers (1)

Archie
Archie

Reputation: 6854

Your problem is thar std::string is unmanaged and cannot be assigned to managed System::String. Solution is marshalling. See this MSDN page: http://msdn.microsoft.com/en-us/library/bb384865.aspx

So here is the solution (for Visual Studio):

#include <msclr/marshal_cppstd.h>

// ...

std::string stringPart = "ABC";
int intPart = 10;

String^ msg = String::Concat("Message", msclr::interop::marshal_as<System::String^>(stringPart));
msg = String::Concat(msg, intPart);
MessageBox::Show(msg);

Upvotes: 9

Related Questions