This should work on 32-bit operating systems:
IntPtr intPtr = (IntPtr)(int)(uint)uintPtr;
That is, turn UIntPtr into uint, turn it into int, and then turn it into IntPtr.
It's good that jitter optimizes all conversions and just turns it into a direct assignment of one value to another, but I haven't really tested it.
See the Jared answer for a solution that runs on 64-bit operating systems.
Eric Lippert
source share