开发者

Converting between managed and unmanaged code

I was wondering if the following is valid for converting between a managed string and a standard string:

String ^ mymgdstring;
std::string mystdstring = *[PTR TO MYMGDSTRING, NOT SURE OF SYNTAX]

(i.e. create a std string which is equal to the dereferenced pointer to a managed string)

If this isn't valid, why not? What is the best method (i.e. most effici开发者_C百科ent) for converting between these?

Also, how do you get a pointer to a managed string?


A copy is necessary, because the .NET String data can be moved around during garbage collection.

You can assume marshal_as is the most efficient way to do this conversion. If a faster way is found, marshal_as will be updated to use it (it's a template and can be specialized).

You can get an interior pointer to the data of a System::String (it will be in Unicode, that's the internal format of .NET strings) using PtrToStringChars. To use it with native code, you must first pin the string by using pin_ptr instead of interior_ptr.


C++/CLI comes with a function called MarshalAs which can perform the conversion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜