在声明它的同一行上初始化 HRAWINPUT 会导致 RegisterRawInputDevices 失败?
Initializing HRAWINPUT on the same line as declaring it causes RegisterRawInputDevices to fail?
我正在尝试学习处理输入,因为我正在研究一个简单的 3d 图形引擎,并且在尝试学习原始输入时我遇到了这个异常:Creating and initializing a HRAWINPUT struct as lParam causes an之前对 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;
}
使用 MingW64 版本 8.3.0 和 运行 在 windows 上编译 7. 这没有意义,这是怎么回事?
EDIT1:RegisterRawInputDevices(Rid, 1, sizeof(Rid[0]))
的 GetLastError()
是 0x57,"ERROR_INVALID_PARAMETER" 但在 x64dbg 中查看调用时我可以看到 1 始终为 1,sizeof(Rid[0] ) 始终 = 0x10,甚至 RID 的参考地址也是相同的。
事实证明 window 过程中的 return 0;
需要 return DefWindowProc(hwnd, message, wParam, lParam);
。
这真的很令人困惑,因为示例代码 here at MSDN 使用 return 0;
我正在尝试学习处理输入,因为我正在研究一个简单的 3d 图形引擎,并且在尝试学习原始输入时我遇到了这个异常:Creating and initializing a HRAWINPUT struct as lParam causes an之前对 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;
}
使用 MingW64 版本 8.3.0 和 运行 在 windows 上编译 7. 这没有意义,这是怎么回事?
EDIT1:RegisterRawInputDevices(Rid, 1, sizeof(Rid[0]))
的 GetLastError()
是 0x57,"ERROR_INVALID_PARAMETER" 但在 x64dbg 中查看调用时我可以看到 1 始终为 1,sizeof(Rid[0] ) 始终 = 0x10,甚至 RID 的参考地址也是相同的。
事实证明 window 过程中的 return 0;
需要 return DefWindowProc(hwnd, message, wParam, lParam);
。
这真的很令人困惑,因为示例代码 here at MSDN 使用 return 0;