How To get the usbdisk's drive letter properly

2023-10-27

Sample image

Introduction

We know USB disk should be a removable disk just like floppy disk, and be used more and more widely now. Because, the USB disk is more faster, reliable, and affordable than old floppy disk.

So, when we want to check one disk or drive of target system is removable or not, we may be thinking of using API function "GetDriveType()". Yes, it really works on some USB device, such as 16MB, 32 MB, 64 MB , and 128MB. ;-) Here, how about removable hard disk which is connected to system by USB channel? - Windows will report them as 'Fix Disk', and we would get the same result using 'GetDriveType()' function.

How can we differentiate between these USB ‘Fix Disk’ and Those IDE ‘Fix Disk’? Here is the solution for this event.

Background

(Why do I want get the USB disks' drive letter properly? Because I want to check the virus while one new USB drive is inserted. We should not be remiss of the virus which is more and more technical day by day:)

Since we can get the base information about the disk type (using API Function ‘GetDriveType()’), we may only want to check the ‘Removable Hard Disk’ to verify its bus-type. Well, we’ll have two steps to get the USB disk’s drive letters:

Code Thoughts

1. Get the disks base information:

switch ( GetDriveType( szDrvName ) ) 
{
 case 0: // The drive type cannot be determined.
 case 1: // The root directory does not exist.
 drivetype = DRVUNKNOWN;
 break;
 case DRIVE_REMOVABLE: // The drive can be removed from the drive.
 drivetype = DRVREMOVE;
 break;
 case DRIVE_CDROM: // The drive is a CD-ROM drive.
 break;
 case DRIVE_FIXED: // The disk cannot be removed from the drive.
 drivetype = DRVFIXED;
 break;
 case DRIVE_REMOTE: // The drive is a remote (network) drive.
 drivetype = DRVREMOTE;
 break;
 case DRIVE_RAMDISK: // The drive is a RAM disk.
 drivetype = DRVRAM;
 break;
}

These codes above are based on ‘Article ID: Q161300 HOWTO: Determine the Type of Drive Using Win 32’ from MSDN.

2. Determinate the bus type of the ‘Fix Disk’:

Now, we may embed our codes at the ‘case = DRIVE_FIXED’:

Open the drive which we get now:

hDevice = CreateFile(szBuf, 
  GENERIC_READ, 
  FILE_SHARE_READ | FILE_SHARE_WRITE, 
  NULL, OPEN_EXISTING, NULL, NULL);

If we opened this drive, check its BUSTYPE, using API GetDisksProperty():

if(GetDisksProperty(hDevice, pDevDesc))
{
 if(pDevDesc->BusType == BusTypeUsb) // This is the ‘Check Point’!!! ;-)
 {
  // We store the drive letter here
  szMoveDiskName[k] = chFirstDriveFromMask(temp); 
  szMoveDiskName[0]=k;
  k++;
 }
}

Close this drive when we finished our work on it:

CloseHandle(hDevice);

3. How does the GetDisksProperty() work?

/********************************************************
*
* FUNCTION: GetDisksProperty(HANDLE hDevice, 
* PSTORAGE_DEVICE_DESCRIPTOR pDevDesc)
*
* PURPOSE: get the info of specified device
*
******************************************************/
BOOL GetDisksProperty(HANDLE hDevice, 
  PSTORAGE_DEVICE_DESCRIPTOR pDevDesc)
{
 STORAGE_PROPERTY_QUERY Query; // input param for query
 DWORD dwOutBytes; // IOCTL output length
 BOOL bResult; // IOCTL return val

 // specify the query type
 Query.PropertyId = StorageDeviceProperty;
 Query.QueryType = PropertyStandardQuery;

 // Query using IOCTL_STORAGE_QUERY_PROPERTY 
 bResult = ::DeviceIoControl(hDevice, // device handle
 IOCTL_STORAGE_QUERY_PROPERTY, // info of device property
  &Query, sizeof(STORAGE_PROPERTY_QUERY), // input data buffer
  pDevDesc, pDevDesc->Size, // output data buffer
  &dwOutBytes, // out's length
  (LPOVERLAPPED)NULL); 

 return bResult;
}

Comments

  1. There are some structures not commented, see usbdisks_src for them.;
  2. Floppy drive (A: or B:) is reported as USB Disks by this demo, -And- it is easy to correct this, just putting some codes to the ‘case = DRIVE_REMOVABLE:‘;

History

  • 2004-03-29 - 1st GO
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

How To get the usbdisk's drive letter properly 的相关文章

随机推荐

  • 机器学习在工业应用中的新思考

    人工智能在学术界默默发展了很多很多年 从最早的神经网络 到10年前风靡的SVM bagging and boosting 如今的深度学习 日新月异 各领风骚数几年 工业界的发展从最早应用于纯粹的互联网企业 近几年开始应用到更多更广泛的场景
  • Win32环境下两种用于C++的线程同步类

    线程同步是多线程程序设计的核心内容 它的目的是正确处理多线程并发时的各种问题 例如线程的等待 多个线程访问同一数据时的互斥 防死锁等 Win32提供多种内核对象和手段用于线程同步 如互斥量 信号量 事件 临界区等 所不同的是 互斥量 信号量
  • RPC流程一 - proto编写

    参考 https github com goyas goya rpc tree master src 对RPC中的Server端和Client端流程进行整理 代码目录 CMakeLists txt config cmake sample e
  • 手把手教你用代码实现 SSO 单点登录

    点击上方 Java基基 选择 设为星标 做积极的人 而不是积极废人 每天 14 00 更新文章 每天掉亿点点头发 源码精品专栏 原创 Java 2021 超神之路 很肝 中文详细注释的开源项目 RPC 框架 Dubbo 源码解析 网络应用框
  • SSH连接慢的问题

    维护linux经常需要使用ssh进行连接 但是有时会很郁闷的发现ssh连接部分机器的时候特别的慢 之前写过一篇文章
  • 【数字IC精品文章收录】近500篇文章

    数字IC全站文章索引demo版 建议收藏慢慢看 一 项目说明 1 1 索引目的 1 2 收录原则 1 3 投稿方式 1 4 版本迭代 二 数字IC学习路线 三 通用技能篇 3 1 数字电路 3 2 硬件描述语言 Verilog 3 3 li
  • React常见面试题

    React常见面试题 1 说说你对react的理解 有哪些特性 2 说说Real DOM和Virtual DOM的区别 优缺点 3 说说React生命周期有哪些不同的阶段 每个阶段对应的方法 4 说说React中setState执行机制 5
  • 【应用笔记】Sub-1G系列产品CW32W031单片机CAD应用参考

    一 功能介绍 CW32W031 的射频部分支持 CAD 中断 从 Deepsleep 进入 STB3 开启 CAD 功能并进入 RX 模式后 CW32W031 会检测信道中是否会有 ChirpIOT 信号 如果存在将 CAD IRQ 置高
  • 袁红岗的程序员修炼之道

    袁红岗的程序员修炼之道 转载引言 袁红岗1994年加盟金蝶 1996年在整个行业还处在DOS时代时最早成功开发出基于Windows平台的金蝶财务软件 开创了依靠技术创新推动企业快速发展的典范 1998年成功开发出基于三层结构组件技术的金蝶K
  • 解决Jenkins报告在浏览器无法显示问题

    在使用jenkins定时启动并收集测试报告的过程中 发现在jenkins上配置的HTML Report打开之后样式不对 页面一片空白 打开开发者模式 发现页面有很多脚本报错 提示有很多资源文件被禁止加载了 百度之后 发现这个是jenkins
  • 【JavaScript】花点时间了解执行上下文

    引言 当我们在浏览器中运行JavaScript代码时 浏览器会先创建一个全局执行上下文 Global Execution Context 然后逐行解析和执行代码 执行上下文是JavaScript中非常重要的概念 它决定了代码的执行顺序和作用
  • Echarts图例legend选中select状态动态更新(踩坑)

    今天计划实现一个小功能 但是却在细节上浪费了大量的时间 特此记录 想要实现的功能是这样的 如下一个ECharts图 后台会一次性接收85条曲线的数据 但在初始化的时候只显示前7条曲线 官方文档 给出了 legend selected 这个属
  • U++ 截屏并拿到所有的截屏资源

    void APluginUseActor spotScreen TArray
  • Qt5下Qxlsx模块安装及使用

    文章目录 1 未安装Qxlsx的程序效果 2 安装Perl 编译Qxlsx源码用 2 1 下载 ActivePerl 5 28 2 2 安装 ActivePerl 5 28 3 下载并编译Qxlsx源码 3 1 下载Qxlsx源码 3 2
  • 树结构(有id和pid字段)数组,生成多层嵌套的json对象

    传入的数组有id和父节点pid字段 通过它们的关联构造成一棵或多棵树结构 param nodes 集合 param treeRootId 根节点的id function createTreeData nodes treeRootId var
  • 【模拟电路】电极驱动H桥

    H桥式驱动电路 是因为它的形状酷似字母H 如图 当Q1和Q4导通时 电机正转 当Q2和Q3导通时 电机反转 任何时候 H桥同一侧的晶体管不能同时导通 否则就会造成电源和地之间短路 电机正转 电机反转 一个双电机驱动电路 H桥 设计思路 使用
  • 深度学习图片数据集分析手段--个人经验小结

    当拿到一批图片数据集的时候 我应该做哪些分析 这里是我自己从业过程中的一些分析手段 从哪里学习的已经不可考 如果有其他的大家觉得有用的分析 可以和我分享一下 避免我自己闭门造车 这里根据已知的信息量分类两种情况 1 只有图片数据 没有标注数
  • Airsim探索01

    Airsim github地址 https github com microsoft AirSim git 官方文档 https microsoft github io AirSim use precompiled
  • linux 中常用的压缩和解压缩命令详解(tar zip)

    文章目录 一 tar命令 1 压缩 2 解压 二 zip命令 1 压缩 2 解压 三 文件加密压缩和密码解压 1 tar命令 1 1 加密压缩 1 2 密码解压 2 zip命令 2 1 加密压缩 2 2 密码解压 在工作中 涉及到文件传输
  • How To get the usbdisk's drive letter properly

    Introduction We know USB disk should be a removable disk just like floppy disk and be used more and more widely now Beca