unmanaged pointer to managed array

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

    #1

    unmanaged pointer to managed array

    I am new to VS.C++ and am having a hard time when i have to share data
    between managed and unmanaged code.
    For example, I need to read binary data from a file and then call an non-CLR
    dll function passing the data that was read as a void*
    below is a snippet of the code that i have written in an attempt to do this.
    I tried __pin but that is no longer supported in VS.2005.

    is there a better way than reading into the array as I did?
    How do you do this?
    thanks for your help
    m


    struct UnmanagedValues // create a struct to hold all nonManaged values.
    {
    void * buffer;
    } unManagedValues ;
    array<wchar_t>^ charBuffer = r->ReadChars(buff erSize); // read the data
    into a managed array.
    unManagedValues .buffer = &charBuffer; //but now I need to set the address of
    the array as a void*. of course this line generates an error



  • William DePalo [MVP VC++]

    #2
    Re: unmanaged pointer to managed array

    "MR" <comconix@newsg roup.nospam> wrote in message
    news:uzLv30okFH A.3316@TK2MSFTN GP14.phx.gbl...[color=blue]
    >I am new to VS.C++ and am having a hard time when i have to share data
    > between managed and unmanaged code.[/color]

    You can use the wrapper template gcroot that appears in <vcclr.h>.
    Essentially it gets you a handle to an allocation on the garbage collected
    heap, and through some template magic allows you to "use" the handle as a
    pointer in native code.

    Details here:

    http://msdn.microsoft.com/library/de...sSpec_16_3.asp

    http://msdn.microsoft.com/msdnmag/issues/05/04/C/

    Regards,
    Will


    Comment

    • adebaene@club-internet.fr

      #3
      Re: unmanaged pointer to managed array



      MR a écrit :[color=blue]
      > I am new to VS.C++ and am having a hard time when i have to share data
      > between managed and unmanaged code.
      > For example, I need to read binary data from a file and then call an non-CLR
      > dll function passing the data that was read as a void*
      > below is a snippet of the code that i have written in an attempt to do this.
      > I tried __pin but that is no longer supported in VS.2005.[/color]

      You should use the new equivalent : cli::pin_ptr

      [color=blue]
      >
      > is there a better way than reading into the array as I did?
      > How do you do this?
      > thanks for your help
      > m
      >
      >
      > struct UnmanagedValues // create a struct to hold all nonManaged values.
      > {
      > void * buffer;
      > } unManagedValues ;
      > array<wchar_t>^ charBuffer = r->ReadChars(buff erSize); // read the data
      > into a managed array.
      > unManagedValues .buffer = &charBuffer; //but now I need to set the address of
      > the array as a void*. of course this line generates an error[/color]

      use :
      cli::pin_ptr<wc har_t> ptr= &charBuffer[0];
      void* ptr2=ptr;
      unManagedValues .buffer=ptr2;

      Arnaud
      MVP - VC

      Comment

      • Norman Diamond

        #4
        Re: unmanaged pointer to managed array

        See:


        and a few other postings before and after that.

        "MR" <comconix@newsg roup.nospam> wrote in message
        news:uzLv30okFH A.3316@TK2MSFTN GP14.phx.gbl...[color=blue]
        >I am new to VS.C++ and am having a hard time when i have to share data
        > between managed and unmanaged code.
        > For example, I need to read binary data from a file and then call an
        > non-CLR
        > dll function passing the data that was read as a void*
        > below is a snippet of the code that i have written in an attempt to do
        > this.
        > I tried __pin but that is no longer supported in VS.2005.
        >
        > is there a better way than reading into the array as I did?
        > How do you do this?
        > thanks for your help
        > m
        >
        >
        > struct UnmanagedValues // create a struct to hold all nonManaged values.
        > {
        > void * buffer;
        > } unManagedValues ;
        > array<wchar_t>^ charBuffer = r->ReadChars(buff erSize); // read the data
        > into a managed array.
        > unManagedValues .buffer = &charBuffer; //but now I need to set the address
        > of
        > the array as a void*. of course this line generates an error
        >
        >
        >[/color]

        Comment

        • Nishant Sivakumar

          #5
          Re: unmanaged pointer to managed array

          See : http://www.voidnish.com/articles/Sho...e=cppcliarrays

          There's a section titled : Direct manipulation of CLI arrays using native
          pointers.

          It may help.

          --
          Regards,
          Nish [VC++ MVP]
          Nish’s thoughts on technology leadership, cloud architecture, and APIs, among other things




          "MR" <comconix@newsg roup.nospam> wrote in message
          news:uzLv30okFH A.3316@TK2MSFTN GP14.phx.gbl...[color=blue]
          >I am new to VS.C++ and am having a hard time when i have to share data
          > between managed and unmanaged code.
          > For example, I need to read binary data from a file and then call an
          > non-CLR
          > dll function passing the data that was read as a void*
          > below is a snippet of the code that i have written in an attempt to do
          > this.
          > I tried __pin but that is no longer supported in VS.2005.
          >
          > is there a better way than reading into the array as I did?
          > How do you do this?
          > thanks for your help
          > m
          >
          >
          > struct UnmanagedValues // create a struct to hold all nonManaged values.
          > {
          > void * buffer;
          > } unManagedValues ;
          > array<wchar_t>^ charBuffer = r->ReadChars(buff erSize); // read the data
          > into a managed array.
          > unManagedValues .buffer = &charBuffer; //but now I need to set the address
          > of
          > the array as a void*. of course this line generates an error
          >
          >
          >[/color]


          Comment

          Working...