使用MarashalAs属性自由C#自动编组数据

时间:2015-05-20 23:19:14

标签: c# c++ c marshalling free

CLR可以使用marshal作为attrubte将托管类型编组为非托管c / c ++类型 我需要的是什么是释放这个分配的内存的正确方法..例如::

在C / C ++代码中

char* dummy(char* text){
//do some thing
return text;//dummy way to return it to the caller to free it.
}

和C#

[DllImport("test.dll",CallingConvention=CallingConvention.Cdecl)]
static extern IntPtr dummy([MarshalAs(UnmanagedType.LPStr)] string text);

最后是测试

    C# Test
IntPtr marshaledData=dummy("any text");
//what is the proper way to free this memory location

0 个答案:

没有答案