zoukankan      html  css  js  c++  java
  • MFC 检查U盘(转)

    #include <Dbt.h>

    #include <winioctl.h>

     

    前言

      本文主要是讲在MFC中使用消息队列,并在消息中捕获系统USB插入、卸载U盘的事件。

     

    正文

      一、MFC中使用消息队列

        1.1  声明

        virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);

        1.2  实现

     

        LRESULT CTestDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
        {
            return CDialog::WindowProc(message,wParam,lParam);
        }

     

        本示例是继承CDialog。    

        char FirstDriveFromMask(ULONG unitmask) //獲取盤符
        {
          char i;
          for (i = 0; i < 26; ++i)
          {
            if (unitmask & 0x1)
            break;
            unitmask = unitmask >> 1;
          }
          return (i + 'A');
        }

     

    二、捕获U盘插入、卸载事件

      LRESULT CTestDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
      {
          switch (message)
          {
              //WM_DEVICECHANGE,系统硬件改变发出的系统消息
              case WM_DEVICECHANGE:
              {
                  PDEV_BROADCAST_HDR lpdb=(PDEV_BROADCAST_HDR)lParam;
                  switch(wParam)
                  {
                      case WM_DEVICECHANGE:
                          break;
                      case DBT_DEVICEARRIVAL://DBT_DEVICEARRIVAL,设备检测结束,并且可以使用
                      {
                          if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
                          {
                              PDEV_BROADCAST_VOLUME lpdbv =  (PDEV_BROADCAST_VOLUME)lpdb;
                              switch(lpdbv->dbcv_flags)
                              {
                                  case 0:                //U盘
                                  {
                                      CString decDriver;
                                      decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
                                      TRACE1("检测到U盘:[%s]插入! ",decDriver.GetBuffer(0));
                                  }
                                      break;
                                  case DBTF_MEDIA:    //光盘
                                      TRACE1("检测到光盘:[%c]插入! ",FirstDriveFromMask(lpdbv ->dbcv_unitmask));
                                      break;
                                }
                          }
                      }
                         break;
                      case DBT_DEVICEREMOVECOMPLETE://DBT_DEVICEREMOVECOMPLETE,设备卸载或者拔出
                      {
                          if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
                          {
                              PDEV_BROADCAST_VOLUME lpdbv =  (PDEV_BROADCAST_VOLUME)lpdb;
                              switch(lpdbv->dbcv_flags)
                              {
                                  case 0:                //U盘
                                  {
                                      CString decDriver;
                                      decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
                                      TRACE1("检测到U盘:[%s]拔出! ",decDriver.GetBuffer(0));
                                  }
                                      break;
                                  case DBTF_MEDIA:    //光盘
                                      break;
                              }
                          }
                      }
                        break;
                  }
              }
              break;
          }
          return CDialog::WindowProc(message,wParam,lParam); 
      }

  • 相关阅读:
    mysql root密码重置
    fetch跨域问题
    HTML5触摸事件(touchstart、touchmove和touchend)
    react-router-dom
    清理网站缓存
    从零开始学java (标识符,关键字,基本数据类型)
    从零开始学java ( 初始java)
    入职一年心得
    guava 函数式编程三兄弟
    java读取各种类型文件
  • 原文地址:https://www.cnblogs.com/waterair/p/6907403.html
Copyright © 2011-2022 走看看