2015-05-22 24 views
5

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; 
} 
+0

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. –

+0

Ama mesaj pencereye geliyor, sadece mesajın alt kısmı yok. – zar

+0

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

cevap

0

Bunu anlayabiliyorum ve başka biri benzer bir sorunla karşılaşırsa çözüm burası.

Sorun, aşağıdaki satırda InterfaceClassGuid idi. Bu yanlış olduğunu

#define HID_CLASSGUID {0x4d1e55b2, 0xf16f, 0x11cf,{ 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}} 

, ben örnekten bunu getirdi ve bunu değiştirmek gerekir fark asla:

GUID InterfaceClassGuid = HID_CLASSGUID; 

HID_CLASSGUID benim kodda aşağıdaki şekilde ayarlanmıştır. Farklı türde bildirimler için kayıt yaptırmak için farklı değerler vardır ve yardım sistemi bu durumda yardımcı olmamıştır, ancak burada geçerli GUID values listesi yer almaktadır.

Aşağıdakilere değiştirdim ve şimdi istediğiniz bildirimleri aldım.

#define HID_CLASSGUID {0x745a17a0,0x74d3, 0x11d0, 0xb6fe, 0x00a0c90f57da} 
İlgili konular