Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can an UIntPtr object be converted to IntPtr in C#?

Tags:

c#

object

casting

I need to convert an UIntPtr object to that of IntPtr in my C# .NET 2.0 application. How can this be accomplished? I don't suppose it's as simple as this:

UIntPtr _myUIntPtr = /* Some initializer value. */
object _myObject = (object)_myUIntPtr;
IntPtr _myIntPtr = (IntPtr)_myObject;
like image 218
Jim Fell Avatar asked Sep 21 '10 15:09

Jim Fell


1 Answers

This should work on x86 and x64

IntPtr intPtr = unchecked((IntPtr)(long)(ulong)uintPtr);
like image 56
JaredPar Avatar answered Sep 28 '22 00:09

JaredPar