byte[] data = new byte[] { 1, 2, 3, 4 }; IntPtr ptr = Marshal.AllocHGlobal(data.Length); Marshal.Copy(data, 0, ptr, data.Length); // ptr now contains a pointer to unmanaged memory containing the data array
string str = "Hello, world!"; IntPtr ptr = Marshal.StringToHGlobalAnsi(str); // ptr now contains a pointer to null-terminated string in unmanaged memoryThe Marshal class is part of the System.Runtime.InteropServices namespace, which is included in the .NET Framework and .NET Core.