Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Get serial number of a disk using SCSI interface



Permlink Replies: 0 Threads: [ Previous | Next ]
Fabio Pardini J...

Posts: 1
Registered: 4/5/13
Get serial number of a disk using SCSI interface
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 15, 2015 9:01 PM
Dear experts,

I created a function based on some examples found on Google, to extract the serial number of a disk using SCSI interface. But all my disks use IDE interface, so I can't test the function. Please, can someone test it for me? Thanks!

function GetSCSISerialNumber(DriveNumber: Byte): string; 
type 
  TScsiPassThrough = packed record 
    Length             : Word; 
    ScsiStatus         : Byte; 
    PathId             : Byte; 
    TargetId           : Byte; 
    Lun                : Byte; 
    CdbLength          : Byte; 
    SenseInfoLength    : Byte; 
    DataIn             : Byte; 
    DataTransferLength : ULONG; 
    TimeOutValue       : ULONG; 
    DataBufferOffset   : DWORD; 
    SenseInfoOffset    : ULONG; 
    Cdb                : array[0..15] of Byte; 
  end; 
  TScsiPassThroughWithBuffers = packed record 
    spt                : TScsiPassThrough; 
    bSenseBuf          : array[0..31] of Byte; 
    bDataBuf           : array[0..191] of Byte; 
  end; 
var 
  Len: DWORD; 
  sLabel: string; 
  hDevice: THandle; 
  dwReturned: DWORD; 
  aBuffer: array[0..SizeOf(TScsiPassThroughWithBuffers) + 
    SizeOf(TScsiPassThrough) - 1] of Byte; 
  Buffer: TScsiPassThroughWithBuffers absolute aBuffer; 
begin 
  Result := ''; 
 
  case DriveNumber of 
    0: sLabel := '\\.\PhysicalDrive0'; 
    1: sLabel := '\\.\PhysicalDrive1'; 
    2: sLabel := '\\.\PhysicalDrive2'; 
    3: sLabel := '\\.\PhysicalDrive3'; 
    4: sLabel := '\\.\PhysicalDrive4'; 
    5: sLabel := '\\.\PhysicalDrive5'; 
    6: sLabel := '\\.\PhysicalDrive6'; 
    7: sLabel := '\\.\PhysicalDrive7'; 
    8: sLabel := '\\.\PhysicalDrive8'; 
    else Exit; 
  end; 
 
  if (Win32Platform = VER_PLATFORM_WIN32_NT) 
  then 
    { Win2K+ } 
    {$IFDEF UNICODE} { Delphi XE } 
    hDevice := CreateFile(PWideChar(sLabel), GENERIC_READ or GENERIC_WRITE, 
      FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0) { MB_OK } 
    {$ELSE} { Delphi 7 } 
    hDevice := CreateFile(PAnsiChar(sLabel), GENERIC_READ or GENERIC_WRITE, 
      FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0) 
    {$ENDIF} 
  else 
    { Win95/98 } 
    hDevice := CreateFile('\\.\SMARTVSD', 0, 0, nil, CREATE_NEW, 0, 0); 
 
  try 
    if (hDevice <> INVALID_HANDLE_VALUE) then 
    begin 
      FillChar (aBuffer, SizeOf(aBuffer), #0); 
      with Buffer.spt do 
      begin 
        Length := SizeOf(TScsiPassThrough); 
        CdbLength := 6; // CDB6GENERIC_LENGTH 
        SenseInfoLength := 24; 
        DataIn := 1; // SCSI_IOCTL_DATA_IN 
        DataTransferLength := 192; 
        TimeOutValue := 2; 
        DataBufferOffset := PChar(@Buffer.bDataBuf) - PChar(@Buffer); 
        SenseInfoOffset := PChar(@Buffer.bSenseBuf) - PChar(@Buffer); 
        Cdb[0] := $12; // OperationCode := SCSIOP_INQUIRY; 
        Cdb[1] := $01; // Flags := CDB_INQUIRY_EVPD;  Vital product data 
        Cdb[2] := $80; // PageCode Unit serial number 
        Cdb[4] := 192; // AllocationLength 
      end; 
      Len := Buffer.spt.DataBufferOffset + Buffer.spt.DataTransferLength; 
      if DeviceIOControl(hDevice, $0004D004, @Buffer, SizeOf(TScsiPassThrough), 
        @Buffer, Len, dwReturned, nil) and ((PChar(@Buffer.bDataBuf)+1)^ = #$80) then 
        SetString(Result, PChar(@Buffer.bDataBuf) + 4, Ord((PChar(@Buffer.bDataBuf) + 3)^)); 
    end; 
  finally 
    CloseHandle(hDevice); 
  end; 
 
end;
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02