1

I'm currently having a small issue which i thought would be easy, but not, so,(maybe it is easy, but i don't know) i need to convert a String^ into a String^* so basically a string pointer, small snippet of code:

ARPLC_command_byuser = textBox1->Text;

I've already tried various of methods, but all seem to fail(well, they don't, i do).

If you have any idea, please tell me what to do.

6
  • 3
    Let us see the code you have tried Commented Jul 9, 2012 at 17:01
  • String^ is already a reference, why do you need a pointer to it, what are you trying to do? Do you want to convert from String^ to std::string or wchar_t*? Commented Jul 9, 2012 at 17:04
  • @sblom - Didn't know they used the ^ syntax in C++/CX, I might have jumped the gun on the retagging. Commented Jul 9, 2012 at 17:05
  • Declaration: String^ *ARPLC_command_byuser; Commented Jul 9, 2012 at 17:06
  • 1
    A pointer to a .net managed pointer? That doesn't seem you'd really want that. Commented Jul 9, 2012 at 17:09

1 Answer 1

3

That's not possible. A managed object is moved around in memory when the garbage collector compacts the heap. Which will invalidate any pointer. This is the primary reason that C++/CLI uses the ^ hat to indicate object references, they are pointers under the hood that the garbage collector can recognize and update when the object is moved.

Technically it is possible to pin a managed object to ensure that it doesn't get moved. Something you can do with pin_ptr<> or GCHandle::Alloc(). This should be avoided. Convert the managed string to a native pointer by copying it into unmanaged memory using the Marshal class.

Sign up to request clarification or add additional context in comments.

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.