Skip to content

Instantly share code, notes, and snippets.

@dogguts
Created July 2, 2018 08:49
Show Gist options
  • Select an option

  • Save dogguts/66cd6b119a4d0a444cd34997fe4ce2d6 to your computer and use it in GitHub Desktop.

Select an option

Save dogguts/66cd6b119a4d0a444cd34997fe4ce2d6 to your computer and use it in GitHub Desktop.
public static IntPtr StringToHGlobalUTF8(string s, out int length)
{
if (s == null)
{
length = 0;
return IntPtr.Zero;
}
var bytes = System.Text.Encoding.UTF8.GetBytes(s);
var ptr = Marshal.AllocHGlobal(bytes.Length + 1);
Marshal.Copy(bytes, 0, ptr, bytes.Length);
Marshal.WriteByte(ptr, bytes.Length, 0);
length = bytes.Length;
return ptr;
}
public static IntPtr StringToHGlobalUTF8(string s)
{
int temp;
return StringToHGlobalUTF8(s, out temp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment