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;
}
When friends use computers to work and play, they have more or less experienced all kinds of
During the daily use of the computer, it is possible to connect to the wired netw
1. Right click on the “Computer” icon on the desktop and select ““Manage” to open the “Compute
Mathematics is a very important subject, and mathematical formulas are useful in many fields. Genera
Precautions for installing WIN7 system (1)
I have problems with Windows 7 remote assistance to help you
Win7 how to partition the hard disk partition Win7 hard disk partition tutorial
Solve the problem of Windows 7 RC installation process
4 steps to find Windows Mail (1) hidden in the small 7
Windows 7 teaches you to listen to songs, chat two correctly
How to update the graphics card driver under Win7
Teach you how to make Win 7 support both DHCP and fixed IP
Turn off Windows 7 and receive the prompt "Setup Backup"
Win7 is divided into several districts. How many districts is it suitable for the win7 system?
Running with the Win7 hypervisor
How to remove the installation boot menu left behind Windows 7
How win7 uses the system to repair the CD
Win8 Encyclopedia 34: How to set the font in the system
How does Win7 modify and delete the network connection name
Solution to software incompatibility under xP Tip
The strongest in history? From 0 to 33600 Logical Port Details (5)
How to watch massive video with Win7 Media Center
Share how to solve winXP, Win7 files can not visit each other