See the question and my original answer on StackOverflow

The structure definition is incorrect, it should be

[StructLayout(LayoutKind.Sequential)]
struct COPYDATASTRUCT
{
  public IntPtr dwData; // in C/C++ this is an UINT_PTR, not an UINT
  public int cbData;
  public IntPtr lpData;
}

Your definition of dwData was matching ok in a 32-bit process (4-bytes), but not in a 64-bit process (8 bytes). Since this is the first field in the structure, all bets are off when its definition is incorrect.