Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert IntPtr to native c++ object

I have COM dll that I am using in C++/Cli, one of the method in this COM dll returns IntPtr I want to convert that back to the native object pointer. How I can do that ? please in put

like image 946
Sandeep Avatar asked Nov 25 '10 13:11

Sandeep


1 Answers

IntPtr is an integral type, you need to first convert it to a pointer type:

  IntPtr somePtr;
  ...
  Mumble* fooPtr = (Mumble*)(void*)somePtr;

Or the more readable version:

  Mumble* fooPtr = (Mumble*)somePtr.ToPointer();

The method call will be optimized away at runtime.

like image 104
Hans Passant Avatar answered Oct 11 '22 14:10

Hans Passant