如何将System :: String ^转换为const char *? [英] How do I convert a System::String^ to const char*?
本文介绍了如何将System :: String ^转换为const char *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C ++ / CLI中开发一个应用程序,并且有一个非托管代码的csv文件写入库,我想从托管部分使用它。所以我的函数看起来像这样:
I'm developing an app in C++/CLI and have a csv file writing library in unmanaged code that I want to use from the managed portion. So my function looks something like this:
bool CSVWriter::Write(const char* stringToWrite);
...但我真的很努力将我闪亮的System :: String ^ 。基本上我是希望通过做一些类似的调用:
...but I'm really struggling to convert my shiny System::String^ into something compatible. Basically I was hoping to call by doing something like:
if( m_myWriter->Write(String::Format("{0}",someValueIWantToSave)) )
{
// report success
}
推荐答案
using namespace System::Runtime::InteropServices;
const char* str = (const char*) (Marshal::StringToHGlobalAnsi(managedString)).ToPointer();
从 Dev Shed 。
这篇关于如何将System :: String ^转换为const char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文