title: IntPtr.ToInt32() and x64 systems
body: <p>In my c# dll I have some code like this to interact with some unmanaged dlls:</p>
  
  <pre><code>IntPtr buffer = ...;
  TTPOLYGONHEADER header = (TTPOLYGONHEADER)Marshal.PtrToStructure(
                         new IntPtr(buffer.ToInt32() + index), typeof(TTPOLYGONHEADER));
  </code></pre>
  
  <p>This has always worked fine when using my dll compiled in AnyCPU with .Net2 and .Net4 on x64 systems, before installing Windows 8.</p>
  
  <p>With Windows 8 when using the .Net4 dll I get an OverFlowException ("Arithmetic operation resulted in an overflow.") at the buffer.ToInt32() call.</p>
  
  <p>The MSDN documentation for IntPtr.ToInt32() says this:</p>
  
  <p>"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."</p>
  
  <p>I wonder why this problem has surfaced only with Windows 8, and what is the correct way to fix it.</p>
  
  <p>Should I use a method like this, instead of the IntPtr.ToInt32() call?</p>
  
  <pre><code>    internal static long GetPtr(IntPtr ptr)
      {
          if (IntPtr.Size == 4) // x86
  
              return ptr.ToInt32();
  
          return ptr.ToInt64(); // x64
      }
  </code></pre>