计算机视觉技术 ssh命令 ssm spring redux automation datagrid upload vue传值 npm安装vue access教学视频 android常用布局 java常用的包 mysql分区表优劣分析 反函数的二阶导数 kubernetes视频 python调用函数 python服务器开发 java多态 java的泛型 java语言介绍 java接口的实例 java连接mysql的jar包 java集合类型 vb编程 信息系统项目管理师教程 修改mac地址软件 java电子书下载 苹果5s降级 ps插入表格 idea重命名快捷键 dota改键工具 HTML5从入门到精通 朋友圈访客记录教程 selinux关闭 显示器面板类型 苍灵世界 猫眼电影票 pr动态字幕 视频添加水印
当前位置: 首页 > 学习教程  > 编程语言

qt识别硬件盘符/USB设备

2020/9/19 14:57:40 文章标签:

识别硬件盘符

#ifdef WIN32
#include <windows.h>
#endif
void ScanHD()
{
    QFileInfoList list =  QDir::drives();  //获取当前系统的盘符
    QString path;
    bool bUsb = false;
    for(int i=0;i<list.count();i++)
    {
        path = list[i].filePath();
        qDebug()<<path;
        //windows API的头文件中,GetDriveType的参数是PCWCH或者LPCWCH类型,需要进行转换,
        //查看typedef CONST WCHAR *LPCWCH, *PCWCH;
        //typedef wchar_t WCHAR;    // wc,   16-bit UNICODE character
        UINT ret = GetDriveType((WCHAR *) path.utf16());
        qDebug()<<ret;
        if(ret == DRIVE_REMOVABLE)
        {
            qDebug()<<"存在可移动盘:"<<path;
            bUsb = true;
            this->AddDisk(path);
        }
    }
}

注册USB事件监听USB设备插拔

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

二、使用步骤

#include "MainWindow.h"
#include <QDebug>

#ifdef WIN32
#include <windows.h>
#include <winioctl.h> 
//#include <WinUser.h>
#include <devguid.h>
//#include <SetupAPI.h>
#include <usbiodef.h>
#include <initguid.h>
#include <dbt.h>

static const GUID GUIDList[] =
{
    { 0xA5DCBF10, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } }, // GUID_DEVINTERFACE_USB_DEVICE
    { 0x53f56307, 0xb6bf, 0x11d0, { 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b } }, // GUID_DEVINTERFACE_DISK
    //{ 0x4D1E55B2, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } }, // GUID_DEVINTERFACE_HID
    //{ 0xad498944, 0x762f, 0x11d0, { 0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c } }, // GUID_NDIS_LAN_CLASS
    //{ 0x86e0d1e0, 0x8089, 0x11d0, { 0x9c, 0xe4, 0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73 } }, // GUID_DEVINTERFACE_COMPORT
    //{ 0x4D36E978, 0xE325, 0x11CE, { 0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18 } }, // GUID_DEVINTERFACE_SERENUM_BUS_ENUMERATOR
    //{ 0x97F76EF0, 0xF883, 0x11D0, { 0xAF, 0x1F, 0x00, 0x00, 0xF8, 0x00, 0x84, 0x5C } }, // GUID_DEVINTERFACE_PARALLEL
    //{ 0x811FC6A5, 0xF728, 0x11D0, { 0xA5, 0x37, 0x00, 0x00, 0xF8, 0x75, 0x3E, 0xD1 } } // GUID_DEVINTERFACE_PARCLASS
    { 0xF18A0E88, 0xC30C, 0x11D0, {0x88, 0x15, 0x00, 0xA0, 0xC9, 0x06, 0xBE, 0xD8}} // GUID_DEVINTERFACE_USB_HUB
};

bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
#ifdef WIN32
	if (eventType == "windows_generic_MSG")
	{
		MSG *param = static_cast<MSG *>(message);
		switch (param->message)
		{
			case WM_COPYDATA:
			{
				COPYDATASTRUCT *cds = reinterpret_cast<COPYDATASTRUCT*>(param->lParam);
				if (cds->dwData == 10000) 
				{
					QString strMessage = QString::fromUtf8(reinterpret_cast<char*>(cds->lpData), cds->cbData);
					onReceivedMessage(0, strMessage.toUtf8());
					*result = 1;
					return true;
				}
			}
		}
		OnWindowsEvent(message);
	}
#endif

	return QWidget::nativeEvent(eventType, message, result);
}

void MainWindow::InitWindowsEvent()
{
    RegisterFilter();
}

void MainWindow::RegisterFilter()
{
    HDEVNOTIFY hDevNotify;
    DEV_BROADCAST_DEVICEINTERFACE NotifacationFiler;
    ZeroMemory(&NotifacationFiler, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
    NotifacationFiler.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    NotifacationFiler.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;

    for (int i = 0; i < sizeof(GUIDList) / sizeof(GUID); i++)
    {
        NotifacationFiler.dbcc_classguid = GUIDList[i];

        hDevNotify = RegisterDeviceNotification((HANDLE)this->winId(), &NotifacationFiler, DEVICE_NOTIFY_WINDOW_HANDLE);
        if (!hDevNotify)
        {
            //int Err = GetLastError();
        }
    }
}

void MainWindow::OnWindowsEvent(void *message)
{
    MSG *msg = reinterpret_cast<MSG*>(message);
    if (msg->message == WM_DEVICECHANGE)
    {
        PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;

        switch (msg->wParam)
        {
        case DBT_DEVICEARRIVAL:
        {
            if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//移动存储设备
            {
            }
            else if (lpdb->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) // interface
            {
                PDEV_BROADCAST_DEVICEINTERFACE devInterface;
                devInterface = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;

                QString interfaceName = QString::fromWCharArray(devInterface->dbcc_name);
				qDebug() << "Desktop DBT_DEVICEARRIVAL dbcc_name: " << interfaceName;

                //监听到有USB设备插拔,识别设备并处理
            }
            else if (lpdb->dbch_devicetype == DBT_DEVTYP_HANDLE) {}
            else if (lpdb->dbch_devicetype == DBT_DEVTYP_PORT) {}
            else if (lpdb->dbch_devicetype == DBT_DEVTYP_OEM) {}

            break;
        }

        case DBT_DEVICEREMOVECOMPLETE:
        {
            if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//移动存储设备
            {
            }
            else if (lpdb->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) // Interface
            {
                PDEV_BROADCAST_DEVICEINTERFACE devInterface;
                devInterface = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;

                QString interfaceName = QString::fromWCharArray(devInterface->dbcc_name);
				qDebug() << "Desktop DBT_DEVICEREMOVECOMPLETE dbcc_name: " << interfaceName;
                //FXVCIManager::Instance()->OnDeviceDetch(interfaceName);
            }
            else if (lpdb->dbch_devicetype == DBT_DEVTYP_HANDLE) {}
            else if (lpdb->dbch_devicetype == DBT_DEVTYP_PORT) {}
            else if (lpdb->dbch_devicetype == DBT_DEVTYP_OEM) {}

            break;
        }
        default:
            break;
        }
    }
}

#endif

检测SD

有空再写


本文链接: http://www.dtmao.cc/news_show_200204.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?