passing array of doubles
-
Hi, I have an ActivX chart control with a method AddPoints(). The IDL looks like this...
[id(40)] VARIANT_BOOL AddPoints(BSTR strPlotName, ULONG nVals, DOUBLE* pdXVals, DOUBLE* pdYVals);
In the C# wrapper generated by DevStudio it looks like this...
public virtual bool AddPoints(string strPlotName, uint nVals, ref double pdXVals, ref double pdYVals);
Question is; how do I pass the double arrays to this method from C#? Thanks.
“If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford
-
Hi, I have an ActivX chart control with a method AddPoints(). The IDL looks like this...
[id(40)] VARIANT_BOOL AddPoints(BSTR strPlotName, ULONG nVals, DOUBLE* pdXVals, DOUBLE* pdYVals);
In the C# wrapper generated by DevStudio it looks like this...
public virtual bool AddPoints(string strPlotName, uint nVals, ref double pdXVals, ref double pdYVals);
Question is; how do I pass the double arrays to this method from C#? Thanks.
“If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford
-
Hi, I have an ActivX chart control with a method AddPoints(). The IDL looks like this...
[id(40)] VARIANT_BOOL AddPoints(BSTR strPlotName, ULONG nVals, DOUBLE* pdXVals, DOUBLE* pdYVals);
In the C# wrapper generated by DevStudio it looks like this...
public virtual bool AddPoints(string strPlotName, uint nVals, ref double pdXVals, ref double pdYVals);
Question is; how do I pass the double arrays to this method from C#? Thanks.
“If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford
I Suspect BobJanova is correct, but I just did some messing around with unsafe code and there is a possability that even though the wrapper is taking a double ref, it may be treating it as a pointer. Check this code out
static void Main(string\[\] args) { double\[\] testArr = new double\[\] { 1.0, 2.0, 3.0 }; for (int i = 0; i < 3; i++) Console.WriteLine(testArr\[i\]); test(ref testArr\[0\]); for (int i = 0; i < 3; i++) Console.WriteLine(testArr\[i\]); } public static unsafe void test(ref double x) { fixed (double\* ptr = &x) { ptr\[1\] = 2.5; } }
this produces the following instead of an error.
1
2
3
1
2.5
3so there is a chance that simply passing a reference to the 0th index in the array might get what you want done. I don't have the wrapper setup to test it out myself.
-
I Suspect BobJanova is correct, but I just did some messing around with unsafe code and there is a possability that even though the wrapper is taking a double ref, it may be treating it as a pointer. Check this code out
static void Main(string\[\] args) { double\[\] testArr = new double\[\] { 1.0, 2.0, 3.0 }; for (int i = 0; i < 3; i++) Console.WriteLine(testArr\[i\]); test(ref testArr\[0\]); for (int i = 0; i < 3; i++) Console.WriteLine(testArr\[i\]); } public static unsafe void test(ref double x) { fixed (double\* ptr = &x) { ptr\[1\] = 2.5; } }
this produces the following instead of an error.
1
2
3
1
2.5
3so there is a chance that simply passing a reference to the 0th index in the array might get what you want done. I don't have the wrapper setup to test it out myself.
-
Hi, I have an ActivX chart control with a method AddPoints(). The IDL looks like this...
[id(40)] VARIANT_BOOL AddPoints(BSTR strPlotName, ULONG nVals, DOUBLE* pdXVals, DOUBLE* pdYVals);
In the C# wrapper generated by DevStudio it looks like this...
public virtual bool AddPoints(string strPlotName, uint nVals, ref double pdXVals, ref double pdYVals);
Question is; how do I pass the double arrays to this method from C#? Thanks.
“If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford
Thanks for the replies, Passing a ref to the first element compiles ok but i seem to be getting garbage values at the other end (in the ActiveX control). But i have not given up yet.
“If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford
-
Thanks for the replies, Passing a ref to the first element compiles ok but i seem to be getting garbage values at the other end (in the ActiveX control). But i have not given up yet.
“If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford
I've found sometimes the only way to reliably do stuff like this is to use
System.Runtime.InteropServices.GCHandle
s.double[] pdXVals = new double[] { 1, 2, 3 };
double[] pdYVals = new double[] { 4, 5, 6 };
GCHandle handleX = GCHandle.Alloc(pdXVals, GCHandleType.Pinned);
GCHandle handleY = GCHandle.Alloc(pdYVals, GCHandleType.Pinned);
// change your wrapper function to GCHandle pdXVals, GCHandle pdYVals
// and pass handleX, handleY// alternatively, the function can take an IntPtr and you can pass handleX.AddrOfPinnedObject() etc
// Important! after function call, when done with the handles
handleX.Free();
handleY.Free();I'm assuming there is a fixed size for the arrays? If not, as there is no size parameter there is no way for the function to know when to stop reading memory and you will get garbage data and/or exceptions.
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Hi, I have an ActivX chart control with a method AddPoints(). The IDL looks like this...
[id(40)] VARIANT_BOOL AddPoints(BSTR strPlotName, ULONG nVals, DOUBLE* pdXVals, DOUBLE* pdYVals);
In the C# wrapper generated by DevStudio it looks like this...
public virtual bool AddPoints(string strPlotName, uint nVals, ref double pdXVals, ref double pdYVals);
Question is; how do I pass the double arrays to this method from C#? Thanks.
“If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford
You might already have seen this[^], which may help you to resolve your problem.
Henry Minute Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” I wouldn't let CG touch my Abacus! When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is. Cogito ergo thumb - Sucking my thumb helps me to think.