我正在尝试注册有关 USB 设备到达和设备从 java swing 应用程序中删除完整事件的通知。
我已成功调用
SetWindowLong(hWnd, MyUser32.GWLP_WNDPROC, listener);
注册一个监听器,我收到类型的通知WM_DEVICECHANGE
。到现在为止还挺好。
现在我想调用 RegisterDeviceNotification 以确保我收到设备到达通知并删除完整事件。这是我尝试过的:
HWND hWnd = new HWND();
hWnd.setPointer(Native.getWindowPointer(frame));
DEV_BROADCAST_DEVICEINTERFACE filter = new DEV_BROADCAST_DEVICEINTERFACE();
filter.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
filter.dbcc_name = new char[1];
filter.dbcc_reserved = new DWORD(0);
filter.dbcc_size = new DWORD(filter.size());
filter.dbcc_size = new DWORD(filter.size());
long retVal = MyUser32.MYINSTANCE.RegisterDeviceNotification(new HANDLE(hWnd.getPointer()), filter.getPointer(), DEVICE_NOTIFY_WINDOW_HANDLE);
if (retVal != 0) {
System.out.println("Error registering for usb: " + Native.getLastError());
}
我的 JNA 声明是:
public long RegisterDeviceNotification(HANDLE hRecipient, Pointer NotificationFilter, DWORD Flags);
public static class DEV_BROADCAST_DEVICEINTERFACE extends Structure {
public DWORD dbcc_size;
public DWORD dbcc_devicetype;
public DWORD dbcc_reserved;
public GUID dbcc_classguid;
public char[] dbcc_name;
}
public static final GUID GUID_DEVINTERFACE_USB_DEVICE = new GUID(new byte[] {
(byte)0xA5, (byte)0xDC, (byte)0xBF, 0x10, 0x65, 0x30, 0x11, (byte)0xD2, (byte)0x90, 0x1F, 0x00, (byte)0xC0, 0x4F, (byte)0xB9, 0x51, (byte)0xED
});
我总是收到错误 1066。任何帮助将不胜感激。如果需要更多信息,请告诉我,我可以将其包括在内。