structs and arrays in c++ and c#...

Started by
10 comments, last by xyz 20 years, 1 month ago
someone?
Advertisement
Yes there is a better way, with a little help of a GCHandle.

example:

[StructLayout(LayoutKind.Sequential)]
public struct Triangle {
int x1, x2, x3;
}

...

public class Mesh {

public Triangle[] Triangles;
private GCHandle triangleHandle;
public IntPtr TrianglePointer;

public Mesh(Triangle[] triangles) {
this.Triangles = triangles;
AllocateHandles();
}

public void AllocateHandles() {
triangleHandle = GCHandle.Alloc(Triangles, GCHandleType.Pinned);
TrianglePointer = triangleHandle.AddrOfPinnedObject();
}

public void FreeHandles() {
triangleHandle.Free();
TrianglePointer = IntPtr.Zero;
}

~Mesh() {
FreeHandles();
}

...

Mesh mesh = new Mesh(...);

...

Gl.glDrawElements(Gl.GL_TRIANGLES, mesh.Triangles.Length*3, Gl.GL_UNSIGNED_INT, mesh.TrianglePointer);

You have to lock the managed memory somehow, otherwise the GC could move it.




[edited by - Xanthos on March 8, 2004 3:50:35 PM]

This topic is closed to new replies.

Advertisement