title: P/Invoke changes the values of passed arguments
body: <p>So I have this c++ function in a DLL</p>
  
  <pre><code>__declspec(dllexport) MOUSERAWDATA __stdcall GetMouseRawData(LPARAM lParam)
  {
      UINT bufferSize = 0;
      BYTE *buffer = new BYTE[bufferSize];
      GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &amp;bufferSize, sizeof(RAWINPUTHEADER));
      GetRawInputData((HRAWINPUT)lParam, RID_INPUT, (LPVOID)buffer, &amp;bufferSize, sizeof(RAWINPUTHEADER));
      RAWINPUT *raw = (RAWINPUT*)buffer;
      MOUSERAWDATA data;
      if (raw-&gt;header.dwType == RIM_TYPEMOUSE)
      {
          data.longX = raw-&gt;data.mouse.lLastX;
          data.longY = raw-&gt;data.mouse.lLastY;
      }
      return data;
  }
  </code></pre>
  
  <p>And the following is in my c# project:</p>
  
  <pre><code>[DllImport("RawInput.dll", CallingConvention = CallingConvention.StdCall)]
  private static extern MouseRawData GetMouseRawData(IntPtr lParam);
  .
  .
  .
  protected override void WndProc(ref Message m)
  {
      switch(m.Msg)
      {
          case WM_CREATE:
              if (AttachMouseListener(this.Handle))
                  Console.WriteLine("It works!");
              break;
          case WM_INPUT:
              MouseRawData data = GetMouseRawData(m.LParam);
              break;
          default:
              base.WndProc(ref m);
              break;
       }
  }
  </code></pre>
  
  <p>When I run this code, the following value is passed to the GetMouseRawData</p>
  
  <p><a href="https://i.stack.imgur.com/SNiz9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SNiz9.png" alt="The value being passed"></a></p>
  
  <p>But for some reason the value of lParam at the C++ side is always different.</p>
  
  <p><a href="https://i.stack.imgur.com/KSSTa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KSSTa.png" alt="enter image description here"></a></p>
  
  <p>I this case 0x004fe95c = 5237084</p>
  
  <p>Anyone knows why this happens?</p>