473,395 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C# and array in a dll

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
Nov 16 '05 #1
5 2246
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?

Nov 16 '05 #2
Mircea Dragan wrote:
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
Nov 16 '05 #3
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

Nov 16 '05 #4

"Mircea Dragan" <mi***********@scch.at> wrote in message news:41***************@scch.at...
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.

Nov 16 '05 #5
"Willy Denoyette [MVP]" wrote:


"Mircea Dragan" <mi***********@scch.at> wrote in message
news:41***************@scch.at...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.


Willy,

Thank you, it works.

--
Best regards,

Mircea

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.