I am a novice programmer trying to create the following application:

1. Can just be a simple CLI program.

2. Gets mouse input from WM_Input, not for buttons just whatever the mouse sends for left/right/up/down.

3. When a mouse event is received a timer starts.

4. If there has not been a mouse event for x amount of milliseconds, stop the timer, report the time elapsed with the idle time subtracted along with the counts that were received during the time interval.

5. Program then idles and waits for another mouse event. Perhaps it can wait on some key to quit (although I could just ctrl+c).

I think I can figure out the timer part (#include <ctime>?). I should be able to figure out the printing of the x and y variables provided I can get it to 'live update'.

The WM_INPUT stuff is over my head though. I tried using the MS example, but it says "case WM_INPUT" which would need a switch would it not? I just did if WM_INPUT whatever it equals so that control could look through the rest of the code.

It compiles but doesn't run without crashing. Running it I get a runtime error "application has requested the Runtime to terminate it in an unusual way".

If I put init() in main I get an init failed with invalid flag (getlasterror 1004). RIDEV_NOLEGACY is a valid flag though. Maybe when I removed the keyboard stuff from the MS example it's no longer asking for the right variable or somet. I'm so confused.

Any help is greatly appreciated. Thank you for your time.

#include <Windows.h>
#include <iostream>
#include <Strsafe.h>

static bool Mouse_Initialized = 0;
HRESULT hResult;
TCHAR szTempOutput[3000];

void init() {

	if (!Mouse_Initialized) {
			Rid[0].usUsagePage = 0x01; 
			Rid[0].usUsage = 0x02; 
			Rid[0].dwFlags = RIDEV_NOLEGACY;
			Rid[0].hwndTarget = 0;

			if (RegisterRawInputDevices(Rid, 2, sizeof(Rid[0])) == FALSE) {
				std::cout << "init failed " << "\n"; 
				std::cout << GetLastError() << "\n";


	Mouse_Initialized = 1;


int main() {


	MSG msg = {0};

	DefWindowProc(msg.hwnd, msg.message, msg.wParam, msg.lParam);

	if (WM_INPUT == 0x00FF) { 

    UINT dwSize;

    GetRawInputData((HRAWINPUT)msg.lParam, RID_INPUT, NULL, &dwSize, 
    LPBYTE lpb = new BYTE[dwSize];

    if (lpb == NULL) 
		std::cout << "lpb == NULL" << "\n"; 
        return 0;

    if (GetRawInputData((HRAWINPUT)msg.lParam, RID_INPUT, lpb, &dwSize, 
         sizeof(RAWINPUTHEADER)) != dwSize )
         OutputDebugString (TEXT("GetRawInputData does not return correct size !\n")); 

    RAWINPUT* raw = (RAWINPUT*)lpb;

    if (raw->header.dwType == RIM_TYPEMOUSE) 
		hResult = StringCchPrintf(szTempOutput, STRSAFE_MAX_CCH, TEXT("Mouse: lLastX=%04x lLastY=%04x \r\n"), 

		if (FAILED(hResult))
		// TODO: write error handler
			std::cout << "FAILED(hResult)" << "\n";

    delete[] lpb; 
    return 0;
return 0;