C ++中托管类型和非托管类型之间的转换?

C ++中托管类型和非托管类型之间的转换?

问题描述:

当我在C ++中使用GUI时,文本字段存储为托管字符串,我想。我需要一种方式将它们转换为标准int,浮动和字符串。任何帮助?

When I use a GUI in C++ the text fields are stored as managed strings, i think. I need a way to convert them to standard ints, floats and strings. Any help?

您可以使用 Marshal.StringToHGlobalAnsi 。确保您在完成后释放它,请致电 Marshal.FreeHGlobal 。要将字符串转换为数字值,可以使用常规的.NET解析函数,如 Int32.Parse

You can convert a System.String into an unmanaged char * using Marshal.StringToHGlobalAnsi. Make sure you free it when you're done by calling Marshal.FreeHGlobal. To convert the strings to numeric values, you can use the regular .NET parsing functions such as Int32.Parse.