Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. passing array of doubles

passing array of doubles

Scheduled Pinned Locked Moved C#
questioncsharpdata-structures
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    __John_
    wrote on last edited by
    #1

    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

    B K _ H 4 Replies Last reply
    0
    • _ __John_

      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

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      Oh this old chestnut. I did a certain amount of searching around this issue and I couldn't find a way to make the auto generated wrapper convert X* to X[] instead of ref X. You might have to manually tweak the wrapper.

      1 Reply Last reply
      0
      • _ __John_

        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

        K Offline
        K Offline
        kevinnicol
        wrote on last edited by
        #3

        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
        3

        so 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.

        B 1 Reply Last reply
        0
        • K kevinnicol

          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
          3

          so 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.

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          I had the same thought but I'm sure I tried passing a reference to the first element and it wouldn't compile. I wonder what I was doing wrong.

          1 Reply Last reply
          0
          • _ __John_

            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

            _ Offline
            _ Offline
            __John_
            wrote on last edited by
            #5

            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

            D 1 Reply Last reply
            0
            • _ __John_

              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

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              I've found sometimes the only way to reliably do stuff like this is to use System.Runtime.InteropServices.GCHandles.

              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)

              1 Reply Last reply
              0
              • _ __John_

                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

                H Offline
                H Offline
                Henry Minute
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups