Categories: General
Posted by
pieterg on
3/3/2006 2:02 AM |
Comments (4)
I have been doing some coding the last few days and I thought that for the fun and heck of it i ll post some of my research here...
C++ has long been a very involved language and with that I mean the difficult topics such as decent pointer handling and memory management in general. C++/CLI is the new specification for C++ language extensions and it supersedes the previous Managed C++ which was very ugly to work with in the sense of readability.
C++/CLI introduces the new concept of handles which refers to a managed reference to an object where as pointers just point to a memory location.
C++/CLI serves as a good wrapping language around legacy code in the sense that if you have some C++ code base you could easily wrap this code and start taking advantage of the CLR.
The conversion of the day is converting char* to String^ and back.
Taking a look at how to convert a char* to a String^ brings to light one method from the Marshal class.
Marshal::StringToHGlobalUni() which copies data from a managed String into unmanaged memory.
This method returns a pointer that will point to the unmanaged memory location. Storing that in a IntPtr should work and converting that IntPtr to a pointer is all that s left.Example
String^ managedName = "Pieter";char* unmanagedName = new
char;IntPtr ptr =
Marshal::
StringToHGlobalUni(managedName);unmanagedName = (
char
*)ptr.ToPointer();Marshal::FreeHGlobal(ptr);
Converting a char* is very easy since the String class constructor takes a const char* :)Examplechar* unmanagedName = "Pieter";
String^ managedName = gcnew String(unmanagedName);
That s enough garbling for today ;)
I have some DirectX fun to attend too.Share this post:email it! | bookmark it! | digg it! | reddit! | kick it! | live it!
67700430-e0b5-4882-a06d-9c6e89e29b95|0|.0