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?
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
.
To use managed memory in native code, you must copy the contents of the managed memory into native memory first.
So for example:
Copying the contents from managed memory is as follows:
const int len = 50;
BYTE *destination = new BYTE[nLength];
System::Byte source[] = new System::Byte[len];
System::Runtime::InteropServices::Marshal::
Copy(source, 0, IntPtr((void *)destination, len);
Because we are dealing with managed memory, garbage collection can shift and move the managed data to another location and all would be lost if we tried to locate the data we want to convert.
Therefore we want to "pin it in memory" by using __pin to convert from managed to unmanaged:
const int len = 50;
BYTE *source = new BYTE[len];
System::Byte destination[] = new System::Byte[len];
BYTE __pin *managedData = &(destination[0]);
::memcpy(source, managedData, len);
You can simply convert System::String^ objects to MFC CString by
CString* name = new CString(managedName);
where managedName is a managed String.