Is it possible to create an instance of native C++ class from C# via P/Invoke ?

What is an instance of native C++ class? It is really just a pointer pointing to the beginning of a memory block of certain size in the native unmanaged world. Okay, that makes sense. If we allocate a memory block of such a size in C# by calling Marshal.AllocHGlobal method and then pass the pointer(IntPtr) to whatever native C++ method in that C++ class, would that work? and How? A block of memory representing a native C++ class instance may not be valid until the block of memory gets initialized by calling the constructor of the native C++ class. Once it gets initialized by the constructor, the native C++ DLL won’t be able to differentiate an instance created from C# or native C++, it is just a block of memory with initialized data with the same constructor method.


← Faqs
Translate »