IOCTL_STORAGE_QUERY_PROPERTY

最近学习scsi和DeviceIoControl,下载了微软WDK一些例子,以下代码精简自Windows-driver-samples-master\storage\tools\spti\src\spti.c ,略有修改。

#include 
#include 
#include 

void QueryPropertyForDevice(HANDLE);
void PrintDeviceDescriptor(PSTORAGE_DEVICE_DESCRIPTOR);

int main(int argc, char *argv[])
{
    if (argc != 2) {
       printf("Usage:  %s \n", argv[0] );
       printf("Examples:\n");
       printf("    %s g:       (PrintDeviceDescriptor)\n", argv[0]);
    }

    char filepath[7];
    StringCbPrintf(filepath, sizeof(filepath), "\\\\.\\%s", argv[1]);

    HANDLE hDevice = CreateFile(filepath,
       GENERIC_READ,
       FILE_SHARE_READ,
       NULL,
       OPEN_EXISTING,
       0,
       NULL);

    if (hDevice == INVALID_HANDLE_VALUE) {
        unsigned long errorCode = GetLastError();
        printf("Error opening %s. Error: %d\n",
               filepath, errorCode);
        return errorCode;
    }

    QueryPropertyForDevice(hDevice);
    CloseHandle(hDevice);

    return 0;
}



#define BOOLEAN_TO_STRING(_b_) \
( (_b_) ? "True" : "False" )

LPCSTR BusTypeStrings[] = {
    "Unknown",
    "Scsi",
    "Atapi",
    "Ata",
    "1394",
    "Ssa",
    "Fibre",
    "Usb",
    "RAID",
    "Not Defined",
};
#define NUMBER_OF_BUS_TYPE_STRINGS (sizeof(BusTypeStrings)/sizeof(BusTypeStrings[0]))

void QueryPropertyForDevice(HANDLE hDevice)
{
    bool result;
    DWORD readed;

    STORAGE_DESCRIPTOR_HEADER *DeviceDescriptorHeader;
    STORAGE_DEVICE_DESCRIPTOR *DeviceDescriptor;
    DWORD devDescLength;
    STORAGE_PROPERTY_QUERY query;

    query.PropertyId = StorageDeviceProperty;
    query.QueryType = PropertyStandardQuery;

    DeviceDescriptorHeader = (STORAGE_DESCRIPTOR_HEADER *) malloc( sizeof(STORAGE_DESCRIPTOR_HEADER) );
    
    result = DeviceIoControl( hDevice,     // device to be queried
        IOCTL_STORAGE_QUERY_PROPERTY,     // operation to perform
        &query, sizeof query,               // no input buffer
        DeviceDescriptorHeader, sizeof(STORAGE_DESCRIPTOR_HEADER),     // output buffer
        &readed,                 // # bytes returned
        NULL );      // synchronous I/O

    devDescLength = DeviceDescriptorHeader->Size;
    DeviceDescriptor = (STORAGE_DEVICE_DESCRIPTOR *) malloc( devDescLength );

    result = DeviceIoControl( hDevice,     // device to be queried
        IOCTL_STORAGE_QUERY_PROPERTY,     // operation to perform
        &query, sizeof query,               // no input buffer
        DeviceDescriptor, devDescLength,     // output buffer
        &readed,                 // # bytes returned
        NULL );      // synchronous I/O

    PrintDeviceDescriptor(DeviceDescriptor);

    free( DeviceDescriptorHeader );
    free( DeviceDescriptor );
}

void PrintDeviceDescriptor(PSTORAGE_DEVICE_DESCRIPTOR DeviceDescriptor)
{
    LPCSTR vendorId = "";
    LPCSTR productId = "";
    LPCSTR productRevision = "";
    LPCSTR serialNumber = "";
    LPCSTR busType;

    if ((ULONG)DeviceDescriptor->BusType < NUMBER_OF_BUS_TYPE_STRINGS) {
        busType = BusTypeStrings[DeviceDescriptor->BusType];
    } else {
        busType = BusTypeStrings[NUMBER_OF_BUS_TYPE_STRINGS-1];
    }

    if ((DeviceDescriptor->ProductIdOffset != 0) &&
        (DeviceDescriptor->ProductIdOffset != -1)) {
        productId        = (LPCSTR)(DeviceDescriptor);
        productId       += (ULONG_PTR)DeviceDescriptor->ProductIdOffset;
    }
    if ((DeviceDescriptor->VendorIdOffset != 0) &&
        (DeviceDescriptor->VendorIdOffset != -1)) {
        vendorId         = (LPCSTR)(DeviceDescriptor);
        vendorId        += (ULONG_PTR)DeviceDescriptor->VendorIdOffset;
    }
    if ((DeviceDescriptor->ProductRevisionOffset != 0) &&
        (DeviceDescriptor->ProductRevisionOffset != -1)) {
        productRevision  = (LPCSTR)(DeviceDescriptor);
        productRevision += (ULONG_PTR)DeviceDescriptor->ProductRevisionOffset;
    }
    if ((DeviceDescriptor->SerialNumberOffset != 0) &&
        (DeviceDescriptor->SerialNumberOffset != -1)) {
        serialNumber     = (LPCSTR)(DeviceDescriptor);
        serialNumber    += (ULONG_PTR)DeviceDescriptor->SerialNumberOffset;
    }

    puts("\n            ***** STORAGE DEVICE DESCRIPTOR DATA *****");
    printf("              Version: %08x\n"
           "            TotalSize: %08x\n"
           "           DeviceType: %08x\n"
           "   DeviceTypeModifier: %08x\n"
           "       RemovableMedia: %s\n"
           "      CommandQueueing: %s\n"
           "            Vendor Id: %s\n"
           "           Product Id: %s\n"
           "     Product Revision: %s\n"
           "        Serial Number: %s\n"
           "             Bus Type: %s\n",
           DeviceDescriptor->Version,
           DeviceDescriptor->Size,
           DeviceDescriptor->DeviceType,
           DeviceDescriptor->DeviceTypeModifier,
           BOOLEAN_TO_STRING(DeviceDescriptor->RemovableMedia),
           BOOLEAN_TO_STRING(DeviceDescriptor->CommandQueueing),
           vendorId,
           productId,
           productRevision,
           serialNumber,
           busType);
    
    printf("\n");
}

参考链接:

https://www.cnblogs.com/shangdawei/p/3164005.html

转载于:https://www.cnblogs.com/sherlock-merlin/p/11074666.html

你可能感兴趣的:(IOCTL_STORAGE_QUERY_PROPERTY)