We use PInvoke for the interaction between C # and C ++.
I have an interop structure, as shown below, with the same structure as the C ++ structure on the other hand.
[StructLayout(LayoutKind.Sequential)] public struct MeshDataStruct : IDisposable { public MeshDataStruct(double[] vertices, int[] triangles , int[] surfaces) { _vertex_count = vertices.Length / 3; _vertices = Marshal.AllocHGlobal(_vertex_count*3*sizeof (double)); Marshal.Copy(vertices, 0, _vertices, _vertex_count); }
Now I would like to add a second ctor
public MeshDataStruct(bool filled_in_by_native_codee) { _vertex_count = 0; _vertices = IntPtr.Zero; }
and then write a method in C ++ that allows C ++ to populate the data. This would allow us to use the same structure for input as well as output ...
However, as I understand it, AllocHGlobal
is available in C # and C ++ / Cli, but not in pure C ++.
So my question is: how can I allocate memory in C ++ so that I can safely free it on the C # side with a call to Marshal.FreeHGlobal(...)
?
source share