public static void CopyStrToPtr(IntPtr ptr, String str) { var bytes = new List<byte>(Encoding.UTF8.GetBytes(str)); bytes.Add(0); // append the null-terminated character Marshal.Copy(bytes.ToArray(), 0, ptr, bytes.Count); } }