0

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.

3
  • I think you mean, "How do you convert std::string to String^?", maybe. Anyway, search for Marshal::PtrToStringAnsi. Commented Aug 30, 2013 at 20:42
  • omg that worked! thanks! if you post that as an answer i will accept it. Commented Aug 30, 2013 at 20:44
  • Post the code you ended up using with relevant resources/links and self-accept :-) Commented Aug 30, 2013 at 20:45

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.