Ben USB flash sürücü eklentisi ve Windows 7'de plugout algılama tespit etmek için bir örnek takip ediyorum. WM_DEVICECHANGE
bildirimi aldım ama DBT_DEVICEARRIVAL
değil, USB aygıtı takılı iken.WM_DEVICECHANGE, ancak benim Qt uygulamasında DBT_DEVICEARRIVAL değil
/*******************************************
* 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;
QString s;
//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;
QString s;
//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;
}
Akla gelen tek şey şu üç nokta şudur: "Bu değeri çalışma zamanında değişebileceği için kaydetmemenizi öneririz." "EffectiveWinId" nin aynı kaldığını kontrol edin. Eğer değişirse, tekrar kayıt olmanız gerekir. –
Ama mesaj pencereye geliyor, sadece mesajın alt kısmı yok. – zar
Yaptığım tek şey 'HWND' için effectiveWinId() 'yi basmaktı, aksi halde derleyici' WId'yi 'HWND'ye çeviremediğinden şikayet etti, bunun bir şey yapıp yapamayacağını bilmiyordum !? ama benim yazım yapmadığımdan nasıl derlerim? – zar