What is the correct syntax for calling this C function in C#? The function uses pointers and arrays
<p>I'm trying to call a function from a DLL written in C. I need to call the function in C#, but I believe I'm running into issues with the syntax. I have a working example that uses the <a href="https://docs.python.org/2/library/ctypes.html" rel="nofollow">ctypes</a> library in Python. Unfortunately the DLL requires a dongle to run, so right now I'm looking for help regarding any obvious discrepancies in the syntax between C, Python, and C#. </p>

<p>The C function has the format</p>

<p><code>int (int nID, int nOrientation, double *pMTFVector, int *pnVectorSize );</code> 
(I'm really unfamiliar with pointers and the PDF documentation has the asterisks surrounded by spaces so I'm not sure what the asterisk should be attached to)</p>

<p>The function of this code is to accept nID and nOrientation to specify features in an image, and then populate an array with values. The documentation describes the outputs below: </p>

<pre><code>out; pMTFVector; array of MTF values, memory is allocated and handled by application, size is given by pnVectorSize

in,out; pnVectorSize maximum number of results allowed to store in pMTFVector, number of results found and stored in pMTFVector
</code></pre>

<p>The python code that actually works is:</p>

<pre><code>lib=cdll.LoadLibrary("MTFCameraTester.dll")
lib.MTFCTGetMTF(c_uint(0), c_int(0), byref((c_double * 1024)()), byref(c_uint(1024)))
</code></pre>

<p>The code I've tried is: </p>

<pre><code> [DllImport("MTFCameraTester.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    public extern static MTFCT_RETURN MTFCTGetMTF(UInt32 nID, int orientation, double[] theArray, IntPtr vectorSize);

        double[] myArray = new double[1024];
        IntPtr myPtr = Marshal.AllocHGlobal(1024);
        returnEnum = MTFCTGetMTF(0, 0, myArray, myPtr);
</code></pre>

<p>When the code is run my <code>returnEnum</code> is <code>-1</code>, which is specified as an error in the documentation. This is the best result I've come across, as I've had many Stack Overflow errors when trying varying combinations of <code>ref</code> and <code>out</code> </p>