-1

你好有人可以帮助我如何获取驱动器/硬件//'C:\''D:\'的名称......

例子

NameDrive function (const sDrive: string): string;
begin
  result: = GetDriveName (sDrive);
end;

Showmessage (NameDrive ('C: \')) / / = ST500DM0 ....

感谢您的关注。

4

1 回答 1

2

可以通过使用 IOCTL_STORAGE_QUERY_PROPERTY 代码(STORAGE_PROPERTY_QUERY PropertyId 设置为 StorageDeviceProperty)或 SMART_ 控制代码或读取 WMI 数据调用 DeviceIoControl 来获取供应商信息。在任何情况下,您都应该使用名称如 \.\PhysicalDriveX 而不是逻辑驱动器的物理驱动器。这是我一次可以看到的 3 个替代方案。

方法 1 的示例(取自 DDK 的类型定义):

type
STORAGE_PROPERTY_ID  = (
                          StorageDeviceProperty,
                          StorageAdapterProperty,
                          StorageDeviceIdProperty,
                          StorageDeviceUniqueIdProperty,              // See storduid.h for details
                          StorageDeviceWriteCacheProperty,
                          StorageMiniportProperty,
                          StorageAccessAlignmentProperty,
                          StorageDeviceSeekPenaltyProperty,
                          StorageDeviceTrimProperty,
                          StorageDeviceWriteAggregationProperty
                      );

    STORAGE_QUERY_TYPE  = (
                          PropertyStandardQuery,          // Retrieves the descriptor
                          PropertyExistsQuery,                // Used to test whether the descriptor is supported
                          PropertyMaskQuery,                  // Used to retrieve a mask of writeable fields in the descriptor
                          PropertyQueryMaxDefined     // use to validate the value
                      );

    STORAGE_PROPERTY_QUERY  = packed record
                         PropertyId,                                          // ID of the property being retrieved
                         QueryType      : Cardinal;         // Flags indicating the type of query being performed
                         AdditionalParameters : array [0..0] of AnsiChar; // Space for additional parameters if necessary
                    end;

    STORAGE_DEVICE_DESCRIPTOR = packed record
                        Version,
                        Size                     : Cardinal;
                        DeviceType               : Byte;
                        DeviceTypeModifier       : Byte;
                        RemovableMedia,
                        CommandQueueing          : Byte;
                        VendorIdOffset,
                        ProductIdOffset,
                        ProductRevisionOffset,
                        SerialNumberOffset       : Cardinal;
                        BusType                  : Cardinal;
                        RawPropertiesLength      : Cardinal;
                        RawDeviceProperties      : array [0..0] of AnsiChar;
                  end;

const STORAGE_PROPERTY_QUERY_SIZE  = 12;



function GetDriveModel(DrvHandle : THandle; var aModel : AnsiString; out ErrCode : Integer) : Boolean;
 var PropQuery : STORAGE_PROPERTY_QUERY;
     PropResponse : STORAGE_DEVICE_DESCRIPTOR;
     PropResponseSize, i : Cardinal;
     Buf : PByte;

begin
    FillChar(PropQuery, STORAGE_PROPERTY_QUERY_SIZE, 0);
    PropQuery.PropertyId := Cardinal(StorageDeviceProperty);
    PropQuery.QueryType := Cardinal(PropertyStandardQuery);
    Buf := GetMemory(4096);
    Result := DeviceIoControl(DrvHandle, IOCTL_STORAGE_QUERY_PROPERTY, @PropQuery, STORAGE_PROPERTY_QUERY_SIZE, Buf, 4096, PropResponseSize, nil);
    ErrCode := GetLastError;
    if not Result then
      begin
        FreeMemory(buf);
        Exit;
      end;
    PropResponse := (PSTORAGE_DEVICE_DESCRIPTOR(Buf))^;


    if PropResponse.RawPropertiesLength <> 0 then
      begin
        aModel := '';
        if PropResponse.VendorIdOffset <> 0 then
          begin
            i := PropResponse.VendorIdOffset;
            while Buf[i] <> 0 do
              begin
                aModel := aModel + AnsiChar(Buf[i]);
                Inc(i);
              end;
          end;
        if PropResponse.ProductIdOffset <> 0 then
          begin
            aModel := aModel + ' ';
            i := PropResponse.ProductIdOffset;
            while Buf[i] <> 0 do
              begin
                aModel := aModel + AnsiChar(Buf[i]);
                Inc(i);
              end;
          end;

      end;
    FreeMemory(buf);
end;

应该以这种方式打开驱动器:

Result := CreateFile(PWideChar(DrvName), GENERIC_READ or GENERIC_WRITE,
                          FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);

其中 DrvName 是例如系统中第一个驱动器的 \\.\PhysicalDrive0。一般来说,不可能完全按照您的意愿进行操作,因为单个卷(例如 D:\)可能是跨越多个物理磁盘的动态卷。

于 2013-05-19T05:28:07.350 回答