/*******************************************
* WINDOWS EVENTS
********************************************/
/*We use the first WM_PAINT event to get the handle of main window
and pass it to RegisterDeviceNotification function.
It not possible to do this in the contructor because the
main window does not exist yet.
WM_DEVICECHANGE event notify us that a device is attached or detached */
bool USBexample
::nativeEvent(const QByteArray & eventType,
void * message,
long * result
) {
MSG * msg = static_cast< MSG * > (message);
int msgType = msg->message;
if(msgType == WM_PAINT)
{
if(!msgp) //Only the first WM_PAINT
{
GUID InterfaceClassGuid = HID_CLASSGUID;
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = InterfaceClassGuid;
HWND hw = (HWND) this->effectiveWinId(); //Main window handle
hDevNotify = RegisterDeviceNotification(hw,&NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
msgp = true;
}
}
if(msgType == WM_DEVICECHANGE)
{
qDebug() << "WM_DEVICECHANGE recieved";
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;
switch(msg->wParam)
{
case DBT_DEVICEARRIVAL: // never comes here!
if (lpdb -> dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
{
qDebug() << "DBT_DEVICEARRIVAL case";
PDEV_BROADCAST_DEVICEINTERFACE lpdbv = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;
int i = 0;
//to find a better way for this...
while(lpdbv->dbcc_name[i] != 0)
{
s.append(lpdbv->dbcc_name[i]);
i++;
}
s = s.toUpper();
if(s.contains(MY_DEVICE_VIDPID))
emit USB_Arrived();
}
break;
case DBT_DEVICEREMOVECOMPLETE:
if (lpdb -> dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
{
qDebug() << "DBT_DEVICEREMOVECOMPLETE case";
PDEV_BROADCAST_DEVICEINTERFACE lpdbv = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;
int i = 0;
//to find a better way for this...
while(lpdbv->dbcc_name[i] != 0)
{
s.append(lpdbv->dbcc_name[i]);
i++;
}
s = s.toUpper();
if(s.contains(MY_DEVICE_VIDPID))
emit USB_Removed();
}
break;
case DBT_DEVICEREMOVEPENDING :
{
qDebug() << "DBT_DEVICEREMOVEPENDING case";
}
break;
default:
{
qDebug() << "Went to Default case";
}
}
}
return false;
}
Bookmarks