Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It should be this easy:</p> <pre><code>[StructLayout(LayoutKind.Sequential)] public struct Vertex { float x; float y; float z; int ID; } [DllImport("library.dll", CallingConvention=CallingConvention.StdCall)] public static extern float first(Vertex[] verticies, int arrLen); </code></pre> <p>The issues you <em>may</em> run into would be if there is any packing done on the C version of the struct, and possibly the struct layout. If the layout doesn't match, you may want to change it to <code>LayoutKind.Explicit</code> and use the <code>[FieldOffset(0)]</code> attribute on each field. C would also have no idea, the length of the verticies array passed, so if that changes, you'd want to pass that along to the method.</p> <p>To get an array back:</p> <pre><code>[DllImport("library.dll", CallingConvention=CallingConvention.StdCall)] public static extern Vertex[] another(float a); </code></pre> <p>The marshaler handles all of the memory issues when passing arguments in, but returning the array it can't do anything. Since the memory is allocated on the unmanaged heap, the GC has no idea about it, and you <em>will</em> wind up with a memory leak. The marshaller will simply copy the native structs to the managed struct array, but it can't free the memory you've allocated with <code>malloc</code>.</p> <p>The easiest way to get around it, if you can change the C++ code, would be to change the signature of <code>another</code> to take in an array of verticies (and the length of the array) instead of returning one. I don't need to write out any code for you that does this, @DavidHeffernan has already done this in his answer, the part of the break.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload