如何将String ^转换为char数组

use*_*623 2 string c++-cli type-conversion visual-c++

可能重复:
需要将String ^转换为char*

我一直在寻找这个解决方案,但我找不到具体的内容.我在Visual Studio C++,Windows窗体应用程序中工作.我需要将String^值转换为char数组.我从储值TextBoxString^:

String^ target_str = targetTextBox->Text;

// lets say that input is "Need this string"
Run Code Online (Sandbox Code Playgroud)

我需要转换它String^并获得类似于此的输出:

char target[] = "Need this string";
Run Code Online (Sandbox Code Playgroud)

如果它被定义为char target[]可行,但我想从中获取此值TextBox.

我尝试过编组,但它没有用.有没有解决方法怎么做?

我已经找到了如何转换std::stringchar数组所以如何解决这个问题的另一种方法是转换String^std::string但我也遇到了这个问题.

Mau*_*ves 5

您最好的选择是遵循此问题中提出的示例.

这是一些示例代码:

String^ test = L"I am a .Net string of type System::String";
IntPtr ptrToNativeString = Marshal::StringToHGlobalAnsi(test);
char* nativeString = static_cast<char*>(ptrToNativeString.ToPointer());
Run Code Online (Sandbox Code Playgroud)

原因是因为.Net字符串显然是GC'd对象,它是公共语言运行时的一部分,您需要通过使用InteropServices边界来跨越CLI边界.祝你好运.