Maalesef, ben bir yapay zeka dil işlemesi modeliyim ve önceden yazdığım kodları hatırlayamam. Ancak, IOCTL_STORAGE_QUERY_PROPERTY ve SMART_RCV_DRIVE_DATA kullanarak bir kod yazmanıza yardımcı olabilirim.

Örnek bir kod parçası:

#include <windows.h>
#include <winioctl.h>
#include <stdio.h>

#define IDENTIFY_BUFFER_SIZE 512
#define IOCTL_STORAGE_QUERY_PROPERTY CTL_CODE(IOCTL_STORAGE_BASE, 0x0500, METHOD_BUFFERED, FILE_ANY_ACCESS)

#pragma pack(push, ident_data, 1)

typedef struct _IDENTIFY_DEVICE_DATA {
    USHORT  GeneralConfiguration;            // Word 0: 0 00h
    USHORT  NumberOfCylinders;                // Word 1: (3-2)
    USHORT  Reserved1;                        // Word 2: 0 00h
    USHORT  NumberOfHeads;                    // Word 3: (5-4)
    USHORT  UnformattedBytesPerTrack;        // Word 4: (7-6)
    USHORT  UnformattedBytesPerSector;        // Word 5: (9-8)
    USHORT  SectorsPerTrack;                // Word 6: (11-10)
    USHORT  VendorUnique1[3];                // Word 7-9: (13-21)
    CHAR    SerialNumber[20];                // Word 10-19: (23-42)
    USHORT  BufferType;                        // Word 20: (44-43)
    USHORT  BufferSectorSize;                // Word 21: (46-45)
    USHORT  NumberOfEccBytes;                // Word 22: (48-47)
    CHAR    FirmwareRevision[8];            // Word 23-26: (50-53)
    CHAR    ModelNumber[40];                // Word 27-46: (54-93)
    USHORT  MaximumBlockTransfer;            // Word 47: (95-94)
    USHORT  VendorUnique2;                    // Word 48: (97-96)
    USHORT  DoubleWordIo;                    // Word 49: (99-98)
    USHORT  Capabilities;                    // Word 50: (101-100)
    USHORT  Reserved2;                        // Word 51: (103-102)
    USHORT  VendorUnique3;                    // Word 52: (105-104)
    ULONG   TotalUserAddressableSectors;    // Word 53-54: (107-108)
    USHORT  VendorUnique4[2];                // Word 55-56: (110-111)
    USHORT  Reserved3;                        // Word 57: (113-112)
    UCHAR   MajorRevision;                    // Word 58: (114)
    UCHAR   MinorRevision;                    // Word 59: (115)
    USHORT  CommandSetSupported1;            // Word 60: (117-116)
    USHORT  CommandSetSupported2;            // Word 61: (119-118)
    USHORT  CommandSetSupported3;            // Word 62: (121-120)
    USHORT  CommandSetEnabled1;                // Word 63: (123-122)
    USHORT  CommandSetEnabled2;                // Word 64: (125-124)
    USHORT  CommandSetDefault;                // Word 65: (127-126)
    USHORT  UltraDMASupport;                // Word 66: (129-128)
    USHORT  UltraDMAActive;                    // Word 67: (131-130)
    USHORT  TimeRequiredForSecurityErase;    // Word 68: (133-132)
    USHORT  TimeRequiredForEnhancedSecurity; // Word 69: (135-134)
    USHORT  CurrentAPMLevel;                // Word 70: (137-136)
    USHORT  MasterPasswordRevision;        // Word 71: (139-138)
    USHORT  HardwareResetResult;            // Word 72: (141-140)
    USHORT  AcousticManagement;                // Word 73: (143-142)
    USHORT  VendorUnique5;                    // Word 74: (145-144)
    UCHAR   Reserved4[4];                    // Word 75-78: (147-150)
    USHORT  DeviceNominalFormFactor;         // Word 79: (152-151)
    USHORT  Reserved5;                        // Word 80: (154-153)
    USHORT  TransportMajorVersion;           // Word 81: (156-155)
    USHORT  TransportMinorVersion;           // Word 82: (158-157)
    USHORT  Reserved6[6];                    // Word 83-88: (160-167)
    ULONG   ExtendedUserAddressableSectors[2];// Word 89-100: (169-200)
    USHORT  MinBlocksPerDownloadMicrocode;   // Word 101: (202-201)
    USHORT  MaxBlocksPerDownloadMicrocode;   // Word 102: (204-203)
    USHORT  Reserved7[19];                    // Word 103-121: (206-243)
    USHORT  Integrity;                        // Word 122: (245-244)
} IDENTIFY_DEVICE_DATA, *PIDENTIFY_DEVICE_DATA;

typedef struct _STORAGE_PROPERTY_QUERY {
    STORAGE_PROPERTY_ID PropertyId;
    STORAGE_QUERY_TYPE QueryType;
    UCHAR AdditionalParameters[1];
} STORAGE_PROPERTY_QUERY, *PSTORAGE_PROPERTY_QUERY;

typedef struct _STORAGE_DESCRIPTOR_HEADER {
    ULONG Version;
    ULONG Size;
} STORAGE_DESCRIPTOR_HEADER, *PSTORAGE_DESCRIPTOR_HEADER;

typedef struct _STORAGE_DEVICE_DESCRIPTOR {
    ULONG Version;
    ULONG Size;
    UCHAR DeviceType;
    UCHAR DeviceTypeModifier;
    BOOLEAN RemovableMedia;
    BOOLEAN CommandQueueing;
    ULONG VendorIdOffset;
    ULONG ProductIdOffset;
    ULONG ProductRevisionOffset;
    ULONG SerialNumberOffset;
    STORAGE_BUS_TYPE BusType;
    ULONG RawPropertiesLength;
    UCHAR RawDeviceProperties[1];
} STORAGE_DEVICE_DESCRIPTOR, *PSTORAGE_DEVICE_DESCRIPTOR;

#pragma pack(pop, ident_data)

int main(int argc, char** argv)
{
    HANDLE hDevice = INVALID_HANDLE_VALUE;
    DWORD dwBytesReturned = 0;
    BOOL bResult = FALSE;
    STORAGE_PROPERTY_QUERY query = {0};
    PSTORAGE_DEVICE_DESCRIPTOR pDevDesc = NULL;
    PSTORAGE_DESCRIPTOR_HEADER pDescHdr = NULL;
    PIDENTIFY_DEVICE_DATA pIdentData = NULL;
    UCHAR identifyDataBuffer[IDENTIFY_BUFFER_SIZE] = {0};
    DWORD dwBufSize = 0;
    DWORD dwLastError = 0;
    UCHAR scsiAddr[256] = {0};
    UCHAR driveNum = 0;

    // get the SCSI address of the drive
    bResult = QueryDosDevice('C:', scsiAddr, 256);
    if (!bResult)
    {
        printf('QueryDosDevice failed with error %d\n', GetLastError());
        return 1;
    }

    // find the drive number in the SCSI address
    for (size_t i = 0; i < strlen(scsiAddr); i++)
    {
        if (scsiAddr[i] >= '0' && scsiAddr[i] <= '9')
        {
            driveNum = scsiAddr[i] - '0';
            break;
        }
    }

    // open the physical drive
    char drivePath[64];
    sprintf(drivePath, '\\.\PhysicalDrive%d', driveNum);
    hDevice = CreateFile(drivePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
    if (hDevice == INVALID_HANDLE_VALUE)
    {
        printf('CreateFile failed with error %d\n', GetLastError());
        return 1;
    }

    // query the device descriptor to get the required buffer size
    query.PropertyId = StorageDeviceProperty;
    query.QueryType = PropertyStandardQuery;
    bResult = DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(STORAGE_PROPERTY_QUERY), NULL, 0, &dwBytesReturned, NULL);
    if (!bResult && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
    {
        printf('DeviceIoControl failed with error %d\n', GetLastError());
        return 1;
    }

    // allocate the required buffer and query the device descriptor again
    dwBufSize = dwBytesReturned;
    pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)malloc(dwBufSize);
    ZeroMemory(pDevDesc, dwBufSize);
    bResult = DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(STORAGE_PROPERTY_QUERY), pDevDesc, dwBufSize, &dwBytesReturned, NULL);
    if (!bResult)
    {
        printf('DeviceIoControl failed with error %d\n', GetLastError());
        return 1;
    }

    // query the storage adapter descriptor to get the required buffer size
    query.PropertyId = StorageAdapterProperty;
    query.QueryType = PropertyStandardQuery;
    bResult = DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(STORAGE_PROPERTY_QUERY), NULL, 0, &dwBytesReturned, NULL);
    if (!bResult && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
    {
        printf('DeviceIoControl failed with error %d\n', GetLastError());
        return 1;
    }

    // allocate the required buffer and query the storage adapter descriptor again
    dwBufSize = dwBytesReturned;
    pDescHdr = (PSTORAGE_DESCRIPTOR_HEADER)malloc(dwBufSize);
    ZeroMemory(pDescHdr, dwBufSize);
    bResult = DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(STORAGE_PROPERTY_QUERY), pDescHdr, dwBufSize, &dwBytesReturned, NULL);
    if (!bResult)
    {
        printf('DeviceIoControl failed with error %d\n', GetLastError());
        return 1;
    }

    // query the drive's IDENTIFY data using the SMART_RCV_DRIVE_DATA command
    bResult = DeviceIoControl(hDevice, SMART_RCV_DRIVE_DATA, NULL, 0, identifyDataBuffer, IDENTIFY_BUFFER_SIZE, &dwBytesReturned, NULL);
    if (!bResult)
    {
        printf('DeviceIoControl failed with error %d\n', GetLastError());
        return 1;
    }

    // cast the IDENTIFY data to the IDENTIFY_DEVICE_DATA structure
    pIdentData = (PIDENTIFY_DEVICE_DATA)identifyDataBuffer;

    // print some of the drive's information
    printf('Vendor ID: %.*s\n', 8, pIdentData->ModelNumber);
    printf('Product ID: %.*s\n', 16, pIdentData->ModelNumber + 8);
    printf('Serial Number: %.*s\n', 20, pIdentData->SerialNumber);

    // clean up
    free(pDevDesc);
    free(pDescHdr);
    CloseHandle(hDevice);

    return 0;
}

Bu kod, C sürücüsünde yer alan bir sabit disk sürücüsünün bilgilerini alır. IOCTL_STORAGE_QUERY_PROPERTY API'si kullanılarak aygıt özellikleri ve depolama adaptörü özellikleri sorgulanır. Ayrıca, SMART_RCV_DRIVE_DATA komutunu kullanarak, sürücüden IDENTIFY verileri alınır. Bu veriler, PIDENTIFY_DEVICE_DATA yapısına dönüştürülür ve sürücünün üretici kimliği, ürün kimliği ve seri numarası yazdırılır.

C++ ile Sabit Disk Özelliklerini Sorgulamak: IOCTL_STORAGE_QUERY_PROPERTY ve SMART_RCV_DRIVE_DATA

原文地址: https://www.cveoy.top/t/topic/lyYE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录