利用Marshal来管理非托管资源

 

void MarshalChartDemo()

{

string name = "xuwei";

IntPtr pName = Marshal.AllocHGlobal(name.Length);

Marshal.Copy(name.ToCharArray(), 0, pName, name.Length);

char[] cName = new char[name.Length];

Marshal.Copy(pName, cName, 0, name.Length);

}

 

void MarshalByteDemo()

{

string name = "xuwei";

var nameBytes = Encoding.Default.GetBytes(name);

IntPtr pName = Marshal.AllocHGlobal(nameBytes.Length);

Marshal.Copy(nameBytes, 0, pName, name.Length);

byte[] cName = new byte[nameBytes.Length];

Marshal.Copy(pName, cName, 0, nameBytes.Length);

string temp = Encoding.Default.GetString(cName);

}

 

  

 

你可能感兴趣的:(利用Marshal来管理非托管资源)