要将system::String转换为std::string,我使用以下代码:
IntPtr p = Marshal::StringToHGlobalAnsi(PORT);
string newString = static_cast(p.ToPointer());
Marshal::FreeHGlobal(p);然而,我获得代码的地方使用
IntPtr p = Marshal::StringToHGlobalAnsi(PORT);
char* newString = static_cast(p.ToPointer());
Marshal::FreeHGlobal(p);但是,由于某些原因,如果我执行char,我会在newString中得到垃圾信息
*
版本。有人知道为什么会这样吗?
谢谢。
发布于 2012-01-28 10:30:22
原因是
版本之所以有效,是因为它立即创建了
价值。此私有副本不受后者的影响
..。
The The The
version被分配一个指向内存的指针,然后在下一行释放。当出现以下情况时,它是无效的
命令将执行。
发布于 2021-03-01 05:43:19
std::string bla = "ConvertMe";
String^ ResultString= gcnew String(bla.c_str());https://stackoverflow.com/questions/9042434
复制相似问题