What is the best way to convert between char* and System::String in C++/CLI

前端 未结 5 637
情深已故
情深已故 2020-11-29 01:07

What is the approved way to convert from char* to System::string and back in C++/CLI? I found a few references to marshal_to<> templated functions on Google, but it appea

5条回答
  •  天命终不由人
    2020-11-29 01:57

    What we did is made a C++\CLI object that held the string in unmangaed code and would give out manged copies of the item. The conversion code looks very much like what Stan has on his blog (I can't remember it exactly)(If you use his code make sure you update it to use delete[]) but we made sure that the destructor would handle releasing all the unmanged elements of the object. This is a little overblown but we didn't have leaks when we tied into legacy C++ code modules.

提交回复
热议问题