Windows XP Windows 7 Windows 2003 Windows Vista Windows教程綜合 Linux 系統教程
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟件教程
 Windows教程網 >> Windows 7系統教程 >> win7系統基礎知識 >> 如何識別移動硬盤

如何識別移動硬盤

日期:2017/1/24 14:49:29      編輯:win7系統基礎知識

好久沒寫東西了,貼點代碼,慶祝五一。

Ring 3下的方法:
童鞋們應該已經發現了,GetDriveType函數只能識別出U盤,無法識別移動硬盤。移動硬盤和本地硬盤將返回相同的類型DRIVE_FIXED。MSDN上的建議是用一個蛋疼無比的函數SetupDiGetDeviceRegistryProperty來識別USB設備。

其實有個更方便的方法,可以通過DeviceIoControl函數查詢總線類型來識別USB設備。貼代碼:

BOOL IsUsbDriver(TCHAR tchDrvName)
/*++Routine Description: 判斷某分區是否是USB設備。Arguments: tchDrvName - 輸入參數,驅動器號。Return Value: 是USB設備則返回TRUE,否則返回FALSE,
查詢失敗也返回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 pDevDesc->BusType == BusTypeUsb;
} else {
// 查詢失敗
return FALSE;
}
}
 

Ring 0下的方法:

在Ring 0下,通過DiskDeviceObject的名字在一定概率上能判斷該DiskDeviceObject對應的卷設備是否是USB設備。即名稱為HardDiskVolumeXXX的DiskDeviceObject通常是本地硬盤。不過這個方法其實也很不靠譜,尤其是Win7下,插個U盤都是HardDiskVolumeXXX。

其實最好的方法同樣是查詢總線類型。貼代碼:

BOOLEAN IsUsbDriver(IN PDEVICE_OBJECT pDeviceObject)
/*++Routine Description: 判斷某磁盤設備是否是USB設備。Arguments: pDeviceObject - 輸入參數,卷設備對應的 DiskDeviceObject。
卷設備與 DiskDeviceObject 之間的關系可以參考:
http://hi.baidu.com/nmn714/blog/item/3d2ccccad529e994c8176842.HtmlReturn Value: 是USB設備則返回TRUE,否則返回FALSE,
查詢失敗也返回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) {
// 創建 IRP 失敗
return FALSE;
}
Status = IoCallDriver(pDeviceObject, pNewIrp);
if (Status == STATUS_PENDING) {
Status = KeWaitForSingleObject(&WaitEvent, Executive, KernelMode, FALSE, NULL);
Status = iOStatus.Status;
}if (!NT_SUCCESS(Status)) {
// 查詢失敗
return FALSE;
} pStorageDeviceDescriptor = (PSTORAGE_DEVICE_DESCRIPTOR)buff;// 返回結果
return pStorageDeviceDescriptor->BusType == BusTypeUsb;
}
Copyright © Windows教程網 All Rights Reserved