internal static IntPtr ManagedToNative(string value)
{
if (value == null)
return IntPtr.Zero;
// not null terminated
byte[] strbuf = Encoding.UTF8.GetBytes(value);
IntPtr buffer = Marshal.AllocHGlobal(strbuf.Length + 1);
Marshal.Copy(strbuf, 0, buffer, strbuf.Length);
// write the terminating null
#if NET20
Marshal.WriteByte(new IntPtr(buffer.ToInt64() + strbuf.Length), 0);
#else
Marshal.WriteByte(buffer + strbuf.Length, 0);
#endif
return buffer;
}