title: Simulating Raw Input on Windows
body: <p>I have an application that does some complicated things with raw input and I would like to have some automated tests for it. This is the code that sends input albeit chopped down a bit to make it easier to follow.</p>
  
  <p>Sending:</p>
  
  <pre><code>GetRawInputDeviceList(nullptr, &amp;numDevices, sizeof(RAWINPUTDEVICELIST));
  rawInputDeviceList = new RAWINPUTDEVICELIST[numDevices];
  GetRawInputDeviceList(rawInputDeviceList, &amp;numDevices, sizeof(RAWINPUTDEVICELIST));
  
  HGLOBAL hRaw = ::GlobalAlloc(GHND, sizeof(RAWINPUT));
  RAWINPUT *pRaw = reinterpret_cast&lt;RAWINPUT*&gt;(::GlobalLock(hRaw));
  pRaw-&gt;data.keyboard.MakeCode = 30;
  pRaw-&gt;data.keyboard.Flags = 0;
  pRaw-&gt;data.keyboard.Reserved = 0;
  pRaw-&gt;data.keyboard.VKey = 0x43;
  pRaw-&gt;data.keyboard.Message = WM_KEYDOWN;
  pRaw-&gt;data.keyboard.ExtraInformation = 0;
  pRaw-&gt;header.dwType = RIM_TYPEKEYBOARD;
  pRaw-&gt;header.dwSize = sizeof(RAWINPUT);
  pRaw-&gt;header.hDevice = rawInputDeviceList[0].hDevice;
  pRaw-&gt;header.wParam = (WPARAM)RIM_INPUT;
  ::GlobalUnlock(hRaw);
  
  SendMessage(hWnd_, WM_INPUT, (WPARAM)RIM_INPUT, (LPARAM)hRaw);
  </code></pre>
  
  <p>Receiving:</p>
  
  <pre><code>case WM_INPUT:
    UINT size = 0;  
    if(GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &amp;size, sizeof(RAWINPUTHEADER)))
      return GetLastError();
  </code></pre>
  
  <p>The return value from GetLastError() is ERROR_INVALID_HANDLE(6). However I can read the handle fine if I use GlobalLock it myself.</p>
  
  <p>According to <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms645596(v=vs.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/desktop/ms645596(v=vs.85).aspx</a>
    "lParam: A handle to the RAWINPUT structure. This comes from the lParam in WM_INPUT."</p>
  
  <p>I've tried just using a pointer and I get the same error message. I've tried with/without unlocking, as well as using LocalAlloc/LocalLock instead. I've tried with PostMessage and it's all the same result, and I am now out of ideas.</p>