views:

198

answers:

0

I have some C++/CLI code that needs to pin a struct (value type) in order to pass it back to unmanaged code. The unmanaged code knows enough to unmarshal what it needs.

The struct may contain managed strings and so I also need to pin these before calling the unmanaged function.

The problem is that my managed helper class is generic:

generic<typename T> 
public ref class OutputSetOf
{
public:
   void Insert(T% v)
   {
       cli::pin_ptr<T> pinned = &v;

       cli::interior_ptr<T> ptr = &v;
       void** pptr = (void**) &pptr;

       <Call to UnmanagedFunction>(*pptr);
   }
}

How can I guarantee that any String (or other references) in T are pinned?

I assume the instance of pinned in OutputSetOf::Insert doesn't help, since it only pins v (which is likely on the stack anyway) and not any member fields in it.

Is there a call be propagate the pinning to member functions? I'd even settle for being able to suppress a GC while the unmanaged call is in progress (as long as setting/clearing that flag is very efficient).