开发者

Given a volume (eg Z:), how to tell if that volume is on a device that is "removable"?

开发者 https://www.devze.com 2022-12-31 15:41 出处:网络
I want to know whether the disk that a given volume resides on is removable (CM_DEVCAP_REMOVABLE). Looking for pointers on win32.

I want to know whether the disk that a given volume resides on is removable (CM_DEVCAP_REMOVABLE). Looking for pointers on win32.

Clarify: What I am really trying to find out is w开发者_Python百科hether the disk that the volume resides on is connected on a port (eg. USB) that is external to the computer.


You can open the volume and issue IOCTL_STORAGE_QUERY_PROPERTY; this returns a STORAGE_DEVICE_DESCRIPTOR which has a RemovableMedia property. I believe this is the same as CM_DEVCAP_REMOVABLE (not 100% sure). At least it reports "fixed" USB flash drives as removable.

HANDLE hFile = CreateFile("\\\\.\\Z:", FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
    STORAGE_PROPERTY_QUERY StoragePropertyQuery;
    StoragePropertyQuery.PropertyId = StorageDeviceProperty;
    StoragePropertyQuery.QueryType = PropertyStandardQuery;
    BYTE Buffer[1024];
    if(DeviceIoControl(hFile, IOCTL_STORAGE_QUERY_PROPERTY, &SotragePropertyQuery, sizeof(STORAGE_PROPERTY_QUERY), Buffer, 1024, &BytesReturned, NULL))
    {
        PSTORAGE_DEVICE_DESCRIPTOR StorageDeviceDescriptor = (PSTORAGE_DEVICE_DESCRIPTOR)Buffer;
        if(StorageDeviceDescriptor->RemovableMedia)
        {
            printf("Z: IS removable\n");
        }
        else
        {
            printf("Z: is NOT removable\n");
        }
    }
    CloseHandle(hFile);
    hFile = INVALID_HANDLE_VALUE;
}


GetDriveType

Stack Overflow rejects GetDriveType because GetDriveType is only 12 characters long. However, Stack Overflow accepts the combination of GetDriveType with an accompanying complaint about Stack Overflow.

0

精彩评论

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

关注公众号