C# Developers' Journal (original) (raw)

5:47p

marshalling abstract pointers In C++, you can return pointers to base types to be casted into derived types safely, even if said base type is abstract.

However, there is a slight problem when you need to convert that abstract pointer into something usable in C#. Here's the rundown of what I think is happening.

Firstly, we have an unmanaged function returning a pointer to an abstract base class. That pointer, and its contents, exist on the unmanaged heap. Next, I use Marshal::PtrToStructure, attempting to cast the pointer into the base type of the corresponding managed hierarchy. The PtrToStructure method tries to create a new object on the managed stack, and throws an exception indicating that instantiating an abstract class is right out.

How can I get this pointer converted to a reference that I can use?

DC