views:

12

answers:

0

How is it possible to marshal a shared_ptr from unmanaged C++ code into C#?

I have a function in C++ (exposed through C bindings) that will allocate a new MyObject and return a pointer to it via a shared_ptr.

In C++ I can call the function:

MyObjHandle* ref = NULL:
GetMyObject(&ref); // double ptr...

where MyObjHandle is defined:

typedef struct obj_t {

   shared_ptr<MyObject> objPtr;

} MyObjHandle ;

How would I PInvoke the call to GetMyObject from C#? I did the obvious of just defining the structure MyObjectHandle pretty much as is in C# expcept I defined the member objPtr as an IntPtr. That failed miserably with a AccessViolationException error. Tried a couple other PInvoke variations which also failed. I can't seem to find any interop samples with shared_ptr and C#. Any help appreciated...