public static IntPtr StringToIntPtr (string str)
{
if (str == null)
return IntPtr.Zero;
byte [] bytes = System.Text.Encoding.UTF8.GetBytes (str);
IntPtr result = Marshal.AllocHGlobal (bytes.Length + 1);
Marshal.Copy (bytes, 0, result, bytes.Length);
Marshal.WriteByte (result, bytes.Length, 0);
return result;
}