开发者

How to judge whether a driver is a floppy or a removable flash disk in C++?

I wonder if there is an API to disti开发者_开发技巧nguish floppy and flash disk in windows, C++ And is it possible to link a flash disk to A:\? Many Thanks!


First, you need to get the drive's type (GetDriveTypeA). If the result is equal to DRIVE_REMOVABLE, the letter drive will point either to a floppy of a removable flash drive (or maybe other type of removable disk). If the result is not DRIVE_REMOVABLE, there is no chance that this is a removable flash drive. However, beware from the point of view Window has, there is little difference between an external USB harddrive and a removable flash disk (I think the only difference is that a removable flash disk does not have a partition table, so it will have only one partition - though I'm not very sure).

Anyway, for DRIVE_REMOVABLE type, you need to query for more advanced properties of the device. In order to do this, first you need to open the physical device with somethink like this:


hDevice = CreateFileA("\\\\?\\X:", GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, NULL)

If the open succeeds, you need to issue a DeviceIoControl command to this device:


STORAGE_PROPERTY_QUERY Query;
Query.PropertyId = StorageDeviceProperty;
Query.QueryType = PropertyStandardQuery;

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
    );


If pDevDesc->BusType == BusTypeUsb, then X: points to a removable flash drive. The code works, however you need to read the documentation for DeviceIoControl in order to setup the pDevDesc parameter. If you have problems with it, I can give you the whole code.


You can change the letters assigned to your drives somewhere in System Control, so the flash disk can be A:.

Use the OS API to query eg. the size of the disk, that should be enough to distinguish floppy and flash disk.


It is possible to link a flash disk to A:, but only if you have no floppy drives.
See Defining an MS-DOS Device Name for information about how to do this in a program.

You can determine whether a drive letter maps to a floppy device by using QueryDosDevice on the drive letter. Floppy drives will return "\Device\Floppy0" or "\Device\Floppy1"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜