Unicode string detection

Written by Troy Howard

23 July 2009

I had the need to detect wether or not a given string (in .Net/C#) was unicode or not.. Specifically filenames. I had a situation where a filename might be passed to me, that could possibly contain unicode. If it DID contained unicode characters, I needed to run GetShortPathName and get the 8.3 filename for the file, before passing it into a legacy component that couldn't handle unicode names…

unicode scrabble

Well, a "big hammer approach" might just call GetShortPathName on every filename, just to be sure… But that's a costly API call if your having to do this a million times a second.

So, long story short, I wrote this little function to detect unicode in a .Net string:

public static bool IsUnicode(string s) {
    return s != Marshal.PtrToStringAnsi(Marshal.StringToHGlobalAnsi(s)); 
} 

Now homework for all you kiddies out there… Is this code a memory leak? If so, what should you do to fix it? If not, why not?