#if defined(Q_OS_WIN)#include#include #include #endifQByteArray *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 list= QSerialPortInfo::availablePorts(); for(int i=0;i 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:"< (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