Passing array by reference from C# to c++/cli
-
Hi, I'm trying to write a function in a c++/cli dll that will receive an empty array from C# (float[] a), use gcnew to allocate the array and then I need to use the array in C#. I tried regular transfer but after the function returns the array is still null in C#. Current trial is: C#:
float[] a;
InitArray(a);c++/cli:
void CLIClass::InitArray(array^ a)
{
a = gcnew array(100);
for(int i=0; i<100; i++)
a[i] = 10.0F;
}I do not wish to return the array as a return value (the function should actually return 3 arrays). Thanks!
-
Hi, I'm trying to write a function in a c++/cli dll that will receive an empty array from C# (float[] a), use gcnew to allocate the array and then I need to use the array in C#. I tried regular transfer but after the function returns the array is still null in C#. Current trial is: C#:
float[] a;
InitArray(a);c++/cli:
void CLIClass::InitArray(array^ a)
{
a = gcnew array(100);
for(int i=0; i<100; i++)
a[i] = 10.0F;
}I do not wish to return the array as a return value (the function should actually return 3 arrays). Thanks!
Does passing a tracking reference work? void CLIClass::InitArray(array^% a)
Mark Salsbery :java:
-
Hi, I'm trying to write a function in a c++/cli dll that will receive an empty array from C# (float[] a), use gcnew to allocate the array and then I need to use the array in C#. I tried regular transfer but after the function returns the array is still null in C#. Current trial is: C#:
float[] a;
InitArray(a);c++/cli:
void CLIClass::InitArray(array^ a)
{
a = gcnew array(100);
for(int i=0; i<100; i++)
a[i] = 10.0F;
}I do not wish to return the array as a return value (the function should actually return 3 arrays). Thanks!
your C# code should use the
out
orref
keyword:float[] a;
InitArray(out a);float[] a=null;
InitArray(ref a);ref would fail here as long as a isn't initialized. Without
ref/out
your callee can modify the array content, however it can't replace the array. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Does passing a tracking reference work? void CLIClass::InitArray(array^% a)
Mark Salsbery :java:
-
I gave him a 5. It's nice when you do that when people help you out here.
Regards, Nish
My technology blog: voidnish.wordpress.com You've gotta read this : Using lambdas - C++ vs. C# vs. C++/CX vs. C++/CLI
-
Does passing a tracking reference work? void CLIClass::InitArray(array^% a)
Mark Salsbery :java:
5!
Regards, Nish
My technology blog: voidnish.wordpress.com You've gotta read this : Using lambdas - C++ vs. C# vs. C++/CX vs. C++/CLI