use DeviceIOControl go retrieve disk geometry


/ Published in: Perl
Save to your folder(s)

# A simple example to retrieve disk geometry via Device IO Control.
# I hard coded the disk number to //./PhysicalDrive0
# This script is a read-only operation to your hard disk. It has no influence to your hard disk.


Copy this code and paste it in your HTML
  1. #!/path/to/perl -w
  2.  
  3. #
  4. # Vinocui @ 2009.7.3
  5. # A simple example to retrieve disk geometry via Device IO Control.
  6. # I hard coded the disk number to //./PhysicalDrive0
  7. # This script is a read-only operation to your hard disk. It has no influence to your hard disk.
  8. #
  9.  
  10. # I shoulde use strict here. but i'm lazy...
  11.  
  12. use Win32API::File qw( :ALL );
  13.  
  14. %MediaType = (
  15. 0=>"Format is unknown",
  16. 1=>"A 5.25\" floppy, with 1.2MB and 512 bytes/sector.",
  17. 2=>"3.5\" floppy, with 1.44MB and 512 bytes/sector.",
  18. 3=>"A 3.5\" floppy, with 2.88MB and 512 bytes/sector.",
  19. 4=>"A 3.5\" floppy, with 20.8MB and 512 bytes/sector.",
  20. 5=>"A 3.5\" floppy, with 720KB and 512 bytes/sector.",
  21. 6=>"A 5.25\" floppy, with 360KB and 512 bytes/sector.",
  22. 7=>"A 5.25\" floppy, with 320KB and 512 bytes/sector.",
  23. 8=>"A 5.25\" floppy, with 320KB and 1024 bytes/sector.",
  24. 9=>"A 5.25\" floppy, with 180KB and 512 bytes/sector.",
  25. 10=>"A 5.25\" floppy, with 160KB and 512 bytes/sector.",
  26. 11=>"Removable media other than floppy.",
  27. 12=>"Fixed hard disk media.",
  28. 13=>"A 3.5\" floppy, with 120MB and 512 bytes/sector.",
  29. 14=>"A 3.5\" floppy, with 640KB and 512 bytes/sector.",
  30. 15=>"A 5.25\" floppy, with 640KB and 512 bytes/sector.",
  31. 16=>"A 5.25\" floppy, with 720KB and 512 bytes/sector.",
  32. 17=>"A 3.5\" floppy, with 1.2MB and 512 bytes/sector.",
  33. 18=>"A 3.5\" floppy, with 1.23MB and 1024 bytes/sector.",
  34. 19=>"A 5.25\" floppy, with 1.23MB and 1024 bytes/sector.",
  35. 20=>"A 3.5\" floppy, with 128MB and 512 bytes/sector.",
  36. 21=>"A 3.5\" floppy, with 230MB and 512 bytes/sector.",
  37. 22=>"An 8\" floppy, with 256KB and 128 bytes/sector.",
  38. 23=>"A 3.5\" floppy, with 200MB and 512 bytes/sector. (HiFD).",
  39. 24=>"A 3.5\" floppy, with 240MB and 512 bytes/sector. (HiFD).",
  40. 25=>"A 3.5\" floppy, with 32MB and 512 bytes/sector."
  41. );
  42.  
  43. $sPath = "//./PhysicalDrive1";
  44. $uAccess = GENERIC_READ;
  45. $uShare = FILE_SHARE_READ | FILE_SHARE_WRITE;
  46. $pSecAttr = [];
  47. $uCreate = OPEN_EXISTING;
  48. $uFlags = FILE_ATTRIBUTE_NORMAL;
  49. $hModel = [];
  50. $hObject = CreateFile( $sPath, $uAccess, $uShare, $pSecAttr, $uCreate, $uFlags, $hModel ) or die "can not open $sPath";
  51. my $opOutBuf = pack("L l I L L L", 0);
  52. $ret = DeviceIoControl( $hObject,
  53. IOCTL_DISK_GET_DRIVE_GEOMETRY,
  54. [],
  55. 0,
  56. $opOutBuf,
  57. length($opOutBuf),
  58. $olRetBytes,
  59. []) or die "Line ", __LINE__ ," DeviceIoControl: ", fileLastError(), "\n";
  60.  
  61. CloseHandle($hObject);
  62.  
  63. if ($ret) {
  64. print $olRetBytes, "Bytes returned.\n";
  65.  
  66. ( $ucCylsLow, $ivcCylsHigh, $uMediaType, $uTracksPerCyl,
  67. $uSectsPerTrack, $uBytesPerSect )= unpack( "L l I L L L", $opOutBuf );
  68.  
  69. print "The low-order 4 bytes of the total number of cylinders: ", $ucCylsLow, "\n";
  70. print "The high-order 4 bytes of the total number of cylinders:", $ivcCylsHigh, "\n";
  71. print "Media type:", $uMediaType, " ", $MediaType{$uMediaType}, "\n";
  72. print "The number of tracks in each cylinder:", $uTracksPerCyl, "\n";
  73. print "The number of sectors in each track:", $uSectsPerTrack, "\n";
  74. print "The number of bytes in each sector:", $uBytesPerSect, "\n";
  75. }else{
  76. print "Sorry buddy, i've tried my best :(\n";
  77. }
  78. 0;

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.