Sarjo 0 Newbie Poster

I am writing a c# application where I need to use matlab functions from my c# sharp code. From the beginning I used the Matlab Automation Server, which worked fine on my computer which has Matlab 7.4.0 (R2007a) installed. However, the application needs to be usable on computers with older versions of Matlab, and it turned out that the automation server can't be used then.

Now I am trying to solve this problem using Matlab Compiler to create dll files of my matlab functions and then call them from c#, based on these two code examples: http://www.mathworks.com/support/solutions/data/1-X1PFC.html
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=12987&objectType=file

My problem has to do with passing arrays to and from my matlab functions. As long as I only use scalar values or arrays containing only one value, everything works fine, but as soon as the array contains more than one value an error occurs.

This is my code:
The copy.m matlab function only returns a copy of the input argument:

function y = copy(a)
y=a;

This is my c# code (the try and catch statements have been removed here to make more readable):

[DllImport(@"copy.dll")]
private static extern bool copyInitialize();
[DllImport(@"copy.dll")]
private static extern void copyTerminate();
[DllImport(@"copy.dll")]
private static extern bool mlfCopy(int num, ref IntPtr output, [In]IntPtr input);

[DllImport(@"mclmcrrt76.dll")]
private static extern IntPtr mxCreateDoubleMatrix_700([In]Int32 numRows, [In]Int32 numCols, [In]Int32 mxComplexity);
[DllImport(@"mclmcrrt76.dll")]
private static extern IntPtr mxGetPr([In]IntPtr mxArray);
[DllImport(@"mclmcrrt76.dll")]
private static extern bool mclInitializeApplication(string options, int count);
[DllImport(@"mclmcrrt76.dll")]
private static extern void mclTerminateApplication();

static void Main(string[] args)
{
double[] copyInput = { 1.0};

mclInitializeApplication("NULL", 0);
copyInitialize();

IntPtr inValA = mxCreateDoubleMatrix_700(copyInput.Length, 1, 0);

int size = Marshal.SizeOf(copyInput [0]) * copyInput.Length;

IntPtr outVal = Marshal.AllocHGlobal(size);

// Copy the array to unmanaged memory.
Marshal.Copy(copyInput , 0, inValA, copyInput .Length);

//Run matlab function
mlfCopy(1, ref outVal, inValA);

// Get return value
double[] copyAns = new double[copyInput .Length];
Marshal.Copy(outVal, copyAns, 0, copyInput.Length);

copyTerminate();
mclTerminateApplication();

When running this code the returned value is identical to the value in copyInput, however, as soon as the size of copyInput is changed (double[] copyInput = { 1.0, 2.0}; ) an error occurs (without any more information than a standard windows error message). I have also tried to use:

[DllImport(@"copy.dll")]
private static extern bool mlxCopy(int numOut, ref IntPtr output, int numIn, [In]IntPtr input);
mlxCopy(1, ref outVal, 1, inValA);

Instead of the mlfCopy, but this doesn't work either.

Is there anyone who has an idea of how to solve this problem? What I really need is to be able to pass two-dimensional arrays to and from matlab functions.

Thanks in advance!