In my c# dll I have some code like this to interact with some unmanaged dlls:
IntPtr buffer = ...;
TTPOLYGONHEADER header = (TTPOLYGONHEADER)Marshal.PtrToStructure(
new IntPtr(buffer.ToInt32() + index), typeof(TTPOLYGONHEADER));
This has always worked fine when using my dll compiled in AnyCPU with .Net2 and .Net4 on x64 systems, before installing Windows 8.
With Windows 8 when using the .Net4 dll I get an OverFlowException ("Arithmetic operation resulted in an overflow.") at the buffer.ToInt32() call.
The MSDN documentation for IntPtr.ToInt32() says this:
"OverflowException: On a 64-bit platform, the value of this instance is too large or too small to represent as a 32-bit signed integer."
I wonder why this problem has surfaced only with Windows 8, and what is the correct way to fix it.
Should I use a method like this, instead of the IntPtr.ToInt32() call?
internal static long GetPtr(IntPtr ptr)
{
if (IntPtr.Size == 4) // x86
return ptr.ToInt32();
return ptr.ToInt64(); // x64
}