title: Weird values from WM_INPUT, casting mistake?
body: <h1>About</h1>
  
  <p>I am trying to get the raw mouse input of the system into a C# application. I am using <code>WM_INPUT</code> (<a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ee418864(v=vs.85).aspx" rel="nofollow noreferrer">MSDN link</a>) to get access to the data. To do so I am using the user32.dll. This (<a href="https://stackoverflow.com/questions/27866645/registering-a-touch-screen-hid-with-registerrawinputdevices-does-not-work">stackoverflow link</a>) helped me alot with writing the API Wrapper.</p>
  
  <h1>Current approach</h1>
  
  <p>The following snippet shows the <code>WndProc</code> method. <code>rimTypeMouseCount</code> already shows that the windows messages seem to get fetched correctly. When I turn the polling rate of my mouse up or down I can see this also by checking the count - Lower polling rate means a lower count, higher polling rate means a higher count while logging for equal time intervals. That's good so far.</p>
  
  <h3>WndProc callback method:</h3>
  
  <pre class="lang-cs prettyprint-override"><code>protected override void WndProc(ref Message m)
  {
      if (m.Msg == 0x00ff) {
          uint dwSize = 40;
  
          byte[] raw = new byte[40];
          IntPtr rawPtr = Marshal.AllocHGlobal(raw.Length);
  
          APIWrapper.GetRawInputData((IntPtr)m.LParam, 0x10000003, rawPtr, ref dwSize, Marshal.SizeOf(new APIWrapper.RAWINPUTHEADER()));
  
          Marshal.Copy(rawPtr, raw, 0, 40);
  
          GCHandle handle = GCHandle.Alloc(raw, GCHandleType.Pinned);
          APIWrapper.RAWINPUT rawData = (APIWrapper.RAWINPUT)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(APIWrapper.RAWINPUT));
          handle.Free();
  
          if (rawData.header.dwType == 0)
          {
              rimTypeMouseCount++;
              logMouseX = logMouseX + rawData.mouse.lLastX.ToString() + Environment.NewLine;
              logMouseY = logMouseY + rawData.mouse.lLastY.ToString() + Environment.NewLine;
          }
      }
  
      base.WndProc(ref m);
  }
  </code></pre>
  
  <h3>RAWINPUT struct:</h3>
  
  <pre class="lang-cs prettyprint-override"><code>[StructLayout(LayoutKind.Explicit)]
      internal struct RAWINPUT
      {
          [FieldOffset(0)]
          public RAWINPUTHEADER header;
  
          [FieldOffset(16 + 8)]
          public RAWMOUSE mouse;
  
          [FieldOffset(16 + 8)]
          public RAWKEYBOARD keyboard;
  
          [FieldOffset(16 + 8)]
          public RAWHID hid;
      }
  </code></pre>
  
  <h1>Problem</h1>
  
  <p>But there is a problem with <code>logMouseX</code> and <code>logMouseY</code>. Those to strings simply append the mouse deltas of the x and y axis with every call. Unfortunately <code>logMouseX</code> only logs the values 0 or 65535 while <code>logMouseY</code> logs 0 only. I also tried to log messages like left mouse button up/down and so on. Nothing seems to work actually. For me it seems a little that the numbers might be random or something like that.</p>
  
  <h1>Question</h1>
  
  <p>This is what I (think I) do:</p>
  
  <ol>
  <li><code>APIWrapper.GetRawInputData(...)</code> needs a pointer to a byte array. So I simply allocated space in unmanaged memory with marshalling and got a pointer as a result.</li>
  <li>I am calling the function that fills the array the pointer is pointing at.</li>
  <li>I am taking the pointer that points to the filled array and copy its content into an actual byte array.</li>
  <li>To access the fields of the struct <code>RAWINPUT</code> I need to cast the byte array to a <code>RAWINPUT</code> struct.</li>
  <li>I do this with the three lines that use a GCHandle. I actually found that snippet somewhere but never worked with that class before.</li>
  </ol>
  
  <p>Might it be that I am messing up something there? Maybe something like a Big-Endian and Little-Endian mix up?</p>
  
  <h1>Edits</h1>
  
  <ul>
  <li><p>I found out that the values I get when using <code>rawData.mouse.lLastX</code> &amp; <code>rawData.mouse.lLastY</code> behave like this:</p>
  
  <ul>
  <li>X is zero when moving the mouse downwards and 65535 when moving upwards</li>
  <li>Y is zero always no matter if I move the mouse left or right</li>
  </ul></li>
  <li><p>I found out that using <code>rawDataL.hid.pbRawData</code> includes more information about the movement. There I get values in dependency of how fast I am moving the mouse. Unfortunately there is no X or Y component so this only works for one axis (downwards/upwards).</p></li>
  <li>I found a simpler way of casts. Unfortunately I still get the stranges values as described above. It looks like this now:</li>
  </ul>
  
  <h3>Casting part of WndProc:</h3>
  
  <pre class="lang-cs prettyprint-override"><code>uint dwSize = 40;
  
  byte[] raw = new byte[40];
  GCHandle handle = GCHandle.Alloc(raw, GCHandleType.Pinned);
  
  APIWrapper.GetRawInputData((IntPtr)m.LParam, 0x10000003, handle.AddrOfPinnedObject(), ref dwSize, Marshal.SizeOf(new APIWrapper.RAWINPUTHEADER()));
  
  APIWrapper.RAWINPUT rawData = (APIWrapper.RAWINPUT)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(APIWrapper.RAWINPUT));
  
  if (rawData.header.dwType == 0)
  {
      // accessing data
  }
  
  handle.Free();
  </code></pre>