How can I send a managed object to native function to use it?
void managed_function()
{
Object^ obj = gcnew Object();
void* ptr = obj ??? // How to convert Managed object to void*?
unmanaged_function(ptr);
}
// The parameter type should be void* and I can not change the type.
// This function is native but it uses managed object. Because type of ptr could not be
// Object^ I called it "Unmanaged Function".
void unmanaged_function(void* ptr)
{
Object^ obj = ptr ??? // How to convert void* to Managed object?
obj->SomeManagedMethods();
}
After googling, reading MSDN and try some codes, I found this method to pass a managed object to an unmanaged function.
These methods show how to convert Object^ to void* and convert void* to Object^.
Note: if “unmanaged_function” has variable arguments, this method won’t work.