C# and array in a dll

M

Mircea Dragan

Hi,

I have a problem. Here is the description:

I have a dll which I call from a C# program. The C# program passes an
array
of doubles to the dll function. Everything works fine as long as I don't
change
any array element in the dll function.

The problem comes into picture when I want to change the values of the
array in the dll.
When the function returns, the array in the C# program is truncated to
only one element
(the first one).

I want to send two arrays of double, one of them which is not changed
and one of them
which is changed in the dll (its elements are computed by the dll
function). When the
dll function returns, both arrays are truncated to one element. Only the
first element
remains, all the others are gone.

I checked in the dll function, and everything works fine there.

Does somebody have a solution?

Thank you!

Mircea Dragan
 
B

Bruce Wood

What language is the "dll function" written in? Is it also a .NET
managed dll, or is it an unmanaged DLL? If it's unmanaged, how are you
calling it?

Could you post a short, complete program (a few lines) that puts some
things into a small array, calls the dll, and then report what is in
the array when it returns?
 
M

MuZZy

Mircea said:
Hi,

I have a problem. Here is the description:

I have a dll which I call from a C# program. The C# program passes an
array
of doubles to the dll function. Everything works fine as long as I don't
change
any array element in the dll function.

The problem comes into picture when I want to change the values of the
array in the dll.
When the function returns, the array in the C# program is truncated to
only one element
(the first one).

I want to send two arrays of double, one of them which is not changed
and one of them
which is changed in the dll (its elements are computed by the dll
function). When the
dll function returns, both arrays are truncated to one element. Only the
first element
remains, all the others are gone.

I checked in the dll function, and everything works fine there.

Does somebody have a solution?

Thank you!

Mircea Dragan


Try adding "ref" when passing the array
 
M

Mircea Dragan

Bruce said:
What language is the "dll function" written in? Is it also a .NET
managed dll, or is it an unmanaged DLL? If it's unmanaged, how are you
calling it?

Could you post a short, complete program (a few lines) that puts some
things into a small array, calls the dll, and then report what is in
the array when it returns?

Hi,

Thank you for the answer.
My dll is in C++. The call from C# to C++ dll works fine, as long as
I don't change any value of the array element.

Here is a short scenario of both calls:

C#
+++++++++++++++++++
[DllImport("test_dll.dll")]
public static extern double Test(
ref double[] val, int len, int len_ret,
[MarshalAs(UnmanagedType.LPStr)] string fName,
ref double[] retval);

The call of dll function:
result=Test(ref myarray, myarray_dim, retarray_dim,
"myfile.txt", ref retarray);

++++++++++++++++++

The C++ prototype
==================
double __declspec(dllexport) CALLBACK Test(double**, int,
int, LPSTR, double**);

==================
In C++ function I want to compute some values for retarray (the second
array of doubles). The
values are computed correctly. The problem is when the function returns.

I have tried with one array (to store the computed values over the
original ones, which
I don't need any more) and with both arrays. In either case, both arrays
are truncated to
the first element (for the computed array, only the first element, which
is computed
correctly). If I don't change any value in these arrays, everything works
fine.
The dimension of both arrays are known and are sent as parameters to C++
function.
The storage for both arrays is done in C#, so C++ function only computes
the values
for one array.

Regards,
Mircea
 
W

Willy Denoyette [MVP]

Bruce Wood wrote:
What language is the "dll function" written in? Is it also a .NET
managed dll, or is it an unmanaged DLL? If it's unmanaged, how are you
calling it?
Could you post a short, complete program (a few lines) that puts some
things into a small array, calls the dll, and then report what is in
the array when it returns?


Hi,

Thank you for the answer.
My dll is in C++. The call from C# to C++ dll works fine, as long as
I don't change any value of the array element.

Here is a short scenario of both calls:

C#
+++++++++++++++++++
[DllImport("test_dll.dll")]
public static extern double Test(
ref double[] val, int len, int len_ret,
[MarshalAs(UnmanagedType.LPStr)] string fName,
ref double[] retval);

The call of dll function:
result=Test(ref myarray, myarray_dim, retarray_dim,
"myfile.txt", ref retarray);

++++++++++++++++++

The C++ prototype
==================
double __declspec(dllexport) CALLBACK Test(double**, int,
int, LPSTR, double**);

==================
In C++ function I want to compute some values for retarray (the second array of doubles). The
values are computed correctly. The problem is when the function returns.

I have tried with one array (to store the computed values over the original ones, which
I don't need any more) and with both arrays. In either case, both arrays are truncated to
the first element (for the computed array, only the first element, which is computed
correctly). If I don't change any value in these arrays, everything works fine.
The dimension of both arrays are known and are sent as parameters to C++ function.
The storage for both arrays is done in C#, so C++ function only computes the values
for one array.

Regards,
Mircea


Don't need to pass array's by ref, array's are reference types, the value passed is a reference (reference is passed by value) to the array so the callee can change the array elements.

Change your code to something like this:



C#
[DllImport("test_dll.dll")]
private static extern double func_c( double[] val, int len,
[MarshalAs(UnmanagedType.LPStr)] string fName
);


static void Main()
{
double[] a1 = new double[10]{1.30, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
Console.WriteLine(func_c(a1, a1.Length, "Teststring"));
foreach(double d in a1)
{
Console.WriteLine(d);
}

}



Here I use the array syntax "double d[]" as argument instead of pointer syntax "double* d", that way it's possible to use array indexing in the code.

extern "C" {
__declspec(dllexport) double __stdcall func_c (double val[], int len, char* s);

}

double __stdcall func_c (double d[], int len, wchar_t* s)
{
double ret = 0.0;
printf_s("Len %d\n", len);
for (int n = 0; n < len; n++)
{
ret += val[n];
val[n] *= 0.345; // change the value in array element
}
return ret;
}



Willy.
 
M

Mircea Dragan

Willy Denoyette said:
What language is the "dll function" written in? Is it also
a .NET
managed dll, or is it an unmanaged DLL? If it's unmanaged,
how are you
calling it?

Could you post a short, complete program (a few lines)
that puts some
things into a small array, calls the dll, and then report
what is in
the array when it returns?


Hi,

Thank you for the answer.
My dll is in C++. The call from C# to C++ dll works fine, as
long as
I don't change any value of the array element.

Here is a short scenario of both calls:

C#
+++++++++++++++++++
[DllImport("test_dll.dll")]
public static extern double Test(
ref double[] val, int len, int len_ret,
[MarshalAs(UnmanagedType.LPStr)] string fName,
ref double[] retval);

The call of dll function:
result=Test(ref myarray, myarray_dim, retarray_dim,
"myfile.txt", ref retarray);

++++++++++++++++++

The C++ prototype
==================
double __declspec(dllexport) CALLBACK Test(double**, int,
int, LPSTR, double**);

==================
In C++ function I want to compute some values for retarray
(the second array of doubles). The
values are computed correctly. The problem is when the
function returns.

I have tried with one array (to store the computed values
over the original ones, which
I don't need any more) and with both arrays. In either case,
both arrays are truncated to
the first element (for the computed array, only the first
element, which is computed
correctly). If I don't change any value in these arrays,
everything works fine.
The dimension of both arrays are known and are sent as
parameters to C++ function.
The storage for both arrays is done in C#, so C++ function
only computes the values
for one array.

Regards,
Mircea


Don't need to pass array's by ref, array's are reference
types, the value passed is a reference (reference is passed
by value) to the array so the callee can change the array
elements.

Change your code to something like this:



C#
[DllImport("test_dll.dll")]
private static extern double func_c( double[] val, int
len,
[MarshalAs(UnmanagedType.LPStr)] string fName
);


static void Main()
{
double[] a1 = new double[10]{1.30, 2.0, 3.0, 4.0, 5.0,
6.0, 7.0, 8.0, 9.0, 10.0};
Console.WriteLine(func_c(a1, a1.Length, "Teststring"));
foreach(double d in a1)
{
Console.WriteLine(d);
}

}



Here I use the array syntax "double d[]" as argument instead
of pointer syntax "double* d", that way it's possible to use
array indexing in the code.

extern "C" {
__declspec(dllexport) double __stdcall func_c (double
val[], int len, char* s);

}

double __stdcall func_c (double d[], int len, wchar_t* s)
{
double ret = 0.0;
printf_s("Len %d\n", len);
for (int n = 0; n < len; n++)
{
ret += val[n];
val[n] *= 0.345; // change the value in array element
}
return ret;
}



Willy.

Willy,

Thank you, it works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top