marshal an array from managed C++ to unmanaged C++ 
Author Message
 marshal an array from managed C++ to unmanaged C++

I am wrapping a legacy C++ class library using managed C++, and I need to
pass a managed array to unmanaged code.  How do I do this?

This is the unmanaged C++ class that I am wrapping:

namespace UnManaged
{
   class Foo
   {
      public:

         Foo(int array_size, double* array);
   };

Quote:
}

This is the managed C++ class that I am trying to write.  What goes in the
second argument of the constructor?

namespace Managed
{
   class Foo
   {
      public:

         Foo(double array __gc[])
         {
            m_pFoo = new UnManaged::Foo(array -> Length, <what goes
here?> );
         }

      private:

         UnManaged::Foo* m_pFoo;
   };

Quote:
}

Thank you for your help.

Warmest Regards, Matt

--
Matthew D. Langston
SLD, Stanford Linear Accelerator Center



Thu, 29 Jul 2004 13:54:13 GMT  
 marshal an array from managed C++ to unmanaged C++
Matthew,

Quote:
> I am wrapping a legacy C++ class library using managed C++, and I need to
> pass a managed array to unmanaged code.  How do I do this?

The following works for me:

            double __pin* ua = &array[0];
            m_pFoo = new UnManaged::Foo(array -> Length, ua );

--
Tomas Restrepo



Thu, 29 Jul 2004 21:17:23 GMT  
 marshal an array from managed C++ to unmanaged C++
Does the act of using __pin magically turn the managed array into an
unmanaged array?  Is there a copy of the array being created behind the
scenes, i.e. does __pin cause the elements of my managed array (named
"array" in the example below) to be copied into the unmanaged heap such that
the pointer named "ua" points to this copy?  If so, am I responsible for
deleting this memory, or does C++ do the clean up when __pin goes out of
scope?

My read of the .NET docs suggest  that __pin simply makes the object being
pointed to (a managed array in my case) stay at the same place in memory in
the managed heap.  If this is so, and my unmanaged C++ has access to the
array on the managed heap, then can the unmanaged C++ scribble all over the
managed heap's memory using the pointer named "ua"?

Regards, Matt



Quote:
> Matthew,

> > I am wrapping a legacy C++ class library using managed C++, and I need
to
> > pass a managed array to unmanaged code.  How do I do this?

> The following works for me:

>             double __pin* ua = &array[0];
>             m_pFoo = new UnManaged::Foo(array -> Length, ua );

> --
> Tomas Restrepo




Fri, 30 Jul 2004 15:32:52 GMT  
 marshal an array from managed C++ to unmanaged C++
Matthew,

Quote:
> Does the act of using __pin magically turn the managed array into an
> unmanaged array?

No. It merely instructs the .NET memory manager _not_ to move the object
while that variable exist. Thus, it can now be touched safely by unmanaged
code.

Quote:
> Is there a copy of the array being created behind the
> scenes, i.e. does __pin cause the elements of my managed array (named
> "array" in the example below) to be copied into the unmanaged heap such
that
> the pointer named "ua" points to this copy?

Nope, it still points into the managed heap.

Quote:
> If so, am I responsible for
> deleting this memory, or does C++ do the clean up when __pin goes out of
> scope?

Nope, once it goes out of scope, normal GC procedures continue to work as
expected.

Quote:

> My read of the .NET docs suggest  that __pin simply makes the object being
> pointed to (a managed array in my case) stay at the same place in memory
in
> the managed heap.  If this is so, and my unmanaged C++ has access to the
> array on the managed heap, then can the unmanaged C++ scribble all over
the
> managed heap's memory using the pointer named "ua"?

Well, certainly. That's the risk you run.

--
Tomas Restrepo



Fri, 30 Jul 2004 19:45:58 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. return an array from unmanaged C++ to managed C++

2. managed C++ wrapper around unmanaged C++ classes: causing StackOverflow exception

3. Performance of unmanaged C++ in a managed C++ app

4. Fatal Error C1010 in Mixing Managed C++ and Unmanaged C++ Code

5. Inherit unmanaged c++ classes from .Net platform (managed c++ or c#)

6. C# client crashs when calling into Managed C++ which calls unmanaged c++ function

7. managed c++ vs unmanaged c++

8. How to pass a function pointer from Managed C++ to unmanaged c++

9. Managed C++ and Unmanaged C++ and Inheritance

10. Interoperability UnManaged C++, Managed C++, C#

11. How to call managed C++ DLL from unmanaged C++ EXE

12. debugging unmanaged c++ from a managed c++ class library

 

 
Powered by phpBB® Forum Software