简体   繁体   English

在声明它的同一行上初始化HRAWINPUT会导致RegisterRawInputDevices失败?

[英]Initializing HRAWINPUT on the same line as declaring it causes RegisterRawInputDevices to fail?

I'm trying to learn to handle input because I'm working on a bare bones 3d graphics engine, and when trying to learn raw input I came across this anomaly: Creating and initializing a HRAWINPUT struct as lParam causes an earlier call to RegisterRawInputDevices to fail. 我正在尝试处理输入,因为我正在使用裸露的3d图形引擎,并且在尝试学习原始输入时遇到了这个异常:创建并初始化HRAWINPUT结构为lParam导致对RegisterRawInputDevices的更早调用失败。

#include <iostream>
#include <windows.h>
using namespace std;

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_INPUT:
        HRAWINPUT hRawInput = lParam; //this causes the program to print "But it failed!"
//HRAWINPUT hRawInput;
//hRawInput = lParam; //These lines however work fine.
        break;
    case WM_DESTROY:
        PostQuitMessage (0);
        break;
    default:
        return DefWindowProc (hwnd, message, wParam, lParam);
    }
    return 0;
}

int WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance,LPSTR lpszArgument,int nCmdShow)
{
    CHAR cname[] = "asdf";
    HWND hwnd;
    MSG messages;
    WNDCLASSEX wincl;
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = cname;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.cbSize = sizeof (WNDCLASSEX);
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.cbClsExtra = 0;
    RegisterClassEx(&wincl);
    hwnd = CreateWindowEx (0,cname,"invisible raw input window",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,544,375,HWND_DESKTOP,NULL,hThisInstance,NULL);
    RAWINPUTDEVICE Rid[1];
    Rid[0].usUsagePage = 0x01;
    Rid[0].usUsage = 0x02;
    Rid[0].dwFlags = RIDEV_INPUTSINK;
    Rid[0].hwndTarget = hwnd;
    if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[0])) == FALSE)
    cout << "But it failed!";

    while (GetMessage(&messages,0,0,0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }

    return messages.wParam;
}

compiled with MingW64 version 8.3.0 and ran on windows 7. This makes no sense, what is going on? 使用MingW64版本8.3.0编译并在Windows 7上运行。这没有意义,这是怎么回事?

EDIT1: the RegisterRawInputDevices(Rid, 1, sizeof(Rid[0])) 's GetLastError() is 0x57, "ERROR_INVALID_PARAMETER" but in x64dbg when viewing the call I can see that 1 is always 1, sizeof(Rid[0]) always = 0x10, and even RID's reference address is identical. EDIT1: RegisterRawInputDevices(Rid, 1, sizeof(Rid[0]))GetLastError()为0x57,“ ERROR_INVALID_PARAMETER”,但在x64dbg中,当查看呼叫时,我可以看到1始终为1,sizeof(Rid [0] )始终为0x10,甚至RID的参考地址也相同。

It turns out return 0; 结果是return 0; in the window procedure needs to be return DefWindowProc(hwnd, message, wParam, lParam); 在窗口过程中需要return DefWindowProc(hwnd, message, wParam, lParam); .

This is really confusing because the example code here at MSDN uses return 0; 这确实令人困惑,因为MSDN此处的示例代码使用return 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM