user2372247
user2372247

Reputation: 41

cast the native pointer to managed object

I need to convert unmanaged pointer to the managed object. Below is the code

Wrapper.cpp

 _declspec(dllexport) int UnmanagedAlgebraComputation::SubtractMultiplyWrapper(UnmanagedSubtraction *unmanagedSubtraction)
{
     AlgebraCSharp::AlgebraComputation^ obj= AlgebraCSharp::AlgebraComputation::GetObject;
     return obj->Subtraction(unmanagedSubtraction);
 }

Here UnmanagedSubtraction, UnmanagedAlgebraComputation is the class in my Wrapper.h file.

SubtractMultiplyWrapper(UnmanagedSubtraction *unmanagedSubtraction) is the method in my wrapper.h file which is implemented in my Wrapper.cpp file.

The problem is I want to pass the managed object to the obj->Subtraction(unmanagedSubtraction) but unmanagedSubtraction is my native C pointer.

It throws the error as

'AlgebraCSharp::AlgebraComputation::Subtraction' : cannot convert parameter 1 from 'UnmanagedSubtraction *' to 'AlgebraCSharp::SubtractComputation ^'

Please let me know if you have any solution related to this.

Upvotes: 0

Views: 744

Answers (2)

user2372247
user2372247

Reputation: 41

We cant convert Native Pointer to Managed object Directly. First We have to convert Native Pointer to IntPtr. and then IntPtr to Managed object.

Upvotes: 1

Medinoc
Medinoc

Reputation: 6608

You can't cast: You must either convert (copy the relevant contents into a managed object) or wrap (have one object be a wrapper around the other).

Upvotes: 1

Related Questions