windows Qt如何知道新的USB存储设备何时连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/852752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:27:46  来源:igfitidea点击:

How to know when a new USB storage device is connected in Qt?

c++windowsqtusb

提问by Skilldrick

I want to know when a USB device is connected to the computer that my Qt application is running on (in Windows). In my main QWidget, I've reimplemented winEventFilterlike this:

我想知道 USB 设备何时连接到运行我的 Qt 应用程序的计算机(在 Windows 中)。在我的主要 QWidget 中,我重新实现了winEventFilter这样:

bool winEventFilter ( MSG * msg, long * result ) {
    qDebug() << msg;
    return false;
}

I'd expect qDebug to send at least something when I connect a USB device, but I don't get anything.

当我连接 USB 设备时,我希望 qDebug 至少发送一些东西,但我什么也没收到。

I'm guessing that I'm fundamentally misunderstanding the process here - this is my first Qt app!

我猜我从根本上误解了这里的过程 - 这是我的第一个 Qt 应用程序!

采纳答案by brader24

I believe what you may be missing is the call to register for device notification. Here is code that I use to do the same thing, though I override the winEvent() method of the QWidget class and not the winEventFilter.

我相信您可能缺少的是注册设备通知的电话。这是我用来做同样事情的代码,尽管我覆盖了 QWidget 类的 winEvent() 方法而不是 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

NOTE: You will most likely need to change the values of the DEV_BROADCAST_DEVICEINTERFACEto fit your needs.

注意:您很可能需要更改 的值DEV_BROADCAST_DEVICEINTERFACE以满足您的需要。

EDIT: To use this code you will need to include the proper header files and perform the proper setup. DEV_BROADCAST_DEVICEINTERFACErequires the Dbt.h header to be included. Also, the focal point of this code is on the RegisterDeviceNotification function. Info is available on MSDN

编辑:要使用此代码,您需要包含正确的头文件并执行正确的设置。 DEV_BROADCAST_DEVICEINTERFACE需要包含 Dbt.h 标头。此外,此代码的重点是 RegisterDeviceNotification 函数。信息可在MSDN 上找到

回答by Dark Star1

I'm working along the same lines but in C#.

我正在沿着相同的路线工作,但在 C# 中。

you need to register your application with the system (look at the RegisterHidNotification() function). Mine looks like this: `

您需要向系统注册您的应用程序(查看 RegisterHidNotification() 函数)。我的看起来像这样:`

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();
        }`

The most important part of the function is the bit I've highlighted in bold (or at least tried to). Defined as: public Guid GUID_DEVINTERFACE_USB_DEVICE = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED");Hope you can adapt it to your application.

该函数最重要的部分是我用粗体突出显示的部分(或至少尝试过)。定义为:public Guid GUID_DEVINTERFACE_USB_DEVICE = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED");希望您可以将其调整到您的应用程序中。

回答by IMAN4K

You can easily reimplement QWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)and check for device connection :

您可以轻松地重新实现QWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)和检查设备连接:

    #ifdef Q_OS_WIN
    #include <Dbt.h>
    #endif

    bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result) {
#ifdef Q_OS_WIN
        MSG* msg = reinterpret_cast<MSG*>(message);
        if (msg->message == WM_DEVICECHANGE) {
            switch (msg->wParam) {
            case DBT_DEVICEARRIVAL:
                qDebug() << "connected";
                break;
            case DBT_DEVICEREMOVECOMPLETE:
                qDebug() << "disconnected";
                break;
            default:
                break;
            }
        }
#endif // Q_OS_WIN
        return false;
    }