CLR system::string^转std::string

    技术2023-06-13  80

    MSDN文档:https://docs.microsoft.com/en-us/cpp/dotnet/overview-of-marshaling-in-cpp?redirectedfrom=MSDN&view=vs-2019

    使用实例

    #include <msclr\marshal.h> #include <msclr\marshal_cppstd.h> using namespace msclr::interop; using namespace System::Runtime::InteropServices; stdAlgoManage gAlgoManage; long VisionApm::Apm_CS::GetAlgoList(List<String^>^% AlgoList1D) { vector<string> AlgoList; stdAlgoList::GetAlgoList(AlgoList); for (vector<string>::iterator iter = AlgoList.begin(); iter != AlgoList.end(); iter++) { String^ tmpMStr = marshal_as<String^>(iter->c_str()); AlgoList1D->Add(tmpMStr); } return 0; } long VisionApm::Apm_CS::AddAlgoFun(String^ funName) { char* pchar = (char*)Marshal::StringToHGlobalAnsi(funName).ToPointer(); std::string tmpFunName=marshal_as<std::string>(funName); gAlgoManage.AddAlgoFun(tmpFunName); Marshal::FreeHGlobal(IntPtr((void*)pchar)); return 0; }
    Processed: 0.013, SQL: 9