public static string AsString (IntPtr handle)
{
if (handle == IntPtr.Zero)
return null;
int len = CFStringGetLength (handle);
if (len == 0)
return string.Empty;
IntPtr chars = CFStringGetCharactersPtr (handle);
IntPtr buffer = IntPtr.Zero;
if (chars == IntPtr.Zero) {
CFRange range = new CFRange (0, len);
buffer = Marshal.AllocHGlobal (len * 2);
CFStringGetCharacters (handle, range, buffer);
chars = buffer;
}
string str;
unsafe {
str = new string ((char *) chars, 0, len);
}
if (buffer != IntPtr.Zero)
Marshal.FreeHGlobal (buffer);
return str;
}