如何获得USB HDD的“IDENTIFY_DEVICE_DATA”?

时间:2017-07-17 09:52:15

标签: delphi usb ioctl

我正在编写一个读取SMART数据并更改驱动器的APM / AAM的HDD工具。我从内部磁盘开始,一切似乎都没问题。我得到了CreateFile\\.\PhysicalDriveX的句柄,并使用DeviceIoControlIOCTL_ATA_PASS_THROUGH来获取此类驱动器的IDENTIFY_DEVICE_DATA

function GetDriveHandle(Name:String; var hDrive:THandle):Boolean;
begin
 hDrive:=CreateFile(PChar(Name), GENERIC_READ or GENERIC_WRITE,
  FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
 Result:=hDrive<>INVALID_HANDLE_VALUE;
end;

function GetIdentify(hDrive:THandle; var Data:IDENTIFY_DEVICE_DATA):Boolean;
var atpexb: ATA_PASS_THROUGH_EX_WITH_BUFFERS;
    bytesRet: DWord;
begin
 bytesRet:=0;
 FillChar(atpexb,SizeOf(atpexb),0);
 atpexb.Length:=SizeOf(ATA_PASS_THROUGH_EX);
 atpexb.AtaFlags:=ATA_FLAGS_DATA_IN;
 atpexb.DataTransferLength:=512;
 atpexb.TimeOutValue:=3;
 atpexb.DataBufferOffset:=SizeOf(ATA_PASS_THROUGH_EX);
 atpexb.CurrentTaskFile.Command:=WIN_IDENTIFYDEVICE;
 atpexb.CurrentTaskFile.SectorCount:=1;
 Result:=DeviceIoControl(hDrive, IOCTL_ATA_PASS_THROUGH, @atpexb, SizeOf(ATA_PASS_THROUGH_EX),
  @atpexb, SizeOf(atpexb), bytesRet, nil);
 if Result then Move(atpexb.DataBuff, Data, SizeOf(Data));
end;

但如果我在USB硬盘上尝试这个,那就不行了。我得到“错误50:不支持请求”。好的,我在互联网上研究了更多,我发现我需要打开一个特殊的USB设备句柄:“\?\ usb#vid_1058&amp; pid_1078#575856314541353752543645#{a5dcbf10-6530-11d2-901f- 00c04fb951ed}”。我使用USB HDD的设备路径获取句柄,但当我尝试将其与DeviceIoControl/IOCTL_ATA_PASS_THROUGH一起使用时,我得到了同样的错误:“请求不受支持”。

0 个答案:

没有答案