Search code examples
c++winapi64-bituser32

GetRawInputData not working for x64 build


GetRawInputData works fine with x86 build, but not with x64.

UINT32 dwSize = 40;
static BYTE lpb[40];
GetRawInputData((HRAWINPUT)lparam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER));

I'm using it to get mouse input. In x86 build I get proper values, but on x64 it returns 0 for lLastX and lLastY.

RAWINPUT* raw = (RAWINPUT*)lpb;

if (raw->header.dwType == RIM_TYPEMOUSE)
{
  int xPosRelative = raw->data.mouse.lLastX;
  int yPosRelative = raw->data.mouse.lLastY;
  ...

I'm pretty much using the code from the Microsoft website, so I'm not sure where to go from this point. Google didn't help. Thanks!


Solution

  • dwSize should be set to the size of the buffer. The buffer appears to be of different sizes on the two targets (which is to be expected as integer sizes vary, for example).

    In the below example I pick 65,000 arbitrarily, but you'll need to work out the correct value for your application.

    dwSize = 65000;
    BYTE * lpb = new BYTE[dwSize];
    GetRawInputData((HRAWINPUT)lparam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER));