1

I have problem with GetRawInputBuffer. Code returns no error, but there are no data present in retrieved response.

I have written code according to this Using GetRawInputBuffer correctly

    UINT RawInputSize;
    UINT Result;
    Result = GetRawInputBuffer(NULL, &(RawInputSize), sizeof(RAWINPUTHEADER));
    if (Result == -1)
    {
        DWORD ErrorCode = GetLastError();
        return;
    }

    UINT AllocatedBufferByteCount = RawInputSize * 16;
    RAWINPUT* RawInputBuffer = reinterpret_cast<RAWINPUT*>(malloc(AllocatedBufferByteCount));

    UINT AllocatedBufferByteCountTwo = AllocatedBufferByteCount;
    Result = GetRawInputBuffer(RawInputBuffer, &(AllocatedBufferByteCountTwo), sizeof(RAWINPUTHEADER));
    if (Result == -1)
    {
        DWORD ErrorCode = GetLastError();
        return;
    }

    UINT RawInputCount = Result;

    RAWINPUT* RawInput = RawInputBuffer;
    for (unsigned int i = 0; i < RawInputCount; ++i)
    {
        switch (RawInput->header.dwType)
        {
            case RIM_TYPEMOUSE:
            {
                this->UpdateMouse(RawInput->data.mouse);
                break;
            }
            case RIM_TYPEKEYBOARD:
            {
                this->UpdateKeyboard(RawInput->data.keyboard);
                break;
            }
        }


        RawInput = NEXTRAWINPUTBLOCK(RawInput); 
    }

    DefRawInputProc(&(RawInputBuffer), RawInputCount, sizeof(RAWINPUTHEADER));

This code is called outside case WM_INPUT. RawInputCount is always zero. If I use GetRawInputData inside case WM_INPUT, I am recieving data correctly.

What is wrong with this code and why are my results empty?

Community
  • 1
  • 1
Martin Perry
  • 9,232
  • 8
  • 46
  • 114

1 Answers1

0

The answer comes a bit late, but since I had the same problem recently, I'll answer it anyways: GetRawInputBuffer uses the WM_INPUT message to get and buffer the messages. However, you propably uses something like while(PeekMessage(&Message, NULL, 0, 0, PM_REMOVE)) to process your window messages, which removes all messages after sent to your application. That way, input messages won't be send to the GetRawInputBuffer method and the method will allways return an size of 0. To solve this problem, you need to use something like this as your main loop:

//Process and remove all messages before WM_INPUT
while(PeekMessage(&Message, NULL, 0, WM_INPUT - 1, PM_REMOVE))
{
    TranslateMessage(&Message);
    DispatchMessage(&Message);
}
//Process and remove all messages after WM_INPUT
while(PeekMessage(&Message, NULL, WM_INPUT + 1, (UINT)-1, PM_REMOVE))
{
    TranslateMessage(&Message);
    DispatchMessage(&Message);
}

This way, WM_INPUT messages are neither processed nor removed by your application, and therefor send to GetRawInputBuffer. Obviusly you could also exchange the PM_REMOVE flag with PM_NOREMOVE, however this could cause other problems, so I would recommend the above method to process and remove all but the WM_INPUT message.

Bizzarrus
  • 1,194
  • 6
  • 10