0

我在将托管 System::String 转换为 C++/CLI 中的 std::string 时遇到问题。这段代码不起作用,我不明白为什么:

string SolvingUnitWrapper::getName(String ^name)
{
    pin_ptr<const wchar_t> wstr = PtrToStringChars(name);
    ostringstream oss;
    oss << wstr; 
    return oss.str();
}

谢谢

4

1 回答 1

4

尝试这个:

std::string managedStrToNative(System::String^ sysstr)
{
  using System::IntPtr;
  using System::Runtime::InteropServices::Marshal;

  IntPtr ip = Marshal::StringToHGlobalAnsi(sysstr);
  std::string outString = static_cast<const char*>(ip.ToPointer());
  Marshal::FreeHGlobal(ip);
  return outString;  
}
于 2013-07-22T14:16:26.310 回答