Advertisement
Guest User

Untitled

a guest
Aug 14th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. function GetSCSISerialNumber(DriveNumber: Byte): string;
  2. type
  3. TScsiPassThrough = packed record
  4. Length : Word;
  5. ScsiStatus : Byte;
  6. PathId : Byte;
  7. TargetId : Byte;
  8. Lun : Byte;
  9. CdbLength : Byte;
  10. SenseInfoLength : Byte;
  11. DataIn : Byte;
  12. DataTransferLength : ULONG;
  13. TimeOutValue : ULONG;
  14. DataBufferOffset : DWORD;
  15. SenseInfoOffset : ULONG;
  16. Cdb : array[0..15] of Byte;
  17. end;
  18. TScsiPassThroughWithBuffers = packed record
  19. spt : TScsiPassThrough;
  20. bSenseBuf : array[0..31] of Byte;
  21. bDataBuf : array[0..191] of Byte;
  22. end;
  23. var
  24. Len: DWORD;
  25. sLabel: string;
  26. hDevice: THandle;
  27. dwReturned: DWORD;
  28. aBuffer: array[0..SizeOf(TScsiPassThroughWithBuffers) +
  29. SizeOf(TScsiPassThrough) - 1] of Byte;
  30. Buffer: TScsiPassThroughWithBuffers absolute aBuffer;
  31. begin
  32. Result := '';
  33.  
  34. case DriveNumber of
  35. 0: sLabel := '\.PhysicalDrive0';
  36. 1: sLabel := '\.PhysicalDrive1';
  37. 2: sLabel := '\.PhysicalDrive2';
  38. 3: sLabel := '\.PhysicalDrive3';
  39. 4: sLabel := '\.PhysicalDrive4';
  40. 5: sLabel := '\.PhysicalDrive5';
  41. 6: sLabel := '\.PhysicalDrive6';
  42. 7: sLabel := '\.PhysicalDrive7';
  43. 8: sLabel := '\.PhysicalDrive8';
  44. else Exit;
  45. end;
  46.  
  47. if (Win32Platform = VER_PLATFORM_WIN32_NT)
  48. then
  49. { Win2K+ }
  50. {$IFDEF UNICODE} { Delphi XE }
  51. hDevice := CreateFile(PWideChar(sLabel), GENERIC_READ or GENERIC_WRITE,
  52. FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0) { MB_OK }
  53. {$ELSE} { Delphi 7 }
  54. hDevice := CreateFile(PAnsiChar(sLabel), GENERIC_READ or GENERIC_WRITE,
  55. FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0)
  56. {$ENDIF}
  57. else
  58. { Win95/98 }
  59. hDevice := CreateFile('\.SMARTVSD', 0, 0, nil, CREATE_NEW, 0, 0);
  60.  
  61. try
  62. if (hDevice <> INVALID_HANDLE_VALUE) then
  63. begin
  64. FillChar (aBuffer, SizeOf(aBuffer), #0);
  65. with Buffer.spt do
  66. begin
  67. Length := SizeOf(TScsiPassThrough);
  68. CdbLength := 6; // CDB6GENERIC_LENGTH
  69. SenseInfoLength := 24;
  70. DataIn := 1; // SCSI_IOCTL_DATA_IN
  71. DataTransferLength := 192;
  72. TimeOutValue := 2;
  73. DataBufferOffset := PChar(@Buffer.bDataBuf) - PChar(@Buffer);
  74. SenseInfoOffset := PChar(@Buffer.bSenseBuf) - PChar(@Buffer);
  75. Cdb[0] := $12; // OperationCode := SCSIOP_INQUIRY;
  76. Cdb[1] := $01; // Flags := CDB_INQUIRY_EVPD; Vital product data
  77. Cdb[2] := $80; // PageCode Unit serial number
  78. Cdb[4] := 192; // AllocationLength
  79. end;
  80. Len := Buffer.spt.DataBufferOffset + Buffer.spt.DataTransferLength;
  81. if DeviceIOControl(hDevice, $0004D004, @Buffer, SizeOf(TScsiPassThrough),
  82. @Buffer, Len, dwReturned, nil) and ((PChar(@Buffer.bDataBuf)+1)^ = #$80) then
  83. SetString(Result, PChar(@Buffer.bDataBuf) + 4, Ord((PChar(@Buffer.bDataBuf) + 3)^));
  84. end;
  85. finally
  86. CloseHandle(hDevice);
  87. end;
  88.  
  89. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement