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!

Comments

AVG Antivirus Review
AVG Antivirus Review United States on 4/19/2010 11:43 PM Thank you for this helpfull review. It really was helpfull. Keep up the good work.
Rapidshare
Rapidshare United States on 4/23/2010 7:39 PM I\'m happy I found this blog, I couldnt discover any info on this subject matter prior to. I also run a site and if you want to ever serious in a little bit of guest writing for me if possible feel free to let me know, i\'m always look for people to check out my site. Please stop by and leave a comment sometime!
locksmith 24 hours
locksmith 24 hours Albania on 5/3/2010 5:19 AM Great weblog, keeping me from working
Miquel Boilard
Miquel Boilard Netherlands on 5/5/2010 2:26 PM

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading