Saltar al contenido

¿Cómo saber cuando se conecta un nuevo dispositivo de almacenamiento USB en Qt?

Solución:

Creo que lo que puede estar perdiendo es la llamada para registrarse para la notificación del dispositivo. Aquí está el código que uso para hacer lo mismo, aunque anulo el método winEvent () de la clase QWidget y no el winEventFilter.

// Register for device connect notification
DEV_BROADCAST_DEVICEINTERFACE devInt;
ZeroMemory( &devInt, sizeof(devInt) );
devInt.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
devInt.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
devInt.dbcc_classguid = GUID_DEVINTERFACE_VOLUME;

m_hDeviceNotify =
    RegisterDeviceNotification( winId(), &devInt, DEVICE_NOTIFY_WINDOW_HANDLE );   
if(m_hDeviceNotify == NULL)
{
    qDebug() << "Failed to register device notification";
} // end if

NOTA: Lo más probable es que necesite cambiar los valores del DEV_BROADCAST_DEVICEINTERFACE para satisfacer sus necesidades.

EDITAR: Para usar este código, deberá incluir los archivos de encabezado adecuados y realizar la configuración adecuada. DEV_BROADCAST_DEVICEINTERFACE requiere que se incluya el encabezado Dbt.h. Además, el punto focal de este código está en la función RegisterDeviceNotification. La información está disponible en MSDN

Estoy trabajando en la misma línea pero en C #.

necesita registrar su aplicación con el sistema (mire la función RegisterHidNotification ()). El mío se ve así:

void RegisterHidNotification() //Register this application to recieve all USB device notices

        {
            BroadcastHeader dbi = new BroadcastHeader();
            int size = Marshal.SizeOf(dbi);
            dbi.Size = size;
            dbi.Type = DeviceType.DeviceInterface;
            **dbi.Classguid = GUID_DEVINTERFACE_USB_DEVICE**;
            dbi.Name = 0;
            IntPtr buffer = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(dbi, buffer, true);
            IntPtr r = RegisterDeviceNotification(this.Handle, buffer, (int)DeviceEvents.regWindowHandle);
            if (r == IntPtr.Zero)
                statusLabel.Text = GetLastError().ToString();
        }`

La parte más importante de la función es la parte que he resaltado en negrita (o al menos lo intenté). Definido como: public Guid GUID_DEVINTERFACE_USB_DEVICE = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED");
Espero que pueda adaptarlo a su aplicación.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *