Marshal float/double array at DLLImport of C++ function

T

Tiago Falcão

I search on google, msdn,... and i not find no solutions. Any ideia?

----------------------------
C++ Example Source:

extern "C" __declspec(dllexport)
double * extract(int nrows,int ncols,int * RGB){
int i=0;
int j=ncols*nrows;
double* x;
x=(double*)calloc(1,sizeof(double)); // I
need return one double array
x[0]=((double)RGB[0]+RGB[1]+RGB[2])/3;
return x ;
}

extern "C" __declspec(dllexport)
double distance(double* img1, double* img2){
return img1[0]-img2[0];
}


--------------------------
C# code:

[DllImport("C:/Users/tiago/Desktop/teste.dll")]
[MarshalAs(UnmanagedType.LPArray, ArraySubType =
UnmanagedType.R8, SizeParamIndex = 1)]
public static extern Double[] extract(int nrows, int ncols,
int[] RGB);

[DllImport("C:/Users/tiago/Desktop/teste.dll")]
public static extern Double distance(Double[] img1, Double[]
img2);

---------------------------
 
N

Nicholas Paldino [.NET/C# MVP]

Tiago,

For the extract function, you have to declare the return value as an
IntPtr, and then marshal the contents manually.

You also have to figure out how many elements are in the array in order
to marshal it correctly.

Finally, you have to make sure you deallocate the memory, which means
you will have to expose a function that takes a pointer and then passes that
to the free function (you will want to do this right after you return the
new array).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top