Hello there JocK. I've been grappling with same question actually, and found
out that one can pass in the whole array, when the C function is really
expecting a whole array and not just a pointer, or pass in a pointer when
it's expecting that. I've copied and pasted the transcript of corresponding
with James, below;
-----------------------
Hi James. I found out in the meantime that for the particular example where
the myFunc in the dll is expecting a real array ("data[]") and not a pointer
to one, I can pass in the array name itself, rather than a pointer to it.
With a simple test function compiled into the dll, which has a declaration in
the C# program of
[DllImport("process.dll")]
public extern static int test(int [] int_in_out);
and then with a C# array like
int [] myInt = new int[4];
after filling up the array with four integers, I can pass it in like
int result = test(myInt);
so I think the entire array is being passed in, copied in rather than just a
pointer being passed. I tried changing the "test" function so it expected a
pointer, and found that your method does indeed work. Thanks.
John
[quoted text, click to view] "James" wrote:
> I would use this:
>
> fixed(byte *ptrData = data)
> {
> myFunc(ptrData);
> }
>
> where data is C# array.
> ptrData is the pointer to the C# array, then you may use pointer as in C.
> you will need the keyword unsafe for compilation.
>
> "johnmann56" <johnmann56@discussions.microsoft.com> wrote in message
> news:383BA46E-4945-4BF6-9A7F-FBE53230BB95@microsoft.com...
> >I have what I imagine is a simple question, but don't know how to approach
> >it
> > really. I have a dll compiled from ANSI C, which has a function
> > declaration
> > with an array;
> >
> > void myFunc(int data[], ...);
> >
> > How can I send in a C# array to "data[]" and get it back out? Thanks.
>
>
-------------------------------
Hope that answers your question.
John
[quoted text, click to view] "JocK" wrote:
> Hello everybody,
>
> We are evaluating the use of C# and GDI+ to develop a GUI based
> application which elaborate image (stored as array of native type).
> This application needs to call several native C functions in order to
> perform some intensive computation on the image. Basically I will store
> the image into array of native type (such as byte, int or double) in C#
> code, than I will call the native C function passing the array. The C
> function could:
>
> - modify the array of data, do not return anything
> - create a new array and return it, using the passed array data
>
> Do this have a performance overhead? Do the arrays need to be copied
> while they are passed and/or returned?
>
> TIA
> JocK
>