获取名称Drive Hardware

时间:2013-05-18 19:39:56

标签: windows delphi winapi

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

例如

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

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

感谢您的关注。

1 个答案:

答案 0 :(得分:1)

通过使用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:\可能是跨越多个物理磁盘的动态卷。