Passing a matlab array into C# DLL

4 views (last 30 days)
John
John on 7 Feb 2013
I have C# interfaces to a proprietary (internal) image processing library.
Interfaces look something like this:
void RGBConvert::ConvertFromRGBToYCbCr(
int ncols,
int nrows,
IntPtr rgb,
IntPtr out)
Where IntPtr is a pointer to the underlying memory. I have called .NET objects many times from Matlab, but passing large data arrays into C# is not something I've done, and am reaching a dead end.
The Matlab manual: http://www.mathworks.com/help/matlab/matlab_external/handling-net-data-in-matlab_bte9oxc-1.html#bte9o7y-1 indicates that IntPtr is not supported. By which I mean, it doesn't covert an array of Unsigned8Bit integers into an IntPtr to pass to the method. (If you construct an IntPtr through some other mechanisms, Matlab will treat it like any other .NET object and pass it around.) So, how do I find a pointer to the Matlab data, and pass it to this method?
Clearly, one approach would be to use the normal Matlab C/C++ interfaces to "trick" matlab into revealing the pointer to the underlying array data structure, and then pass that pointer to the IntPtr constructor. Then pass the IntPtr on. We had hoped to have a "pure" C# implementation, and to not have to have a C/C++ interface in addition to the C# interface.
Another avenue might be to take the Matlab ConvertArray call, to convert a matlab array to a .NET array, but then I still need an IntPtr pointing at the raw data.
I don't mind extraneous memory copies here, this is not performance critical code, this is experimentation code.
Anyone have experience with this issue, and recommendations?
Thoughts?

Answers (1)

Conrad
Conrad on 8 Feb 2013
Hi John, what about creating a wrapper method with signature
double[,] RGBConvertWRP(int ncols,
int nrows,
double[,] rgb)
and then using NET.convertArray to pass the rgb variable to this new method? You can then just call your original RGBConvert method from this newly created wrapper method?
Conrad
  1 Comment
John
John on 12 Feb 2013
@Conrad the NET.convertArray calls create .NET arrays, and not c++ arrays, which is what an IntPtr points to.

Sign in to comment.

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!