A couple of days ago I began writing an article for MSDN Magazine. The article is about Metro, Windows 8 and interop between Win32 and WinRT. One of the most frequent and error prone operation is “String manipulation”, due to the possibility of a buffer overflow, encoding, an invalid pointer or different data types – Yes… data types to represent a string, it’s not the same a char to a wchar_t or ANSI to UNICODE.
Every Windows based on NT (2000, XP & later) natively supports UNICODE, Actually, UNICODE has been the standard ever since. In fact, every string in .NET is UNICODE and strings like every other object are allocated by VirtualAlloc.
Now back to the article I’m currently writing, in my personal case I found in XML strings a convenient way to pass information between processes & DLLs, let’s say it’s like my custom IPC mechanism, of course when the amount of information being transferred is not a huge file but more an XML representation of an given object.
In the code snippet shown below, we got a method called ParseXml which calls another method called ReplaceSpecialCharactersIfAny.
The implementation of ReplaceSpecialCharacterIAny is pretty straightforward, we store in a std::map the character to be replaced & the replacement string. We also reserve twice the length of the input XML string (We don’t know for certain how many replacements we’ll do, so twice the size to be used as a buffer seems to be fair enough). Then we replaced every occurrence of the special characters in the std::map and an encoded String^ is returned.
Another alternative to this approach could’ve also been via Regex (but I’ll leave it to another post) or a more specialized way.