Pass a ref array to the method in C# (which written in C++)

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

Hi All!
I have a dll, which is written in VC++.NET.
In the dll, I write a method like that
void SetData(double* array, int len)
.....

then, I use this dll in a C#-Application, trying to pass
double[] myArray = new double[10];
MyObj.SetData(myArray, 10);

it posted an error: Can not convert from 'doublt[]' to 'ref double'.
Pls help me to solve this problem!
thanks!
 
Hi Alex
you can try

double[] myArray = new double[10];
unsafe
{
fixed(double *myPtr = myArray)
SetData(myPtr, 10);
}

with "Allow unsafe code blocks" configuration enable.
 
Thank Shortgrey!

but I don't know how my project has not worked yet, event I have done
anything as you guide

double[] x = new double[10];

double[] y = new double[10];



for (int i = 0; i < 10; i++)

{

x = i;

y = 10*i;


unsafe

{

fixed(double *xx = x)

fixed(double *yy = y)

myDllObj.SetData(xx, yy, 10, true);


}



The same error!






Shortgrey said:
Hi Alex
you can try

double[] myArray = new double[10];
unsafe
{
fixed(double *myPtr = myArray)
SetData(myPtr, 10);
}

with "Allow unsafe code blocks" configuration enable.
Hi All!
I have a dll, which is written in VC++.NET.
In the dll, I write a method like that
void SetData(double* array, int len)
....

then, I use this dll in a C#-Application, trying to pass
double[] myArray = new double[10];
MyObj.SetData(myArray, 10);

it posted an error: Can not convert from 'doublt[]' to 'ref double'.
Pls help me to solve this problem!
thanks!
 
Back
Top