How To get the usbdisk's drive letter properly

通过DeviceIoControl获取U盘或移动硬盘的出品商、制造商和版本号等信息 本文通过DeviceIoControl获取插入计算机的USB设备的vender、producter和version 阅读详情

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, 32MB, 64MB, 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 Win32’ 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
U盘病毒(类removeable disk病毒) U盘病毒,打开文件夹只显示exe的磁盘,类removeable disk病毒 阅读详情

相关推荐

Delphi硬盘ID检索教程与实战

Delphi,作为现代软件开发领域中的一种历史悠久的编程语言,以其快速开发能力、强大的编译器和丰富的组件库而闻名。本章节旨在为读者提供对Delphi语言的基本了解和它在软件开发中的应用概述。硬盘ID是硬盘制造时由厂商分配的一个唯一标识符,类似于硬盘的”指纹”。它是一个用于识别硬盘的物理地址,通常由一系列字母和数字组成。在系统中,硬盘ID可以用于区分不同的存储设备,尤其是在有多个硬盘的计算机系统中。了解硬盘ID对于硬盘的管理和维护具有重要意义。

weixin_34064233的博客 739

DELPHI 查询硬盘厂商、型号、ID号

直接上代码,不是原创,我不过是扒老外衣,供自己取暖。需要注意的是,以下代码在D7完美运行,DELPHI XE10.2 ,需要改字符类型。也可完美运行,本人亲测。 引用单元 unit hddinfo; interface uses Windows, SysUtils, Classes; const   IOCTL_STORAGE_QUERY_PROPERTY = $2D1400; type TH...

qq457565758的博客 1165

DELPHI获取硬盘物理ID源代码

DELPHI获取硬盘物理ID,全球唯一ID,无重复,DELPHI开发源代码

IOCTL_STORAGE_QUERY_PROPERTY

最近学习scsi和DeviceIoControl,下载了微软WDK一些例子,以下代码精简自Windows-driver-samples-master\storage\tools\spti\src\spti.c ,略有修改。 #include <windows.h> #include <stdio.h> #include <strsafe.h> voi...

weixin_30632899的博客 3428

实时获得USB磁盘(再次更新)

by czy10分感谢sinister老大指点,程序流层:先通过设备消息得到新加入的盘符并且排除映射盘(net use/subst).然后通过DeviceIoControl函数发送IOCTL_STORAGE_BASE equ FILE_DEVICE_MASS_STORAGE得到盘符的总线类别,而不是通过一般的GetDirverTyte,或是网上有些人说的IOCTL_STORAGE_GET_ME

1455

实战DeviceIoControl 之五:列举已安装的存储设备

P.bhw98{ PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 9pt; PADDING-BOTTOM: 0px; MARGIN: 10px 0px 5px; LINE-HEIGHT: normal; PADDING-TOP: 0px; FONT-FAM

1万+

野火STM32F103ZET6(霸道)开发板学习中,FatFs文件系统f_mount使用时的 FR_INVALID_DRIVE(11)的错误

在进行FatFs文件系统的移植时,正常更改了diskio.c中相应的存储硬件的使用函数,但是在编写主函数使用f_mount函数挂载外部FLASH时仍然不能够使用,并且报错11, FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */ 以下为diskio.c中文件的更改 #include "diskio.h" /* FatFs lower layer API */ #inclu...

qq_27699199的博客 3936

STM32-串行FLASH文件系统FatFs

串行FLASH文件系统FatFs FatFs官方链接 FatFs文件系统的源码可以从FatFs官方下载:官方链接http://elm-chan.org/fsw/ff/00index_e.html FatFs文件系统简介 ​ FatFs是面向小型嵌入式系统的一种通用的FAT文件系统。它完全是由ANSI C语言编写并且完全独立于底层的I/O介质。因此它可以很容易地不加修改地移植到其他的处理器当中,如8051、PIC、AVR、SH、Z80、H8、ARM等。FatFs 支持FAT12、 FAT16、FAT32等格式

Lagligelang的博客 3341

USBView & How to get the Serial Number from a USB disk & qextserialport

说明:码农的福音,实现查找 USB 设备序列号的文章,对于操作USB的码农或许有些帮助。[佐料:整理这篇的代码,并将其整合到工程的代码中时,另外找到了一个更好的资源,https://code.google.com/p/qextserialport/,  qextserialport,一个为Qt写的USB enumerate & discover库,轻量而灵敏,强烈推荐] 原文:http://ww

暧暧远人村,依依墟里烟 ... 3365

获取磁盘总线类型和厂商信息以及产品信息的

以下是从群聊天中整理的代码 #ifndef VolumeType_h__ #define VolumeType_h__ #pragma once #include  #define IOCTL_STORAGE_QUERY_PROPERTY CTL_CODE(IOCTL_STORAGE_BASE, 0x0500, METHOD_BUFFERED, FILE_ANY_ACCE

FrankieWang008的专栏 1664

delphi 硬盘特理序列号

unit DiveID; interface //{$I CnPack.inc} uses Classes, Windows, SysUtils, ExtCtrls; const RelationCache = 2; RelationNumaNode = 1; RelationProcessorCore = 0; RelationProcessorPackage = 3; type TCacheType = (CacheUnified, CacheInstruction,

yygyyygy1233的博客 857

Windows下如何获取usb device信息

Windows provided some API to get the device's detail information, include USB STORAGE HID PCI etc #include #include // for GUID_DEVCLASS_CDROM etc #include #include // for MAX_DEVICE_ID_

chrovery的专栏 1万+

获取硬盘和卷或分区相关信息(容量,ID,卷标名字等)

1、枚举所有硬盘 这里主要借助了setup API,这些API主要是NT4.0之后提供的一些用于操作设备的API。 枚举所有硬盘借助了SetupDiGetClassDevs与SetupDiEnumDeviceInterfaces和 SetupDiGetDeviceInterfaceDetail这三个API。 #pragma once #ifndef DEVICE_DISK_H #define DEVICE_DISK_H #include <Windows.h> #include &l

lengye7的博客 3609

判断磁盘类型(U盘,硬盘(移动硬盘还是电脑内置硬盘))

判断磁盘类型(U盘,硬盘(移动硬盘还是电脑内置硬盘)) // CheckMoveDisk.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <Windows.h> #include <vector> #include <string> #include "AsStringUtil.h" #include <stdio.h> using namespace s

weixin_37724132的博客 3138

U盘检测的程序

#include "stdafx.h"#include "test.h"#include "usbhead.h"int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){ int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::Get

dijkstar的专栏 3354

IOCTL_STORAGE_PROPERTY_QUERY

Thanks.I also find some thing :1)use drive letter, use createfile2)use IOCTL_STORAGE_PROPERTY_QUERY, query some property of thestorage device3)if device's Bustype is BusTypeUsb, it's based on usb device4)vid & pid is obvious in out data

mayahw1983的专栏 2761
上一篇: Cisco路由器的安全配置方案
下一篇: 穿透代理服务器编程
iiprogram
博客等级 码龄25年 1066粉丝 1515原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值