开发者

How to Convert IntPtr to native c++ object

I have COM dll t开发者_JS百科hat 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


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.


IntPtr has a ToPointer method that returns a void*. Call this method, then use reintepret_cast to cast this pointer to the right native type.


I would like to modify Hans Passant's answer,
IntPtr directly returns void pointer .. which you can cast easily in any type of native C++ Pointer.

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

here .ToPointer() will return void pointer, now you can cast to your custom pointer type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜