阅读:2579回复:9
在文件驱动程序中检测是否是U盘[转自网络]
ULONG GetStorageDeviceBusType(IN PDEVICE_OBJECT DeviceObject)
{ PIRP NewIrp; PSTORAGE_DEVICE_DESCRIPTOR Descriptor; STORAGE_PROPERTY_QUERY Query; CHAR Buffer[BUFFER_SIZE]; KEVENT WaitEvent; NTSTATUS Status; IO_STATUS_BLOCK IoStatus; // first set the query properties Query.PropertyId = StorageDeviceProperty; Query.QueryType = PropertyStandardQuery; // initialize the waitable event KeInitializeEvent(&WaitEvent, NotificationEvent, FALSE); // we should build the query irp ourselves NewIrp = IoBuildDeviceIoControlRequest(IOCTL_STORAGE_QUERY_PROPERTY, DeviceObject, (PVOID)&Query, sizeof(Query), (PVOID)Buffer, NAME_BUFFER_SIZE, FALSE, &WaitEvent, &IoStatus); if (NULL == NewIrp) // can't create new irp { DbgPrint("[%s] [%u] I can't create a new irp to query the property of device (%p)!\n", __FILE__, __LINE__, DeviceObject); return BusTypeUnknown; } // send this irp to the storage device Status = IoCallDriver(DeviceObject, NewIrp); if (Status == STATUS_PENDING) { Status = KeWaitForSingleObject(&WaitEvent, Executive, KernelMode, FALSE, NULL); Status = IoStatus.Status; } if (!NT_SUCCESS(Status)) { DbgPrint("[%s] [%u] Query IOCTL_STORAGE_QUERY_PROPERTY of device (%p) failed, Status=0x% 08X!\n", __FILE__, __LINE__, DeviceObject, Status); return BusTypeUnknown; } Descriptor = (PSTORAGE_DEVICE_DESCRIPTOR)Buffer; return Descriptor->BusType; } 若本函数返回BusType=3是一般硬盘,BusType=7是USB接口设备。 |
|
|
沙发#
发布于:2009-04-14 23:03
以前一直用这个ioctl代码在应用层测试是否是u盘,偶然间找到这么一段写好的代码,大家用得着就用吧
|
|
|
板凳#
发布于:2009-04-14 23:05
具体的其它所有类型如下:
// 存储设备的总线类型 typedef enum _STORAGE_BUS_TYPE { BusTypeUnknown = 0x00, BusTypeScsi, BusTypeAtapi, BusTypeAta, BusType1394, BusTypeSsa, BusTypeFibre, BusTypeUsb, BusTypeRAID, BusTypeMaxReserved = 0x7F } STORAGE_BUS_TYPE, *PSTORAGE_BUS_TYPE; |
|
|
地板#
发布于:2009-04-14 23:28
不错,但是 已经有人发过了
|
|
地下室#
发布于:2009-04-19 18:29
谢谢分享,正在找这方面的资料呢!
|
|
5楼#
发布于:2009-04-19 19:37
这函数我添加到我的代码中,一使用就出现蓝屏,有人知道是什么原因吗?
|
|
6楼#
发布于:2009-04-20 16:11
a bug,you can fix it yourself.
|
|
|
7楼#
发布于:2011-03-17 09:52
好东西 正用得着 谢谢了
|
|
8楼#
发布于:2011-03-17 17:31
不是完整的
|
|
9楼#
发布于:2011-05-14 22:23
学习了
|
|