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 by reference from C# to c++/cli

Passing array by reference from C# to c++/cli

Scheduled Pinned Locked Moved C#
csharpc++data-structures
6 Posts 4 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.
  • E Offline
    E Offline
    eyalbi007
    wrote on last edited by
    #1

    Hi, I'm trying to write a function in a c++/cli dll that will receive an empty array from C# (float[] a), use gcnew to allocate the array and then I need to use the array in C#. I tried regular transfer but after the function returns the array is still null in C#. Current trial is: C#:

    float[] a;
    InitArray(a);

    c++/cli:

    void CLIClass::InitArray(array^ a)
    {
    a = gcnew array(100);
    for(int i=0; i<100; i++)
    a[i] = 10.0F;
    }

    I do not wish to return the array as a return value (the function should actually return 3 arrays). Thanks!

    M L 2 Replies Last reply
    0
    • E eyalbi007

      Hi, I'm trying to write a function in a c++/cli dll that will receive an empty array from C# (float[] a), use gcnew to allocate the array and then I need to use the array in C#. I tried regular transfer but after the function returns the array is still null in C#. Current trial is: C#:

      float[] a;
      InitArray(a);

      c++/cli:

      void CLIClass::InitArray(array^ a)
      {
      a = gcnew array(100);
      for(int i=0; i<100; i++)
      a[i] = 10.0F;
      }

      I do not wish to return the array as a return value (the function should actually return 3 arrays). Thanks!

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Does passing a tracking reference work? void CLIClass::InitArray(array^% a)

      Mark Salsbery :java:

      E N 2 Replies Last reply
      0
      • E eyalbi007

        Hi, I'm trying to write a function in a c++/cli dll that will receive an empty array from C# (float[] a), use gcnew to allocate the array and then I need to use the array in C#. I tried regular transfer but after the function returns the array is still null in C#. Current trial is: C#:

        float[] a;
        InitArray(a);

        c++/cli:

        void CLIClass::InitArray(array^ a)
        {
        a = gcnew array(100);
        for(int i=0; i<100; i++)
        a[i] = 10.0F;
        }

        I do not wish to return the array as a return value (the function should actually return 3 arrays). Thanks!

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        your C# code should use the out or ref keyword:

        float[] a;
        InitArray(out a);

        float[] a=null;
        InitArray(ref a);

        ref would fail here as long as a isn't initialized. Without ref/out your callee can modify the array content, however it can't replace the array. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        1 Reply Last reply
        0
        • M Mark Salsbery

          Does passing a tracking reference work? void CLIClass::InitArray(array^% a)

          Mark Salsbery :java:

          E Offline
          E Offline
          eyalbi007
          wrote on last edited by
          #4

          Yes, all I was missing is the % operator. Thanks!

          N 1 Reply Last reply
          0
          • E eyalbi007

            Yes, all I was missing is the % operator. Thanks!

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #5

            I gave him a 5. It's nice when you do that when people help you out here.

            Regards, Nish


            My technology blog: voidnish.wordpress.com You've gotta read this : Using lambdas - C++ vs. C# vs. C++/CX vs. C++/CLI

            1 Reply Last reply
            0
            • M Mark Salsbery

              Does passing a tracking reference work? void CLIClass::InitArray(array^% a)

              Mark Salsbery :java:

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #6

              5!

              Regards, Nish


              My technology blog: voidnish.wordpress.com You've gotta read this : Using lambdas - C++ vs. C# vs. C++/CX vs. C++/CLI

              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