Home > database >  WINAPI GetRawInputData gives an error because of wrong parameters [closed]
WINAPI GetRawInputData gives an error because of wrong parameters [closed]

Time:09-16

GetRawInputData() returns -1 (error) and GetLastError() returns 87 which is "The parameter is incorrect.", the first call to the function to get the data size succeeds but the second one, where I try to actually get the data, fails.

UINT DataSize;
if (GetRawInputData((HRAWINPUT)Message.lParam, RID_INPUT, NULL, &DataSize, sizeof(RAWINPUTHEADER)) == -1)
{
    Error("Failed getting raw input amaount\n");
}
RAWINPUT *Raw;
Raw = (PRAWINPUT)VirtualAlloc(NULL, DataSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!Raw)
{
    Error("Failed to allocate memory!\n");
}
if (GetRawInputData((HRAWINPUT)Message.lParam, RID_INPUT, Raw, &DataSize, sizeof(RAWINPUTHEADER) == -1))  // <-- Fails here
{
    Error("Failed getting raw input\n");
}

CodePudding user response:

As @dialer mentioned in a comment, the closing parenthesis are wrong in your 2nd call to GetRawInputData():

if (GetRawInputData(..., sizeof(RAWINPUTHEADER) == -1))

You are comparing the result of sizeof() to -1, and then passing the result of that comparison (0 or 1) to the cbSizeHeader parameter (and then checking whether GetRawInputData() returns a non-zero return value), hence the "invalid parameter" error.

Which BTW, GetRawInputDaata() is NOT documented as using GetLastError() for error reporting, so you can't rely on that error code anyway when GetRawInputDaata() returns -1.

Change your if statement to close like this instead:

if (GetRawInputData(..., sizeof(RAWINPUTHEADER)) == -1)
  • Related