How to identify the mobile hard disk

  

has not written anything for a long time, paste the code, celebrate May Day.

Ring 3 method:
Children should have found out that the GetDriveType function can only recognize the U disk and cannot recognize the mobile hard disk. The removable and local hard drives will return the same type DRIVE_FIXED. The advice on MSDN is to identify the USB device with a function called SetupDiGetDeviceRegistryProperty.

In fact, there is a more convenient way to identify the USB device by querying the bus type through the DeviceIoControl function. Post code:

BOOL IsUsbDriver(TCHAR tchDrvName)
/*++Routine Description: Determines whether a partition is a USB device. Arguments: tchDrvName - Input parameters, drive letter. Return Value: Returns TRUE if it is a USB device, otherwise returns FALSE,
The query also returns FALSE. Author:
Fypher
http://hi.baidu.com/nmn714
2011/5/1--*/
{
TCHAR tcsDrvName[8] = TEXT("\\\\ \\\\.\\\\X:");
tcsDrvName[4] = tchDrvName;HANDLE hDevice = CreateFile( tcsDrvName,
GENERIC_READ,
FILE_SHARE_READ |  FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);if ( hDevice == INVALID_HANDLE_VALUE ) {
return FALSE;
}
STORAGE_PROPERTY_QUERY StoragePropertyQuery;
StoragePropertyQuery.PropertyId = StorageDeviceProperty;
StoragePropertyQuery.QueryType = PropertyStandardQuery;
BYTE buff[1024] = {0};
PSTORAGE_DEVICE_DESCRIPTOR pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)buff;
pDevDesc->Size = sizeof(buff);
DWord dwOutLen;
BOOL res = DeviceIoControl( hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
&StoragePropertyQuery,
sizeof(STORAGE_PROPERTY_QUERY),
pDevDesc,
pDevDesc->Size,
&dwOutLen,
NULL
);
CloseHandle(hDevice);if (res) {
//Return result
return pDevDesc-> BusType == BusTypeUsb;
} else {
//Query failed
return F ALSE;
}
}

Ring 0 method:

Under Ring 0, the DiskDeviceObject name can be used to determine whether the volume device corresponding to the DiskDeviceObject is a USB device by a certain probability. . That is, the DiskDeviceObject named HardDiskVolumeXXX is usually a local hard disk. However, this method is actually very unreliable, especially under Win7, inserting a U disk is HardDiskVolumeXXX.

In fact, the best way is to query the bus type. Post code:

BOOLEAN IsUsbDriver(IN PDEVICE_OBJECT pDeviceObject)
/*++Routine Description: Determines whether a disk device is a USB device. Arguments: pDeviceObject - Input parameter, DiskDeviceObject corresponding to the volume device.
The relationship between the volume device and DiskDeviceObject can be referred to:
http://hi.baidu.com/nmn714/blog/item/3d2ccccad529e994c8176842.HtmlReturn Value: is a USB device that returns TRUE, otherwise returns FALSE, < BR> The query failed also returns FALSE. Author:
Fypher
http://hi.baidu.com/nmn714
2011/5/1--*/
{
PIRP pNewIrp;
PSTORAGE_DEVICE_DESCRIPTOR pStorageDeviceDescriptor;
STORAGE_PROPERTY_QUERY StoragePropertyQuery;
CHAR buff[128];
KEVENT WaitEvent;
NTSTATUS Status;
IO_STATUS_BLOCK iOStatus; StoragePropertyQuery.PropertyId = StorageDeviceProperty;
StoragePropertyQuery.QueryType = PropertyStandardQuery;KeInitializeEvent(& WaitEvent, NotificationEvent, FALSE); pNewIrp = IoBuildDeviceIoControlRequest( IOCTL_STORAGE_QUERY_PROPERTY,
pDeviceObject,
(PVOID)&StoragePropertyQuery,
sizeof(StoragePropertyQuery),
(PVOID)buff,
sizeof(buff ),
FALSE,
&WaitEvent,
&iOStatus
);
if (!pNewIrp) {
//Fail to create IRP
return FALSE;
}
Status = IoCallDriver(pDeviceObject, pNewIrp);
if (Status == STATUS_PENDING) {
Status = KeWaitForSingleObject(&WaitEvent, Execut Ive, KernelMode, FALSE, NULL);
Status = iOStatus.Status;
}if (!NT_SUCCESS(Status)) {
//Query failed
return FALSE;
} pStorageDeviceDescriptor = (PSTORAGE_DEVICE_DESCRIPTOR)buff;//Return result
return pStorageDeviceDescriptor->BusType == BusTypeUsb;
}
Copyright © Windows knowledge All Rights Reserved