Hi all,
I read the following article:
Which was very good. It showed me how to pass in an int[]. I ran the code and it worked, the minimum item was passed back. What I want is to modify the array and pass that back so I modified the code in the article to look like this:
/****C++****/
extern
{
for (int i = 0; i < 16; i++)
buf[ i ] = i;
return 0;
}
/****C#****/
[
DllImport("MarshalArray.dll")]extern static int MinArray(ref int[] buf); int[] sampleData = new int[16];
int result = MinArray(ref sampleData);
This however does not work. Has anybody successfully passed an array to a DLL, modified it and got it back
TIA,

Marshalling arrays
DomChi
Alex,
Thanks for the response. I had tried the call without the ref first. It hadn't worked so I tried it with the ref. Sadly this did not work either. I undestand the passing of lengths. I was just trying to make something work and I guess I got sloppy. Thanks for the reminder
.
javi_inv
Alex,
Thanks. This works perfectly.
JanSchreuder
Arrays are reference types and marshaled as LPARRAY by default. In your case it'd be:
[DllImport("MarshalArray.dll")]
extern static int MinArray(int[] buf);
I also highly recommend designing your C++ API so that it takes both array address and array size. Notice that even though you seemingly are passing the array by value, in fact you are passing a pointer to the array data storage, while marshaller keeps it pinned for the duration of the call. Thus any changes you you make on the C++ side will be accessible on the C# side
mdthomann
(Thread moved as requested)
The approach I suggested works. It's been used by me and others on many occasions. Moreover, I've just built a test project and it worked as expected. You can find it at http://www.alexfeinman.com/download.asp doc=MarshalArray.zip