Reputation: 3670
I have my string:
myFunc(std::string text) {}
and I want to convert it into a System::Object. Something like...
array<Object^>^ inArgs = gcnew array<Object^>(2);
const char* ms;
ms = text.c_str();
inArgs[1] = *ms;
Except when I try to get the string out it only gives me the first character.
...it doesn't have to be an std::string. a string of any type will do. im doing a cross thread update and trying to pass a string.
Upvotes: 0
Views: 3065
Reputation: 942177
System::String has a constructor that accepts a const char*. You don't sound picky about the proper code page conversion so it will probably be fine:
std::string test("hello world");
auto managed = gcnew String(test.c_str());
Console::WriteLine(managed);
Upvotes: 3
Reputation: 3670
This code is probably far from the right way. I just emulated what I do in C# for cross thread updates. The actual answer came from the comments.
init form, setup delegates, start the second thread:
public ref class Form_1 : public System::Windows::Forms::Form
{
public:
CL* c;
delegate System::Void CrossThreadUpdate(Object^ obj);
CrossThreadUpdate^ crossThreadUpdate;
void update(Object^ obj);
Thread^ t;
void updateTextArea(String^ text);
void initCL();
Form_1(void)
{
InitializeComponent();
crossThreadUpdate = gcnew CrossThreadUpdate(this, &Form_1::update);
t = gcnew Thread(gcnew ThreadStart(this, &Form_1::initCL));
t->Start();
//
//TODO: Add the constructor code here
//
}
thread does its thing and then lets us know its done
void Form_1::initCL()
{
c = new CL();
updateTextArea("CL READY");
}
update the form
void Form_1::update(Object^ obj) {
array<Object^>^ arr = (array<Object^>^)obj;
int^ op = safe_cast<int^>(arr[0]);
switch(*op)
{
case 1:
String^ text = safe_cast<String^>(arr[1]);
wstring stringConvert = L"";
MarshalString(text, stringConvert);
wcout << stringConvert << endl;
this->textBox1->Text += text + "\r\n";
break;
}
}
void Form_1::updateTextArea(String^ text) {
array<Object^>^ args = gcnew array<Object^>(1);
array<Object^>^ inArgs = gcnew array<Object^>(2);
inArgs[0] = 1;
inArgs[1] = text;
args[0] = inArgs;
this->Invoke(crossThreadUpdate, args);
}
Upvotes: 0