Return to Snippet

Revision: 15387
at July 3, 2009 04:31 by vinocui


Initial Code
#!/path/to/perl -w

# 
# Vinocui @ 2009.7.3
# 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.
# 

# I shoulde use strict here. but i'm lazy...

use Win32API::File qw( :ALL );

%MediaType = (
    0=>"Format is unknown",
    1=>"A 5.25\" floppy, with 1.2MB and 512 bytes/sector.",
    2=>"3.5\" floppy, with 1.44MB and 512 bytes/sector.",
    3=>"A 3.5\" floppy, with 2.88MB and 512 bytes/sector.",
    4=>"A 3.5\" floppy, with 20.8MB and 512 bytes/sector.",
    5=>"A 3.5\" floppy, with 720KB and 512 bytes/sector.",
    6=>"A 5.25\" floppy, with 360KB and 512 bytes/sector.",
    7=>"A 5.25\" floppy, with 320KB and 512 bytes/sector.",
    8=>"A 5.25\" floppy, with 320KB and 1024 bytes/sector.",
    9=>"A 5.25\" floppy, with 180KB and 512 bytes/sector.",
    10=>"A 5.25\" floppy, with 160KB and 512 bytes/sector.",
    11=>"Removable media other than floppy.",
    12=>"Fixed hard disk media.",
    13=>"A 3.5\" floppy, with 120MB and 512 bytes/sector.",
    14=>"A 3.5\" floppy, with 640KB and 512 bytes/sector.",
    15=>"A 5.25\" floppy, with 640KB and 512 bytes/sector.",
    16=>"A 5.25\" floppy, with 720KB and 512 bytes/sector.",
    17=>"A 3.5\" floppy, with 1.2MB and 512 bytes/sector.",
    18=>"A 3.5\" floppy, with 1.23MB and 1024 bytes/sector.",
    19=>"A 5.25\" floppy, with 1.23MB and 1024 bytes/sector.",
    20=>"A 3.5\" floppy, with 128MB and 512 bytes/sector.",
    21=>"A 3.5\" floppy, with 230MB and 512 bytes/sector.",
    22=>"An 8\" floppy, with 256KB and 128 bytes/sector.",
    23=>"A 3.5\" floppy, with 200MB and 512 bytes/sector. (HiFD).",
    24=>"A 3.5\" floppy, with 240MB and 512 bytes/sector. (HiFD).",
    25=>"A 3.5\" floppy, with 32MB and 512 bytes/sector."
    );

$sPath = "//./PhysicalDrive1";
$uAccess = GENERIC_READ;
$uShare = FILE_SHARE_READ | FILE_SHARE_WRITE;
$pSecAttr = [];
$uCreate = OPEN_EXISTING;
$uFlags = FILE_ATTRIBUTE_NORMAL;
$hModel = [];
$hObject = CreateFile( $sPath, $uAccess, $uShare, $pSecAttr, $uCreate, $uFlags, $hModel ) or die "can not open $sPath";
my $opOutBuf = pack("L l I L L L", 0);
$ret = DeviceIoControl( $hObject,
			IOCTL_DISK_GET_DRIVE_GEOMETRY, 
			[], 
			0, 
			$opOutBuf, 
			length($opOutBuf), 
			$olRetBytes, 
			[]) or die "Line ", __LINE__ ," DeviceIoControl: ", fileLastError(), "\n";

CloseHandle($hObject);

if ($ret) {
    print $olRetBytes, "Bytes returned.\n";

    ( $ucCylsLow, $ivcCylsHigh, $uMediaType, $uTracksPerCyl,
      $uSectsPerTrack, $uBytesPerSect )= unpack( "L l I L L L", $opOutBuf );
    
    print "The low-order 4 bytes of the total number of cylinders: ", $ucCylsLow, "\n";
    print "The high-order 4 bytes of the total number of cylinders:", $ivcCylsHigh, "\n";
    print "Media type:", $uMediaType, " ", $MediaType{$uMediaType}, "\n";
    print "The number of tracks in each cylinder:", $uTracksPerCyl, "\n";
    print "The number of sectors in each track:", $uSectsPerTrack, "\n";
    print "The number of bytes in each sector:", $uBytesPerSect, "\n";
}else{
    print "Sorry buddy, i've tried my best :(\n";
}
0;

Initial URL


Initial Description
# 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.

Initial Title
use DeviceIOControl go retrieve disk geometry

Initial Tags


Initial Language
Perl