I think the first optimization you could do here would be to make your first attempt to call MultiByteToWideChar to start with a buffer instead of a null pointer. Since you specified CP_UTF8 , MultiByteToWideChar must go all the way to determine the expected length. If there is a length that is longer than the vast majority of your lines, you can optimize the distribution of the buffer of that size on the stack; and if that fails, then move on to dynamic allocation. That is, move the first branch if your if/else block is outside of if/else .
You can also save some time by calculating the length of the original string once and passing it explicitly - this way MultiByteToWideChar should not do strlen every time you call it.
However, it seems that if the rest of your project is C #, you should use the .NET BCL class libraries designed for this, instead of having side-by-side assembly in C ++ / CLI for the sole purpose of converting strings, This is for System.Text.Encoding .
I doubt that any caching data structure that you can use here will be significant.
Oh, and do not ignore the result of MultiByteToWideChar - you should not lead to anything void , you have undefined behavior in the MultiByteToWideChar event.
Billy oneal
source share