QTC++监控USB插拔

#if defined(Q_OS_WIN)
#include <qt_windows.h>
#include <QtCore/qglobal.h>
#include <dbt.h>
#endif


QByteArray *MainWindow::receivedData=new QByteArray();//接收到数据
ThreadComPort *MainWindow::threadInitComPort=0;
#if defined(Q_OS_WIN)
static const GUID GUID_DEVINTERFACE_USBSTOR = { 0xA5DCBF10L, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };
static const GUID InterfaceClassGuid = GUID_DEVINTERFACE_USBSTOR;
static bool isDoingSearch=false;

static void SerachComPort(){
    if(isDoingSearch==false){
            isDoingSearch=true;
            bool hasDevice=false;
            QList<QSerialPortInfo> list= QSerialPortInfo::availablePorts();
            for(int i=0;i<list.count();i++){
                if(list[i].description().contains("USB CDC",Qt::CaseInsensitive)){
                    qDebug()<<"设备已经插入,端口:"<<list[i].portName();
                    Globals::PortName=list[i].portName();
                    hasDevice=true;
                }
            }
           if(!hasDevice){
                qDebug()<<"请插入设备";
                Globals::PortName="NoDevice";
                AddControl(new NoDevice());
                Globals::parent->InitComPort();
           }
           isDoingSearch=false;
        }
}
LRESULT CALLBACK dw_internal_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    if (message == WM_DEVICECHANGE) {
        switch (wParam) {
        case DBT_DEVNODES_CHANGED:
            qDebug()<<"设备插拔啦.";
            Globals::parent->CloseComPort();
            SerachComPort();
            break;
        }
    }
    // qDebug()<<"HWND:"<<hwnd;
    return DefWindowProc(hwnd, message, wParam, lParam);
}


 static inline HWND WndProc(const void* userData)
 {
     QString className="UsbMonitor";
     HINSTANCE hi = qWinAppInst();

     WNDCLASS wc;
     wc.style = 0;
     wc.lpfnWndProc = dw_internal_proc;
     wc.cbClsExtra = 0;
     wc.cbWndExtra = 0;
     wc.hInstance = hi;
     wc.hIcon = 0;
     wc.hCursor = 0;
     wc.hbrBackground = 0;
     wc.lpszMenuName = NULL;
     wc.lpszClassName = reinterpret_cast<const wchar_t *>(className.utf16());
     RegisterClass(&wc);

     HWND hwnd = CreateWindow(wc.lpszClassName,	   // classname
                              wc.lpszClassName,	   // window name
                              0,					  // style
                              0, 0, 0, 0,			 // geometry
                              0,					  // parent
                              0,					  // menu handle
                              hi,					 // application
                              0);					 // windows creation data.
     if (hwnd) {
         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;
         RegisterDeviceNotification(hwnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
     }
     return hwnd;
 }
#endif

  

转载于:https://www.cnblogs.com/HCCZX/p/4759845.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值