Converting native arrays to managed arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Altman

    Converting native arrays to managed arrays

    Hi all,

    I'm writing C++/CLI code (compiled with /clr) that accepts a native char array
    (and its length) and calls a managed routine that expects an array of
    System::Byte. I was just wondering if there is any magic that I'm missing that
    easily marshals the native array into a managed array, or do I need to write my
    own code to allocate the managed array<and loop through it copying the native
    data into the managed array?

    TIA - Bob


  • Mark Salsbery [MVP]

    #2
    Re: Converting native arrays to managed arrays

    "Bob Altman" <rda@nospam.nos pamwrote in message
    news:ep2BOzXeIH A.4852@TK2MSFTN GP05.phx.gbl...
    Hi all,
    >
    I'm writing C++/CLI code (compiled with /clr) that accepts a native char
    array (and its length) and calls a managed routine that expects an array
    of System::Byte. I was just wondering if there is any magic that I'm
    missing that easily marshals the native array into a managed array, or do
    I need to write my own code to allocate the managed array<and loop
    through it copying the native data into the managed array?
    You can use System::Runtime ::InteropServic es::Marshal::Co py(), which does
    the "loop through it copying " part for you :)

    Mark

    --
    Mark Salsbery
    Microsoft MVP - Visual C++
    >
    TIA - Bob
    >

    Comment

    • Bob Altman

      #3
      Re: Converting native arrays to managed arrays

      You can use System::Runtime ::InteropServic es::Marshal::Co py(), which does the
      "loop through it copying " part for you :)
      >
      Mark
      Thanks Mark!


      Comment

      • Bob Altman

        #4
        Re: Converting native arrays to managed arrays

        "Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nosp amwrote in message
        news:BD264A84-
        You can use System::Runtime ::InteropServic es::Marshal::Co py(), which does the
        "loop through it copying " part for you :)
        I've tried several variations on the following code, but it complains that it
        can't find an overload for Marshal::Copy that it likes. I suspect that it
        doesn't like the boxed handle to an IntPtr that I think I'm trying to feed it
        (by way of the gcnew IntPtr() code). How do I cast a char* to an IntPtr?

        const char* value = <some array of bytes>
        int size = 15;
        array<unsigned char>^ myArray = gcnew array<unsigned char>(size);
        Marshal::Copy(g cnew IntPtr((void*)v alue), myArray, 0, size);


        Comment

        • Mark Salsbery [MVP]

          #5
          Re: Converting native arrays to managed arrays

          "Bob Altman" <rda@nospam.nos pamwrote in message
          news:uRKAeeYeIH A.1164@TK2MSFTN GP02.phx.gbl...
          "Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nosp amwrote in
          message news:BD264A84-
          >
          >You can use System::Runtime ::InteropServic es::Marshal::Co py(), which does
          >the "loop through it copying " part for you :)
          >
          I've tried several variations on the following code, but it complains that
          it can't find an overload for Marshal::Copy that it likes. I suspect that
          it doesn't like the boxed handle to an IntPtr that I think I'm trying to
          feed it (by way of the gcnew IntPtr() code). How do I cast a char* to an
          IntPtr?
          >
          const char* value = <some array of bytes>
          int size = 15;
          array<unsigned char>^ myArray = gcnew array<unsigned char>(size);
          Marshal::Copy(g cnew IntPtr((void*)v alue), myArray, 0, size);
          >
          >
          Maybe try something like this:

          const char *value = new char[100];
          int size = 15;
          array<unsigned char>^ myArray = gcnew array<unsigned char>(size);
          Marshal::Copy(I ntPtr(const_cas t<void*>(static _cast<const void*>(value))) ,
          myArray, 0, size);
          delete[] value;

          Mark

          --
          Mark Salsbery
          Microsoft MVP - Visual C++

          Comment

          • Mark Salsbery [MVP]

            #6
            Re: Converting native arrays to managed arrays

            Also note that in my code sample, it ends up using the

            Marshal::.Copy Method (IntPtr, array<Byte>[]()[], Int32, Int32)

            form of Marshall::Copy( ) since the array is a Byte (unsigned char) array.

            Cheers,
            Mark

            --
            Mark Salsbery
            Microsoft MVP - Visual C++


            "Bob Altman" <rda@nospam.nos pamwrote in message
            news:uRKAeeYeIH A.1164@TK2MSFTN GP02.phx.gbl...
            "Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nosp amwrote in
            message news:BD264A84-
            >
            >You can use System::Runtime ::InteropServic es::Marshal::Co py(), which does
            >the "loop through it copying " part for you :)
            >
            I've tried several variations on the following code, but it complains that
            it can't find an overload for Marshal::Copy that it likes. I suspect that
            it doesn't like the boxed handle to an IntPtr that I think I'm trying to
            feed it (by way of the gcnew IntPtr() code). How do I cast a char* to an
            IntPtr?
            >
            const char* value = <some array of bytes>
            int size = 15;
            array<unsigned char>^ myArray = gcnew array<unsigned char>(size);
            Marshal::Copy(g cnew IntPtr((void*)v alue), myArray, 0, size);
            >
            >

            Comment

            Working...