Groups | Blog | Home
all groups > dotnet interop > october 2005 >

dotnet interop : Passing huge array from C# to c function


JocK
10/13/2005 3:32:47 AM
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
johnmann56
10/13/2005 9:13:24 PM
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]
-------------------------------

Hope that answers your question.
John




[quoted text, click to view]
JocK
10/14/2005 9:15:27 AM
John,
what about performances if I have

int[] myInt = new int[10000000]

Is the array copied during the marshalling?

Thanks
JocK
Jason Newell
10/14/2005 2:28:23 PM
Jock,

I think you might want to look into C#'s fixed keyword. Here are some
articles on the topic. HTH.

http://msdn.microsoft.com/library/en-us/csspec/html/vclrfcsharpspec_A_6.asp
http://msdn.microsoft.com/library/en-us/dncscol/html/csharp10182001.asp

Jason


[quoted text, click to view]
Mattias Sjögren
10/14/2005 10:20:43 PM

[quoted text, click to view]

To pass in an existing managed array to a native function, the array
can simply be pinned and no copying has to happen. (Assuming the
callee expects a "C-style" array, not a SAFEARRAY).

If the native code allocates and returns an array, it will have top be
copied to the managed heap to appear as a managed array. If you want
to manipulate the native array directly without copying, you have to
use unsafe code in C# and can't treat it like a real array.

So to avoid memory consuming copying, it's best if you allocate the
array on the managed side and simply let the C code manipulate the
existing buffer instead of allocating a new one.


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
JocK
10/17/2005 9:01:07 AM

Mattias Sj=F6gren ha scritto:

[quoted text, click to view]

Mattias,

thanks a lot, that's is what i really need to know!
Could you nicely point me to some examples on how to call from C# a
native C function that create and return an array (array of char, for
instance) and copy it to a managed array?=20

Thanks a lot
Cheers
JocK
AddThis Social Bookmark Button