public static string ToLower(string src, string locale) { if (src == null) { return(string.Empty); } int length = src.Length + 10; IntPtr resPtr = Marshal.AllocCoTaskMem(length * 2); try { ErrorCode err; int outLength = NativeMethods.u_strToLower(resPtr, length, src, src.Length, locale, out err); if (err > 0 && err != ErrorCode.BUFFER_OVERFLOW_ERROR) { throw new Exception("UnicodeString.ToLower() failed with code " + err); } if (outLength > length) { Marshal.FreeCoTaskMem(resPtr); length = outLength + 1; resPtr = Marshal.AllocCoTaskMem(length * 2); NativeMethods.u_strToLower(resPtr, length, src, src.Length, locale, out err); } if (err > 0) { throw new Exception("UnicodeString.ToLower() failed with code " + err); } string result = Marshal.PtrToStringUni(resPtr); // Strip any garbage left over at the end of the string. if (err == ErrorCode.STRING_NOT_TERMINATED_WARNING && result != null) { return(result.Substring(0, outLength)); } return(result); } finally { Marshal.FreeCoTaskMem(resPtr); } }