October 22nd, 2020

Taking a shortcut: You can query properties from a volume, and it will forward to the physical drive

If you have the handle to a volume, you can issue certain disk ioctls to the volume, and it will forward them to the underlying disk. We saw this earlier when we used IOCTL_STORAGE_GET_DEVICE_NUMBER to obtain the physical drive number from a volume. The name of the ioctl is IOCTL_STORAGE, but we issued it against a volume anyway.

And as we saw earlier, if the volume does not have a unique physical disk, then the call will fail.

This feature is particularly handy with storage property queries. For example, you can ask what how the drive is connected to the system by querying the volume:

wil::unique_hfile volume = GetVolumeHandleForFile(L"C:\\");

STORAGE_PROPERTY_QUERY query{};
query.PropertyId = StorageAdapterProperty;
query.QueryType = PropertyStandardQuery;
DWORD bytesWritten;
STORAGE_ADAPTER_DESCRIPTOR result{};

if (DeviceIoControl(volume.get(), IOCTL_STORAGE_QUERY_PROPERTY,
    &query, sizeof(query),
    &result, sizeof(result),
    &bytesWritten, nullptr)) {
    /* result.BusType tells you how the drive is connected */
}

Next time, we’ll use this to answer a commonly-asked question.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Henke37

    On the topic of shortcuts, QueryDosDevice seems like one.