Handmade Hero»Forums»Code
Andrew
2 posts
Buffer Overrun on Exit when using Raw Input with Win32 (SOLVED)
Edited by Andrew on Reason: Issue solved
Basically, I get a buffer overrun error when using raw input with win32.

I have a RAWINPUTDEVICE variable and register it at startup,
1
2
3
4
5
6
RAWINPUTDEVICE riMouse;
riMouse.usUsagePage = HID_USAGE_PAGE_GENERIC;
riMouse.usUsage     = HID_USAGE_GENERIC_MOUSE;
riMouse.dwFlags     = RIDEV_INPUTSINK;
riMouse.hwndTarget  = window;
RegisterRawInputDevices(&riMouse, 1, sizeof(riMouse));


And then in my PeekMessage() loop I respond to the WM_INPUT call
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
case WM_INPUT:
{
   if(message.wParam == RIM_INPUT && useRelativeMouse)
       {
          GetRawInputData((HRAWINPUT)message.lParam, RID_INPUT, rawInputData, &dwSize, sizeof(RAWINPUTHEADER));
          rawInput = (RAWINPUT*)rawInputData;
          if(rawInput->header.dwType == RIM_TYPEMOUSE)
          {        
            input.mouseInput.deltaX = rawInput->data.mouse.lLastX;
            input.mouseInput.deltaY = rawInput->data.mouse.lLastY;
          }
       }
}break;


This works for getting the input, however when I exit the program I get
"Stack cookie instrumentation code detected a stack-based buffer overrun."
If I do not call RegisterRawInputDevices() I do not get a crash but obviously the input does not work. I also do not get a crash if I do not move the mouse while running the program.

I tried setting the RIDEV_REMOVE flag on my RAWINPUTDEVICE and calling RegisterRawInputDevices() again, and again this works if I do not move the mouse at all while running.

1
2
3
4
5
    riMouse.dwFlags = RIDEV_REMOVE;
    riMouse.hwndTarget = 0;
    RegisterRawInputDevices(&riMouse, 1, sizeof(riMouse));
    riMouse.hwndTarget = window;
    RegisterRawInputDevices(&riMouse, 1, sizeof(riMouse));


I tried the above with and without the second call of RegisterRawInputDevices() and got the same result with each.

I have read through several examples and I can't find what I am doing wrong.

Thanks for the help.

EDIT *SOLVED*
My rawInputData array was too small I guess, increasing the size worked.
Mārtiņš Možeiko
2562 posts / 2 projects
Buffer Overrun on Exit when using Raw Input with Win32 (SOLVED)
What is rawInputData? Do you have at least dwSize bytes allocated for it?
Andrew
2 posts
Buffer Overrun on Exit when using Raw Input with Win32 (SOLVED)
rawInputData is an array of bytes. I just tried making the array larger and it seems to have fixed the issue. I feel pretty stupid right now but I guess that is when you learn the most.

Thanks for the help!
Mārtiņš Možeiko
2562 posts / 2 projects
Buffer Overrun on Exit when using Raw Input with Win32 (SOLVED)
You can know how big it must be if you call GetRawInputData first with buffer NULL, then it will return required size in dwSize variable.