home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _d1ee939f14bfa5db8659e361142001cc < prev    next >
Text File  |  2004-06-01  |  86KB  |  2,493 lines

  1. # File.pm -- Low-level access to Win32 file/dir functions/constants.
  2.  
  3. package Win32API::File;
  4.  
  5. use strict;
  6. use Carp;
  7. use Fcntl qw( O_RDONLY O_RDWR O_WRONLY O_APPEND O_BINARY O_TEXT );
  8. use vars qw( $VERSION @ISA );
  9. use vars qw( @EXPORT @EXPORT_OK @EXPORT_FAIL %EXPORT_TAGS );
  10. $VERSION= '0.09';
  11.  
  12. use base qw( Exporter DynaLoader );
  13.  
  14. @EXPORT= qw();
  15. %EXPORT_TAGS= (
  16.     Func =>    [qw(        attrLetsToBits        createFile
  17.         fileConstant        fileLastError        getLogicalDrives
  18.     CloseHandle        CopyFile        CreateFile
  19.     DefineDosDevice        DeleteFile        DeviceIoControl
  20.     FdGetOsFHandle        GetDriveType        GetFileType
  21.     GetHandleInformation    GetLogicalDrives    GetLogicalDriveStrings
  22.     GetOsFHandle        GetVolumeInformation    IsRecognizedPartition
  23.     IsContainerPartition    MoveFile        MoveFileEx
  24.     OsFHandleOpen        OsFHandleOpenFd        QueryDosDevice
  25.     ReadFile        SetErrorMode        SetFilePointer
  26.     SetHandleInformation    WriteFile )],
  27.     FuncA =>    [qw(
  28.     CopyFileA        CreateFileA        DefineDosDeviceA
  29.     DeleteFileA        GetDriveTypeA        GetLogicalDriveStringsA
  30.     GetVolumeInformationA    MoveFileA        MoveFileExA
  31.     QueryDosDeviceA )],
  32.     FuncW =>    [qw(
  33.     CopyFileW        CreateFileW        DefineDosDeviceW
  34.     DeleteFileW        GetDriveTypeW        GetLogicalDriveStringsW
  35.     GetVolumeInformationW    MoveFileW        MoveFileExW
  36.     QueryDosDeviceW )],
  37.     Misc =>        [qw(
  38.     CREATE_ALWAYS        CREATE_NEW        FILE_BEGIN
  39.     FILE_CURRENT        FILE_END        INVALID_HANDLE_VALUE
  40.     OPEN_ALWAYS        OPEN_EXISTING        TRUNCATE_EXISTING )],
  41.     DDD_ =>    [qw(
  42.     DDD_EXACT_MATCH_ON_REMOVE            DDD_RAW_TARGET_PATH
  43.     DDD_REMOVE_DEFINITION )],
  44.     DRIVE_ =>    [qw(
  45.     DRIVE_UNKNOWN        DRIVE_NO_ROOT_DIR    DRIVE_REMOVABLE
  46.     DRIVE_FIXED        DRIVE_REMOTE        DRIVE_CDROM
  47.     DRIVE_RAMDISK )],
  48.     FILE_ =>    [qw(
  49.     FILE_READ_DATA            FILE_LIST_DIRECTORY
  50.     FILE_WRITE_DATA            FILE_ADD_FILE
  51.     FILE_APPEND_DATA        FILE_ADD_SUBDIRECTORY
  52.     FILE_CREATE_PIPE_INSTANCE    FILE_READ_EA
  53.     FILE_WRITE_EA            FILE_EXECUTE
  54.     FILE_TRAVERSE            FILE_DELETE_CHILD
  55.     FILE_READ_ATTRIBUTES        FILE_WRITE_ATTRIBUTES
  56.     FILE_ALL_ACCESS            FILE_GENERIC_READ
  57.     FILE_GENERIC_WRITE        FILE_GENERIC_EXECUTE )],
  58.     FILE_ATTRIBUTE_ =>    [qw(
  59.     FILE_ATTRIBUTE_ARCHIVE        FILE_ATTRIBUTE_COMPRESSED
  60.     FILE_ATTRIBUTE_HIDDEN        FILE_ATTRIBUTE_NORMAL
  61.     FILE_ATTRIBUTE_OFFLINE        FILE_ATTRIBUTE_READONLY
  62.     FILE_ATTRIBUTE_SYSTEM        FILE_ATTRIBUTE_TEMPORARY )],
  63.     FILE_FLAG_ =>    [qw(
  64.     FILE_FLAG_BACKUP_SEMANTICS    FILE_FLAG_DELETE_ON_CLOSE
  65.     FILE_FLAG_NO_BUFFERING        FILE_FLAG_OVERLAPPED
  66.     FILE_FLAG_POSIX_SEMANTICS    FILE_FLAG_RANDOM_ACCESS
  67.     FILE_FLAG_SEQUENTIAL_SCAN    FILE_FLAG_WRITE_THROUGH )],
  68.     FILE_SHARE_ =>    [qw(
  69.     FILE_SHARE_DELETE    FILE_SHARE_READ        FILE_SHARE_WRITE )],
  70.     FILE_TYPE_ =>    [qw(
  71.     FILE_TYPE_CHAR        FILE_TYPE_DISK        FILE_TYPE_PIPE
  72.     FILE_TYPE_UNKNOWN )],
  73.     FS_ =>    [qw(
  74.     FS_CASE_IS_PRESERVED        FS_CASE_SENSITIVE
  75.     FS_UNICODE_STORED_ON_DISK    FS_PERSISTENT_ACLS 
  76.     FS_FILE_COMPRESSION        FS_VOL_IS_COMPRESSED )],
  77.     HANDLE_FLAG_ =>    [qw(
  78.     HANDLE_FLAG_INHERIT        HANDLE_FLAG_PROTECT_FROM_CLOSE )],
  79.     IOCTL_STORAGE_ =>    [qw(
  80.     IOCTL_STORAGE_CHECK_VERIFY    IOCTL_STORAGE_MEDIA_REMOVAL
  81.     IOCTL_STORAGE_EJECT_MEDIA    IOCTL_STORAGE_LOAD_MEDIA
  82.     IOCTL_STORAGE_RESERVE        IOCTL_STORAGE_RELEASE
  83.     IOCTL_STORAGE_FIND_NEW_DEVICES    IOCTL_STORAGE_GET_MEDIA_TYPES
  84.     )],
  85.     IOCTL_DISK_ =>    [qw(
  86.     IOCTL_DISK_FORMAT_TRACKS    IOCTL_DISK_FORMAT_TRACKS_EX
  87.     IOCTL_DISK_GET_DRIVE_GEOMETRY    IOCTL_DISK_GET_DRIVE_LAYOUT
  88.     IOCTL_DISK_GET_MEDIA_TYPES    IOCTL_DISK_GET_PARTITION_INFO
  89.     IOCTL_DISK_HISTOGRAM_DATA    IOCTL_DISK_HISTOGRAM_RESET
  90.     IOCTL_DISK_HISTOGRAM_STRUCTURE    IOCTL_DISK_IS_WRITABLE
  91.     IOCTL_DISK_LOGGING        IOCTL_DISK_PERFORMANCE
  92.     IOCTL_DISK_REASSIGN_BLOCKS    IOCTL_DISK_REQUEST_DATA
  93.     IOCTL_DISK_REQUEST_STRUCTURE    IOCTL_DISK_SET_DRIVE_LAYOUT
  94.     IOCTL_DISK_SET_PARTITION_INFO    IOCTL_DISK_VERIFY )],
  95.     GENERIC_ =>        [qw(
  96.     GENERIC_ALL            GENERIC_EXECUTE
  97.     GENERIC_READ            GENERIC_WRITE )],
  98.     MEDIA_TYPE =>    [qw(
  99.     Unknown            F5_1Pt2_512        F3_1Pt44_512
  100.     F3_2Pt88_512        F3_20Pt8_512        F3_720_512
  101.     F5_360_512        F5_320_512        F5_320_1024
  102.     F5_180_512        F5_160_512        RemovableMedia
  103.     FixedMedia        F3_120M_512 )],
  104.     MOVEFILE_ =>    [qw(
  105.     MOVEFILE_COPY_ALLOWED        MOVEFILE_DELAY_UNTIL_REBOOT
  106.     MOVEFILE_REPLACE_EXISTING    MOVEFILE_WRITE_THROUGH )],
  107.     SECURITY_ =>    [qw(
  108.     SECURITY_ANONYMOUS        SECURITY_CONTEXT_TRACKING
  109.     SECURITY_DELEGATION        SECURITY_EFFECTIVE_ONLY
  110.     SECURITY_IDENTIFICATION        SECURITY_IMPERSONATION
  111.     SECURITY_SQOS_PRESENT )],
  112.     SEM_ =>        [qw(
  113.     SEM_FAILCRITICALERRORS        SEM_NOGPFAULTERRORBOX
  114.     SEM_NOALIGNMENTFAULTEXCEPT    SEM_NOOPENFILEERRORBOX )],
  115.     PARTITION_ =>    [qw(
  116.     PARTITION_ENTRY_UNUSED        PARTITION_FAT_12
  117.     PARTITION_XENIX_1        PARTITION_XENIX_2
  118.     PARTITION_FAT_16        PARTITION_EXTENDED
  119.     PARTITION_HUGE            PARTITION_IFS
  120.     PARTITION_FAT32            PARTITION_FAT32_XINT13
  121.     PARTITION_XINT13        PARTITION_XINT13_EXTENDED
  122.     PARTITION_PREP            PARTITION_UNIX
  123.     VALID_NTFT            PARTITION_NTFT )],
  124. );
  125. @EXPORT_OK= ();
  126. {
  127.     my $key;
  128.     foreach $key (  keys(%EXPORT_TAGS)  ) {
  129.     push( @EXPORT_OK, @{$EXPORT_TAGS{$key}} );
  130.     #push( @EXPORT_FAIL, @{$EXPORT_TAGS{$key}} )   unless  $key =~ /^Func/;
  131.     }
  132. }
  133. $EXPORT_TAGS{ALL}= \@EXPORT_OK;
  134.  
  135. bootstrap Win32API::File $VERSION;
  136.  
  137. # Preloaded methods go here.
  138.  
  139. # To convert C constants to Perl code in cFile.pc
  140. # [instead of C or C++ code in cFile.h]:
  141. #    * Modify F<Makefile.PL> to add WriteMakeFile() =>
  142. #      CONST2PERL/postamble => [[ "Win32API::File" => ]] WRITE_PERL => 1.
  143. #    * Either comment out C<#include "cFile.h"> from F<File.xs>
  144. #      or make F<cFile.h> an empty file.
  145. #    * Make sure the following C<if> block is not commented out.
  146. #    * "nmake clean", "perl Makefile.PL", "nmake"
  147.  
  148. if(  ! defined &GENERIC_READ  ) {
  149.     require "Win32API/File/cFile.pc";
  150. }
  151.  
  152. sub fileConstant
  153. {
  154.     my( $name )= @_;
  155.     if(  1 != @_  ||  ! $name  ||  $name =~ /\W/  ) {
  156.     require Carp;
  157.     Carp::croak( 'Usage: ',__PACKAGE__,'::fileConstant("CONST_NAME")' );
  158.     }
  159.     my $proto= prototype $name;
  160.     if(  defined \&$name
  161.      &&  defined $proto
  162.      &&  "" eq $proto  ) {
  163.     no strict 'refs';
  164.     return &$name;
  165.     }
  166.     return undef;
  167. }
  168.  
  169. # We provide this for backwards compatibility:
  170. sub constant
  171. {
  172.     my( $name )= @_;
  173.     my $value= fileConstant( $name );
  174.     if(  defined $value  ) {
  175.     $!= 0;
  176.     return $value;
  177.     }
  178.     $!= 11; # EINVAL
  179.     return 0;
  180. }
  181.  
  182. BEGIN {
  183.     my $code= 'return _fileLastError(@_)';
  184.     local( $!, $^E )= ( 1, 1 );
  185.     if(  $! ne $^E  ) {
  186.     $code= '
  187.         local( $^E )= _fileLastError(@_);
  188.         my $ret= $^E;
  189.         return $ret;
  190.     ';
  191.     }
  192.     eval "sub fileLastError { $code }";
  193.     die "$@"   if  $@;
  194. }
  195.  
  196. # Since we ISA DynaLoader which ISA AutoLoader, we ISA AutoLoader so we
  197. # need this next chunk to prevent Win32API::File->nonesuch() from
  198. # looking for "nonesuch.al" and producing confusing error messages:
  199. use vars qw($AUTOLOAD);
  200. sub AUTOLOAD {
  201.     require Carp;
  202.     Carp::croak(
  203.       "Can't locate method $AUTOLOAD via package Win32API::File" );
  204. }
  205.  
  206. # Replace "&rout;" with "goto &rout;" when that is supported on Win32.
  207.  
  208. # Aliases for non-Unicode functions:
  209. sub CopyFile            { &CopyFileA; }
  210. sub CreateFile            { &CreateFileA; }
  211. sub DefineDosDevice        { &DefineDosDeviceA; }
  212. sub DeleteFile            { &DeleteFileA; }
  213. sub GetDriveType        { &GetDriveTypeA; }
  214. sub GetLogicalDriveStrings    { &GetLogicalDriveStringsA; }
  215. sub GetVolumeInformation    { &GetVolumeInformationA; }
  216. sub MoveFile            { &MoveFileA; }
  217. sub MoveFileEx            { &MoveFileExA; }
  218. sub QueryDosDevice        { &QueryDosDeviceA; }
  219.  
  220. sub OsFHandleOpen {
  221.     if(  3 != @_  ) {
  222.     croak 'Win32API::File Usage:  ',
  223.           'OsFHandleOpen(FILE,$hNativeHandle,"rwatb")';
  224.     }
  225.     my( $fh, $osfh, $access )= @_;
  226.     if(  ! ref($fh)  ) {
  227.     if(  $fh !~ /('|::)/  ) {
  228.         $fh= caller() . "::" . $fh;
  229.     }
  230.     no strict "refs";
  231.     $fh= \*{$fh};
  232.     }
  233.     my( $mode, $pref );
  234.     if(  $access =~ /r/i  ) {
  235.     if(  $access =~ /w/i  ) {
  236.         $mode= O_RDWR;
  237.         $pref= "+<";
  238.     } else {
  239.         $mode= O_RDONLY;
  240.         $pref= "<";
  241.     }
  242.     } else {
  243.     if(  $access =~ /w/i  ) {
  244.         $mode= O_WRONLY;
  245.         $pref= ">";
  246.     } else {
  247.     #   croak qq<Win32API::File::OsFHandleOpen():  >,
  248.     #      qq<Access ($access) missing both "r" and "w">;
  249.         $mode= O_RDONLY;
  250.         $pref= "<";
  251.     }
  252.     }
  253.     $mode |= O_APPEND   if  $access =~ /a/i;
  254.     #$mode |= O_TEXT   if  $access =~ /t/i;
  255.     # Some versions of the Fcntl module are broken and won't autoload O_TEXT:
  256.     if(  $access =~ /t/i  ) {
  257.     my $o_text= eval "O_TEXT";
  258.     $o_text= 0x4000   if  $@;
  259.     $mode |= $o_text;
  260.     }
  261.     $mode |= O_BINARY   if  $access =~ /b/i;
  262.     my $fd= OsFHandleOpenFd( $osfh, $mode );
  263.     return  undef   if  $fd < 0;
  264.     return  open( $fh, $pref."&=".$fd );
  265. }
  266.  
  267. sub GetOsFHandle {
  268.     if(  1 != @_  ) {
  269.     croak 'Win32API::File Usage:  $OsFHandle= GetOsFHandle(FILE)';
  270.     }
  271.     my( $file )= @_;
  272.     if(  ! ref($file)  ) {
  273.     if(  $file !~ /('|::)/  ) {
  274.         $file= caller() . "::" . $file;
  275.     }
  276.     no strict "refs";
  277.     $file= \*{$file};
  278.     }
  279.     my( $fd )= fileno($file);
  280.     if(  ! defined( $fd )  ) {
  281.     if(  $file =~ /^\d+\Z/  ) {
  282.         $fd= $file;
  283.     } else {
  284.         return ();    # $! should be set by fileno().
  285.     }
  286.     }
  287.     my $h= FdGetOsFHandle( $fd );
  288.     if(  INVALID_HANDLE_VALUE() == $h  ) {
  289.     $h= "";
  290.     } elsif(  "0" eq $h  ) {
  291.     $h= "0 but true";
  292.     }
  293.     return $h;
  294. }
  295.  
  296. sub attrLetsToBits
  297. {
  298.     my( $lets )= @_;
  299.     my( %a )= (
  300.       "a"=>FILE_ATTRIBUTE_ARCHIVE(),    "c"=>FILE_ATTRIBUTE_COMPRESSED(),
  301.       "h"=>FILE_ATTRIBUTE_HIDDEN(),    "o"=>FILE_ATTRIBUTE_OFFLINE(),
  302.       "r"=>FILE_ATTRIBUTE_READONLY(),    "s"=>FILE_ATTRIBUTE_SYSTEM(),
  303.       "t"=>FILE_ATTRIBUTE_TEMPORARY() );
  304.     my( $bits )= 0;
  305.     foreach(  split(//,$lets)  ) {
  306.     croak "Win32API::File::attrLetsToBits: Unknown attribute letter ($_)"
  307.       unless  exists $a{$_};
  308.     $bits |= $a{$_};
  309.     }
  310.     return $bits;
  311. }
  312.  
  313. use vars qw( @_createFile_Opts %_createFile_Opts );
  314. @_createFile_Opts= qw( Access Create Share Attributes
  315.                Flags Security Model );
  316. @_createFile_Opts{@_createFile_Opts}= (1) x @_createFile_Opts;
  317.  
  318. sub createFile
  319. {
  320.     my $opts= "";
  321.     if(  2 <= @_  &&  "HASH" eq ref($_[$#_])  ) {
  322.     $opts= pop( @_ );
  323.     }
  324.     my( $sPath, $svAccess, $svShare )= @_;
  325.     if(  @_ < 1  ||  3 < @_  ) {
  326.     croak "Win32API::File::createFile() usage:  \$hObject= createFile(\n",
  327.           "  \$sPath, [\$svAccess_qrw_ktn_ce,[\$svShare_rwd,]]",
  328.           " [{Option=>\$Value}] )\n",
  329.           "    options: @_createFile_Opts\nCalled";
  330.     }
  331.     my( $create, $flags, $sec, $model )= ( "", 0, [], 0 );
  332.     if(  ref($opts)  ) {
  333.         my @err= grep( ! $_createFile_Opts{$_}, keys(%$opts) );
  334.     @err  and  croak "_createFile:  Invalid options (@err)";
  335.     $flags= $opts->{Flags}        if  exists( $opts->{Flags} );
  336.     $flags |= attrLetsToBits( $opts->{Attributes} )
  337.                     if  exists( $opts->{Attributes} );
  338.     $sec= $opts->{Security}        if  exists( $opts->{Security} );
  339.     $model= $opts->{Model}        if  exists( $opts->{Model} );
  340.     $svAccess= $opts->{Access}    if  exists( $opts->{Access} );
  341.     $create= $opts->{Create}    if  exists( $opts->{Create} );
  342.     $svShare= $opts->{Share}    if  exists( $opts->{Share} );
  343.     }
  344.     $svAccess= "r"        unless  defined($svAccess);
  345.     $svShare= "rw"        unless  defined($svShare);
  346.     if(  $svAccess =~ /^[qrw ktn ce]*$/i  ) {
  347.     ( my $c= $svAccess ) =~ tr/qrw QRW//d;
  348.     $create= $c   if  "" ne $c  &&  "" eq $create;
  349.     local( $_ )= $svAccess;
  350.     $svAccess= 0;
  351.     $svAccess |= GENERIC_READ()   if  /r/i;
  352.     $svAccess |= GENERIC_WRITE()   if  /w/i;
  353.     } elsif(  "?" eq $svAccess  ) {
  354.     croak
  355.       "Win32API::File::createFile:  \$svAccess can use the following:\n",
  356.           "    One or more of the following:\n",
  357.           "\tq -- Query access (same as 0)\n",
  358.           "\tr -- Read access (GENERIC_READ)\n",
  359.           "\tw -- Write access (GENERIC_WRITE)\n",
  360.           "    At most one of the following:\n",
  361.           "\tk -- Keep if exists\n",
  362.           "\tt -- Truncate if exists\n",
  363.           "\tn -- New file only (fail if file already exists)\n",
  364.           "    At most one of the following:\n",
  365.           "\tc -- Create if doesn't exist\n",
  366.           "\te -- Existing file only (fail if doesn't exist)\n",
  367.           "  ''   is the same as 'q  k e'\n",
  368.           "  'r'  is the same as 'r  k e'\n",
  369.           "  'w'  is the same as 'w  t c'\n",
  370.           "  'rw' is the same as 'rw k c'\n",
  371.           "  'rt' or 'rn' implies 'c'.\n",
  372.           "  Or \$svAccess can be numeric.\n", "Called from";
  373.     } elsif(  $svAccess == 0  &&  $svAccess !~ /^[-+.]*0/  ) {
  374.     croak "Win32API::File::createFile:  Invalid \$svAccess ($svAccess)";
  375.     }
  376.     if(  $create =~ /^[ktn ce]*$/  ) {
  377.         local( $_ )= $create;
  378.         my( $k, $t, $n, $c, $e )= ( scalar(/k/i), scalar(/t/i),
  379.       scalar(/n/i), scalar(/c/i), scalar(/e/i) );
  380.     if(  1 < $k + $t + $n  ) {
  381.         croak "Win32API::File::createFile: \$create must not use ",
  382.           qq<more than one of "k", "t", and "n" ($create)>;
  383.     }
  384.     if(  $c  &&  $e  ) {
  385.         croak "Win32API::File::createFile: \$create must not use ",
  386.           qq<both "c" and "e" ($create)>;
  387.     }
  388.     my $r= ( $svAccess & GENERIC_READ() ) == GENERIC_READ();
  389.     my $w= ( $svAccess & GENERIC_WRITE() ) == GENERIC_WRITE();
  390.     if(  ! $k  &&  ! $t  &&  ! $n  ) {
  391.         if(  $w  &&  ! $r  ) {        $t= 1;
  392.         } else {                $k= 1; }
  393.     }
  394.     if(  $k  ) {
  395.         if(  $c  ||  $w && ! $e  ) {    $create= OPEN_ALWAYS();
  396.         } else {                $create= OPEN_EXISTING(); }
  397.     } elsif(  $t  ) {
  398.         if(  $e  ) {            $create= TRUNCATE_EXISTING();
  399.         } else {                $create= CREATE_ALWAYS(); }
  400.     } else { # $n
  401.         if(  ! $e  ) {            $create= CREATE_NEW();
  402.         } else {
  403.         croak "Win32API::File::createFile: \$create must not use ",
  404.           qq<both "n" and "e" ($create)>;
  405.         }
  406.     }
  407.     } elsif(  "?" eq $create  ) {
  408.     croak 'Win32API::File::createFile: $create !~ /^[ktn ce]*$/;',
  409.           ' pass $svAccess as "?" for more information.';
  410.     } elsif(  $create == 0  &&  $create ne "0"  ) {
  411.     croak "Win32API::File::createFile: Invalid \$create ($create)";
  412.     }
  413.     if(  $svShare =~ /^[drw]*$/  ) {
  414.         my %s= ( "d"=>FILE_SHARE_DELETE(), "r"=>FILE_SHARE_READ(),
  415.              "w"=>FILE_SHARE_WRITE() );
  416.         my @s= split(//,$svShare);
  417.     $svShare= 0;
  418.     foreach( @s ) {
  419.         $svShare |= $s{$_};
  420.     }
  421.     } elsif(  $svShare == 0  &&  $svShare !~ /^[-+.]*0/  ) {
  422.     croak "Win32API::File::createFile: Invalid \$svShare ($svShare)";
  423.     }
  424.     return  CreateFileA(
  425.           $sPath, $svAccess, $svShare, $sec, $create, $flags, $model );
  426. }
  427.  
  428.  
  429. sub getLogicalDrives
  430. {
  431.     my( $ref )= @_;
  432.     my $s= "";
  433.     if(  ! GetLogicalDriveStringsA( 256, $s )  ) {
  434.     return undef;
  435.     }
  436.     if(  ! defined($ref)  ) {
  437.     return  split( /\0/, $s );
  438.     } elsif(  "ARRAY" ne ref($ref)  ) {
  439.     croak 'Usage:  C<@arr= getLogicalDrives()> ',
  440.           'or C<getLogicalDrives(\\@arr)>', "\n";
  441.     }
  442.     @$ref= split( /\0/, $s );
  443.     return $ref;
  444. }
  445.  
  446. # Autoload methods go after =cut, and are processed by the autosplit program.
  447.  
  448. 1;
  449. __END__
  450.  
  451. =head1 NAME
  452.  
  453. Win32API::File - Low-level access to Win32 system API calls for files/dirs.
  454.  
  455. =head1 SYNOPSIS
  456.  
  457.   use Win32API::File 0.08 qw( :ALL );
  458.  
  459.   MoveFile( $Source, $Destination )
  460.     or  die "Can't move $Source to $Destination: ",fileLastError(),"\n";
  461.   MoveFileEx( $Source, $Destination, MOVEFILE_REPLACE_EXISTING() )
  462.     or  die "Can't move $Source to $Destination: ",fileLastError(),"\n";
  463.   [...]
  464.  
  465. =head1 DESCRIPTION
  466.  
  467. This provides fairly low-level access to the Win32 System API
  468. calls dealing with files and directories.
  469.  
  470. To pass in C<NULL> as the pointer to an optional buffer, pass in
  471. an empty list reference, C<[]>.
  472.  
  473. Beyond raw access to the API calls and related constants, this module
  474. handles smart buffer allocation and translation of return codes.
  475.  
  476. All functions, unless otherwise noted, return a true value for success
  477. and a false value for failure and set C<$^E> on failure.
  478.  
  479. =head2 Exports
  480.  
  481. Nothing is exported by default.  The following tags can be used to
  482. have large sets of symbols exported:  C<":Func">, C<":FuncA">,
  483. C<":FuncW">, C<":Misc">, C<":DDD_">, C<":DRIVE_">, C<":FILE_">,
  484. C<":FILE_ATTRIBUTE_">, C<":FILE_FLAG_">, C<":FILE_SHARE_">,
  485. C<":FILE_TYPE_">, C<":FS_">, C<":HANDLE_FLAG_">, C<":IOCTL_STORAGE_">,
  486. C<":IOCTL_DISK_">, C<":GENERIC_">, C<":MEDIA_TYPE">, C<":MOVEFILE_">,
  487. C<":SECURITY_">, C<":SEM_">, and C<":PARTITION_">.
  488.  
  489. =over
  490.  
  491. =item C<":Func">
  492.  
  493. The basic function names: C<attrLetsToBits>,       C<createFile>,
  494. C<fileConstant>,          C<fileLastError>,        C<getLogicalDrives>,
  495. C<CloseHandle>,           C<CopyFile>,             C<CreateFile>,
  496. C<DefineDosDevice>,       C<DeleteFile>,           C<DeviceIoControl>,
  497. C<FdGetOsFHandle>,        C<GetDriveType>,         C<GetFileType>,
  498. C<GetHandleInformation>,  C<GetLogicalDrives>,     C<GetLogicalDriveStrings>,
  499. C<GetOsFHandle>,          C<GetVolumeInformation>, C<IsRecognizedPartition>,
  500. C<IsContainerPartition>,  C<MoveFile>,             C<MoveFileEx>,
  501. C<OsFHandleOpen>,         C<OsFHandleOpenFd>,      C<QueryDosDevice>,
  502. C<ReadFile>,              C<SetErrorMode>,         C<SetFilePointer>,
  503. C<SetHandleInformation>,  and                      C<WriteFile>.
  504.  
  505. =over
  506.  
  507. =item attrLetsToBits
  508.  
  509. =item C<$uBits= attrLetsToBits( $sAttributeLetters )>
  510.  
  511. Converts a string of file attribute letters into an unsigned value with
  512. the corresponding bits set.  C<$sAttributeLetters> should contain zero
  513. or more letters from C<"achorst">:
  514.  
  515. =over
  516.  
  517. =item C<"a">
  518.  
  519. C<FILE_ATTRIBUTE_ARCHIVE>
  520.  
  521. =item C<"c">
  522.  
  523. C<FILE_ATTRIBUTE_COMPRESSED>
  524.  
  525. =item C<"h">
  526.  
  527. C<FILE_ATTRIBUTE_HIDDEN>
  528.  
  529. =item C<"o">
  530.  
  531. C<FILE_ATTRIBUTE_OFFLINE>
  532.  
  533. =item C<"r">
  534.  
  535. C<FILE_ATTRIBUTE_READONLY>
  536.  
  537. =item C<"s">
  538.  
  539. C<FILE_ATTRIBUTE_SYSTEM>
  540.  
  541. =item C<"t">
  542.  
  543. C<FILE_ATTRIBUTE_TEMPORARY>
  544.  
  545. =back
  546.  
  547. =item createFile
  548.  
  549. =item C<$hObject= createFile( $sPath )>
  550.  
  551. =item C<$hObject= createFile( $sPath, $rvhvOptions )>
  552.  
  553. =item C<$hObject= createFile( $sPath, $svAccess )>
  554.  
  555. =item C<$hObject= createFile( $sPath, $svAccess, $rvhvOptions )>
  556.  
  557. =item C<$hObject= createFile( $sPath, $svAccess, $svShare )>
  558.  
  559. =item C<$hObject= createFile( $sPath, $svAccess, $svShare, $rvhvOptions )>
  560.  
  561. This is a Perl-friendly wrapper around C<CreateFile>.
  562.  
  563. On failure, C<$hObject> gets set to a false value and C<regLastError()>
  564. and C<$^E> are set to the reason for the failure.  Otherwise,
  565. C<$hObject> gets set to a Win32 native file handle which is alwasy
  566. a true value [returns C<"0 but true"> in the impossible(?) case of
  567. the handle having a value of C<0>].
  568.  
  569. C<$sPath> is the path to the file [or device, etc.] to be opened.  See
  570. C<CreateFile> for more information on possible special values for
  571. C<$sPath>.  
  572.  
  573. C<$svAccess> can be a number containing the bit mask representing
  574. the specific type(s) of access to the file that you desire.  See the
  575. C<$uAccess> parameter to C<CreateFile> for more information on these
  576. values.
  577.  
  578. More likely, C<$svAccess> is a string describing the generic type of
  579. access you desire and possibly the file creation options to use.  In
  580. this case, C<$svAccess> should contain zero or more characters from
  581. C<"qrw"> [access desired], zero or one character each from C<"ktn">
  582. and C<"ce">, and optional white space.  These letters stand for,
  583. respectively, "Query access", "Read access", "Write access", "Keep if
  584. exists", "Truncate if exists", "New file only", "Create if none", and
  585. "Existing file only".  Case is ignored.
  586.  
  587. You can pass in C<"?"> for C<$svAccess> to have an error message
  588. displayed summarizing its possible values.  This is very handy when
  589. doing on-the-fly programming using the Perl debugger:
  590.  
  591.     Win32API::File::createFile:  $svAccess can use the following:
  592.     One or more of the following:
  593.         q -- Query access (same as 0)
  594.         r -- Read access (GENERIC_READ)
  595.         w -- Write access (GENERIC_WRITE)
  596.     At most one of the following:
  597.         k -- Keep if exists
  598.         t -- Truncate if exists
  599.         n -- New file only (fail if file already exists)
  600.     At most one of the following:
  601.         c -- Create if doesn't exist
  602.         e -- Existing file only (fail if doesn't exist)
  603.       ''   is the same as 'q  k e'
  604.       'r'  is the same as 'r  k e'
  605.       'w'  is the same as 'w  t c'
  606.       'rw' is the same as 'rw k c'
  607.       'rt' or 'rn' implies 'c'.
  608.       Or $access can be numeric.
  609.  
  610. C<$svAccess> is designed to be "do what I mean", so you can skip
  611. the rest of its explanation unless you are interested in the complex
  612. details.  Note that, if you want write access to a device, you need
  613. to specify C<"k"> [and perhaps C<"e">, as in C<"w ke"> or C<"rw ke">]
  614. since Win32 suggests C<OPEN_EXISTING> be used when opening a device.
  615.  
  616. =over
  617.  
  618. =item C<"q"> 
  619.  
  620. Stands for "Query access".  This is really a no-op since you always have
  621. query access when you open a file.  You can specify C<"q"> to document
  622. that you plan to query the file [or device, etc.].  This is especially
  623. helpful when you don't want read nor write access since something like
  624. C<"q"> or C<"q ke"> may be easier to understand than just C<""> or C<"ke">.
  625.  
  626. =item C<"r">
  627.  
  628. Stands for "Read access".  Sets the C<GENERIC_READ> bit(s) in the
  629. C<$uAccess> that is passed to C<CreateFile>.  This is the default
  630. access if the C<$svAccess> parameter is missing [or if it is C<undef>
  631. and C<$rvhvOptions> doesn't specify an C<"Access"> option].
  632.  
  633. =item C<"w">
  634.  
  635. Stands for "Write access".  Sets the C<GENERIC_WRITE> bit(s) in the
  636. C<$uAccess> that is passed to C<CreateFile>.
  637.  
  638. =item C<"k">
  639.  
  640. Stands for "Keep if exists".  If the requested file exists, then it is
  641. opened.  This is the default unless C<GENERIC_WRITE> access has been
  642. requested but C<GENERIC_READ> access has not been requested.   Contrast
  643. with C<"t"> and C<"n">.
  644.  
  645. =item C<"t">
  646.  
  647. Stands for "Truncate if exists".  If the requested file exists, then
  648. it is truncated to zero length and then opened.  This is the default if
  649. C<GENERIC_WRITE> access has been requested and C<GENERIC_READ> access
  650. has not been requested.  Contrast with C<"k"> and C<"n">.
  651.  
  652. =item C<"n">
  653.  
  654. Stands for "New file only".  If the requested file exists, then it is
  655. not opened and the C<createFile> call fails.  Contrast with C<"k"> and
  656. C<"t">.  Can't be used with C<"e">.
  657.  
  658. =item C<"c">
  659.  
  660. Stands for "Create if none".  If the requested file does not
  661. exist, then it is created and then opened.  This is the default
  662. if C<GENERIC_WRITE> access has been requested or if C<"t"> or
  663. C<"n"> was specified.  Contrast with C<"e">.
  664.  
  665. =item C<"e">
  666.  
  667. Stands for "Existing file only".  If the requested file does not
  668. exist, then nothing is opened and the C<createFile> call fails.  This
  669. is the default unless C<GENERIC_WRITE> access has been requested or
  670. C<"t"> or C<"n"> was specified.   Contrast with C<"c">.   Can't be
  671. used with C<"n">.
  672.  
  673. =back
  674.  
  675. The characters from C<"ktn"> and C<"ce"> are combined to determine the
  676. what value for C<$uCreate> to pass to C<CreateFile> [unless overridden
  677. by C<$rvhvOptions>]:
  678.  
  679. =over
  680.  
  681. =item C<"kc">
  682.  
  683. C<OPEN_ALWAYS>
  684.  
  685. =item C<"ke">
  686.  
  687. C<OPEN_EXISTING>
  688.  
  689. =item C<"tc">
  690.  
  691. C<TRUNCATE_EXISTING>
  692.  
  693. =item C<"te">
  694.  
  695. C<CREATE_ALWAYS>
  696.  
  697. =item C<"nc">
  698.  
  699. C<CREATE_NEW>
  700.  
  701. =item C<"ne">
  702.  
  703. Illegal.
  704.  
  705. =back
  706.  
  707. C<$svShare> controls how the file is shared, that is, whether other
  708. processes can have read, write, and/or delete access to the file while
  709. we have it opened.  C<$svShare> will usually be a string containing zero
  710. or more characters from C<"rwd"> but can also be a numeric bit mask.
  711.  
  712. C<"r"> sets the C<FILE_SHARE_READ> bit which allows other processes to have
  713. read access to the file.  C<"w"> sets the C<FILE_SHARE_WRITE> bit which
  714. allows other processes to have write access to the file.  C<"d"> sets the
  715. C<FILE_SHARE_DELETE> bit which allows other processes to have delete access
  716. to the file [ignored under Windows 95].
  717.  
  718. The default for C<$svShare> is C<"rw"> which provides the same sharing as
  719. using regular perl C<open()>.
  720.  
  721. If another process currently has read, write, and/or delete access to
  722. the file and you don't allow that level of sharing, then your call to
  723. C<createFile> will fail.  If you requested read, write, and/or delete
  724. access and another process already has the file open but doesn't allow
  725. that level of sharing, then your call to C<createFile> will fail.  Once
  726. you have the file open, if another process tries to open it with read,
  727. write, and/or delete access and you don't allow that level of sharing,
  728. then that process won't be allowed to open the file.
  729.  
  730. C<$rvhvOptions> is a reference to a hash where any keys must be from
  731. the list C<qw( Access Create Share Attributes Flags Security Model )>.
  732. The meaning of the value depends on the key name, as described below.
  733. Any option values in C<$rvhvOptions> override the settings from
  734. C<$svAccess> and C<$svShare> if they conflict.
  735.  
  736. =over
  737.  
  738. =item Flags => $uFlags
  739.  
  740. C<$uFlags> is an unsigned value having any of the C<FILE_FLAG_*> or
  741. C<FILE_ATTRIBUTE_*> bits set.  Any C<FILE_ATTRIBUTE_*> bits set via the
  742. C<Attributes> option are logically C<or>ed with these bits.  Defaults
  743. to C<0>.
  744.  
  745. If opening the client side of a named pipe, then you can also specify
  746. C<SECURITY_SQOS_PRESENT> along with one of the other C<SECURITY_*>
  747. constants to specify the security quality of service to be used.
  748.  
  749. =item Attributes => $sAttributes
  750.  
  751. A string of zero or more characters from C<"achorst"> [see C<attrLetsToBits>
  752. for more information] which are converted to C<FILE_ATTRIBUTE_*> bits to
  753. be set in the C<$uFlags> argument passed to C<CreateFile>.
  754.  
  755. =item Security => $pSecurityAttributes
  756.  
  757. C<$pSecurityAttributes> should contain a C<SECURITY_ATTRIBUTES> structure
  758. packed into a string or C<[]> [the default].
  759.  
  760. =item Model => $hModelFile
  761.  
  762. C<$hModelFile> should contain a handle opened with C<GENERIC_READ>
  763. access to a model file from which file attributes and extended attributes
  764. are to be copied.  Or C<$hModelFile> can be C<0> [the default].
  765.  
  766. =item Access => $sAccess
  767.  
  768. =item Access => $uAccess
  769.  
  770. C<$sAccess> should be a string of zero or more characters from
  771. C<"qrw"> specifying the type of access desired:  "query" or C<0>,
  772. "read" or C<GENERIC_READ> [the default], or "write" or
  773. C<GENERIC_WRITE>.
  774.  
  775. C<$uAccess> should be an unsigned value containing bits set to
  776. indicate the type of access desired.  C<GENERIC_READ> is the default.
  777.  
  778. =item Create => $sCreate
  779.  
  780. =item Create => $uCreate
  781.  
  782. C<$sCreate> should be a string constaing zero or one character from
  783. C<"ktn"> and zero or one character from C<"ce">.  These stand for
  784. "Keep if exists", "Truncate if exists", "New file only", "Create if
  785. none", and "Existing file only".  These are translated into a
  786. C<$uCreate> value.
  787.  
  788. C<$uCreate> should be one of C<OPEN_ALWAYS>, C<OPEN_EXISTING>,
  789. C<TRUNCATE_EXISTING>, C<CREATE_ALWAYS>, or C<CREATE_NEW>.
  790.  
  791. =item Share => $sShare
  792.  
  793. =item Share => $uShare
  794.  
  795. C<$sShare> should be a string with zero or more characters from
  796. C<"rwd"> that is translated into a C<$uShare> value.  C<"rw"> is
  797. the default.
  798.  
  799. C<$uShare> should be an unsigned value having zero or more of the
  800. following bits set:  C<FILE_SHARE_READ>, C<FILE_SHARE_WRITE>, and
  801. C<FILE_SHARE_DELETE>.  C<FILE_SHARE_READ|FILE_SHARE_WRITE> is the
  802. default.
  803.  
  804. =back
  805.  
  806. Examples:
  807.  
  808.     $hFlop= createFile( "//./A:", "r", "r" )
  809.       or  die "Can't prevent others from writing to floppy: $^E\n";
  810.     $hDisk= createFile( "//./C:", "rw ke", "" )
  811.       or  die "Can't get exclusive access to C: $^E\n";
  812.     $hDisk= createFile( $sFilePath, "ke",
  813.       { Access=>FILE_READ_ATTRIBUTES } )
  814.       or  die "Can't read attributes of $sFilePath: $^E\n";
  815.     $hTemp= createFile( "$ENV{Temp}/temp.$$", "wn", "",
  816.       { Attributes=>"hst", Flags=>FILE_FLAG_DELETE_ON_CLOSE() } )
  817.       or  die "Can't create temporary file, temp.$$: $^E\n";
  818.  
  819. =item getLogicalDrives
  820.  
  821. =item C<@roots= getLogicalDrives()>
  822.  
  823. Returns the paths to the root directories of all logical drives
  824. currently defined.  This includes all types of drive lettters, such
  825. as floppies, CD-ROMs, hard disks, and network shares.  A typical
  826. return value on a poorly equipped computer would be C<("A:\\","C:\\")>.
  827.  
  828. =item CloseHandle
  829.  
  830. =item C<CloseHandle( $hObject )>
  831.  
  832. Closes a Win32 native handle, such as one opened via C<CreateFile>. 
  833. Like most routines, returns a true value if successful and a false
  834. value [and sets C<$^E> and C<regLastError()>] on failure.
  835.  
  836. =item CopyFile
  837.  
  838. =item C<CopyFile( $sOldFileName, $sNewFileName, $bFailIfExists )>
  839.  
  840. C<$sOldFileName> is the path to the file to be copied. 
  841. C<$sNewFileName> is the path to where the file should be copied. 
  842. Note that you can B<NOT> just specify a path to a directory in
  843. C<$sNewFileName> to copy the file to that directory using the
  844. same file name.
  845.  
  846. If C<$bFailIfExists> is true and C<$sNewFileName> is the path to
  847. a file that already exists, then C<CopyFile> will fail.  If
  848. C<$bFailIfExists> is falsea, then the copy of the C<$sOldFileNmae>
  849. file will overwrite the C<$sNewFileName> file if it already exists.
  850.  
  851. Like most routines, returns a true value if successful and a false
  852. value [and sets C<$^E> and C<regLastError()>] on failure.
  853.  
  854. =item CreateFile
  855.  
  856. =item C<$hObject= CreateFile( $sPath, $uAccess, $uShare, $pSecAttr, $uCreate, $uFlags, $hModel )>
  857.  
  858. On failure, C<$hObject> gets set to a false value and C<$^E> and
  859. C<fileLastError()> are set to the reason for the failure.  Otherwise,
  860. C<$hObject> gets set to a Win32 native file handle which is always a
  861. true value [returns C<"0 but true"> in the impossible(?) case of the
  862. handle having a value of C<0>].
  863.  
  864. C<$sPath> is the path to the file [or device, etc.] to be opened.
  865.  
  866. C<$sPath> can use C<"/"> or C<"\\"> as path delimiters and can even
  867. mix the two.  We will usually only use C<"/"> in our examples since
  868. using C<"\\"> is usually harder to read.
  869.  
  870. Under Windows NT, C<$sPath> can start with C<"//?/"> to allow the use
  871. of paths longer than C<MAX_PATH> [for UNC paths, replace the leading
  872. C<"//"> with C<"//?/UNC/">, as in C<"//?/UNC/Server/Share/Dir/File.Ext">].
  873.  
  874. C<$sPath> can start with C<"//./"> to indicate that the rest of the
  875. path is the name of a "DOS device."  You can use C<QueryDosDevice>
  876. to list all current DOS devices and can add or delete them with
  877. C<DefineDosDevice>.  If you get the source-code distribution of this
  878. module from CPAN, then it includes an example script, F<ex/ListDevs.plx>
  879. that will list all current DOS devices and their "native" definition.
  880. Again, note that this doesn't work under Win95 nor Win98.
  881.  
  882. The most common such DOS devices include:
  883.  
  884. =over
  885.  
  886. =item C<"//./PhysicalDrive0">
  887.  
  888. Your entire first hard disk.  Doesn't work under Windows 95.  This
  889. allows you to read or write raw sectors of your hard disk and to use
  890. C<DeviceIoControl> to perform miscellaneous queries and operations
  891. to the hard disk.   Writing raw sectors and certain other operations
  892. can seriously damage your files or the function of your computer.
  893.  
  894. Locking this for exclusive access [by specifying C<0> for C<$uShare>]
  895. doesn't prevent access to the partitions on the disk nor their file
  896. systems.  So other processes can still access any raw sectors within
  897. a partition and can use the file system on the disk as usual.
  898.  
  899. =item C<"//./C:">
  900.  
  901. Your F<C:> partition.  Doesn't work under Windows 95.  This allows
  902. you to read or write raw sectors of that partition and to use
  903. C<DeviceIoControl> to perform miscellaneous queries and operations
  904. to the partition.  Writing raw sectors and certain other operations
  905. can seriously damage your files or the function of your computer.
  906.  
  907. Locking this for exclusive access doesn't prevent access to the
  908. physical drive that the partition is on so other processes can
  909. still access the raw sectors that way.  Locking this for exclusive
  910. access B<does> prevent other processes from opening the same raw
  911. partition and B<does> prevent access to the file system on it.  It
  912. even prevents the current process from accessing the file system
  913. on that partition.
  914.  
  915. =item C<"//./A:">
  916.  
  917. The raw floppy disk.  Doesn't work under Windows 95.  This allows
  918. you to read or write raw sectors of the floppy disk and to use
  919. C<DeviceIoControl> to perform miscellaneous queries and operations
  920. to the floopy disk or drive.
  921.  
  922. Locking this for exclusive access prevents all access to the floppy.
  923.  
  924. =item C<"//./PIPE/PipeName">
  925.  
  926. A named pipe, created via C<CreateNamedPipe>.
  927.  
  928. =back
  929.  
  930. C<$uAccess> is an unsigned value with bits set indicating the
  931. type of access desired.  Usually either C<0> ["query" access],
  932. C<GENERIC_READ>, C<GENERIC_WRITE>, C<GENERIC_READ|GENERIC_WRITE>,
  933. or C<GENERIC_ALL>.  More specific types of access can be specified,
  934. such as C<FILE_APPEND_DATA> or C<FILE_READ_EA>.
  935.  
  936. C<$uShare> controls how the file is shared, that is, whether other
  937. processes can have read, write, and/or delete access to the file while
  938. we have it opened.  C<$uShare> is an unsigned value with zero or more
  939. of these bits set:  C<FILE_SHARE_READ>, C<FILE_SHARE_WRITE>, and
  940. C<FILE_SHARE_DELETE>.
  941.  
  942. If another process currently has read, write, and/or delete access to
  943. the file and you don't allow that level of sharing, then your call to
  944. C<CreateFile> will fail.  If you requested read, write, and/or delete
  945. access and another process already has the file open but doesn't allow
  946. that level of sharing, thenn your call to C<createFile> will fail.  Once
  947. you have the file open, if another process tries to open it with read,
  948. write, and/or delete access and you don't allow that level of sharing,
  949. then that process won't be allowed to open the file.
  950.  
  951. C<$pSecAttr> should either be C<[]> [for C<NULL>] or a
  952. C<SECURITY_ATTRIBUTES> data structure packed into a string.
  953. For example, if C<$pSecDesc> contains a C<SECURITY_DESCRIPTOR>
  954. structure packed into a string, perhaps via:
  955.  
  956.     RegGetKeySecurity( $key, 4, $pSecDesc, 1024 );
  957.  
  958. then you can set C<$pSecAttr> via:
  959.  
  960.     $pSecAttr= pack( "L P i", 12, $pSecDesc, $bInheritHandle );
  961.  
  962. C<$uCreate> is one of the following values:  C<OPEN_ALWAYS>,
  963. C<OPEN_EXISTING>, C<TRUNCATE_EXISTING>, C<CREATE_ALWAYS>, and
  964. C<CREATE_NEW>.
  965.  
  966. C<$uFlags> is an unsigned value with zero or more bits set indicating
  967. attributes to associate with the file [C<FILE_ATTRIBUTE_*> values] or
  968. special options [C<FILE_FLAG_*> values].
  969.  
  970. If opening the client side of a named pipe, then you can also set
  971. C<$uFlags> to include C<SECURITY_SQOS_PRESENT> along with one of the
  972. other C<SECURITY_*> constants to specify the security quality of
  973. service to be used.
  974.  
  975. C<$hModel> is C<0> [or C<[]>, both of which mean C<NULL>] or a Win32
  976. native handle opened with C<GENERIC_READ> access to a model file from
  977. which file attributes and extended attributes are to be copied if a
  978. new file gets created.
  979.  
  980. Examples:
  981.  
  982.     $hFlop= CreateFile( "//./A:", GENERIC_READ(),
  983.       FILE_SHARE_READ(), [], OPEN_EXISTING(), 0, [] )
  984.       or  die "Can't prevent others from writing to floppy: $^E\n";
  985.     $hDisk= createFile( $sFilePath, FILE_READ_ATTRIBUTES(),
  986.       FILE_SHARE_READ()|FILE_SHARE_WRITE(), [], OPEN_EXISTING(), 0, [] )
  987.       or  die "Can't read attributes of $sFilePath: $^E\n";
  988.     $hTemp= createFile( "$ENV{Temp}/temp.$$", GENERIC_WRITE(), 0,
  989.       CREATE_NEW(), FILE_FLAG_DELETE_ON_CLOSE()|attrLetsToBits("hst"), [] )
  990.       or  die "Can't create temporary file, temp.$$: $^E\n";
  991.  
  992. =item DefineDosDevice
  993.  
  994. =item C<DefineDosDevice( $uFlags, $sDosDeviceName, $sTargetPath )>
  995.  
  996. Defines a new DOS device, overrides the current definition of a DOS
  997. device, or deletes a definition of a DOS device.  Like most routines,
  998. returns a true value if successful and a false value [and sets C<$^E>
  999. and C<regLastError()>] on failure.
  1000.  
  1001. C<$sDosDeviceName> is the name of a DOS device for which we'd like
  1002. to add or delete a definition.
  1003.  
  1004. C<$uFlags> is an unsigned value with zero or more of the following
  1005. bits set:
  1006.  
  1007. =over
  1008.  
  1009. =item C<DDD_RAW_TARGET_PATH>
  1010.  
  1011. Indicates that C<$sTargetPath> will be a raw Windows NT object name. 
  1012. This usually means that C<$sTargetPath> starts with C<"\\Device\\">. 
  1013. Note that you cannot use C<"/"> in place of C<"\\"> in raw target path
  1014. names.
  1015.  
  1016. =item C<DDD_REMOVE_DEFINITION>
  1017.  
  1018. Requests that a definition be deleted.  If C<$sTargetPath> is
  1019. C<[]> [for C<NULL>], then the most recently added definition for
  1020. C<$sDosDeviceName> is removed.  Otherwise the most recently added
  1021. definition matching C<$sTargetPath> is removed.
  1022.  
  1023. If the last definition is removed, then the DOS device name is
  1024. also deleted.
  1025.  
  1026. =item C<DDD_EXACT_MATCH_ON_REMOVE>
  1027.  
  1028. When deleting a definition, this bit causes each C<$sTargetPath> to
  1029. be compared to the full-length definition when searching for the most
  1030. recently added match.  If this bit is not set, then C<$sTargetPath>
  1031. only needs to match a prefix of the definition.
  1032.  
  1033. =back
  1034.  
  1035. C<$sTargetPath> is the DOS device's specific definition that you
  1036. wish to add or delete.  For C<DDD_RAW_TARGET_PATH>, these usually
  1037. start with C<"\\Device\\">.  If the C<DDD_RAW_TARGET_PATH> bit is
  1038. not set, then C<$sTargetPath> is just an ordinary path to some file
  1039. or directory, providing the functionality of the B<subst> command.
  1040.  
  1041. =item DeleteFile
  1042.  
  1043. =item C<DeleteFile( $sFileName )>
  1044.  
  1045. Deletes the named file.  Compared to Perl's C<unlink>, C<DeleteFile>
  1046. has the advantage of not deleting read-only files.  For B<some>
  1047. versions of Perl, C<unlink> silently calls C<chmod> whether it needs
  1048. to or not before deleting the file so that files that you have
  1049. protected by marking them as read-only are not always protected from
  1050. Perl's C<unlink>.
  1051.  
  1052. Like most routines, returns a true value if successful and a false
  1053. value [and sets C<$^E> and C<regLastError()>] on failure.
  1054.  
  1055. =item DeviceIoControl
  1056.  
  1057. =item C<DeviceIoControl( $hDevice, $uIoControlCode, $pInBuf, $lInBuf, $opOutBuf, $lOutBuf, $olRetBytes, $pOverlapped )>
  1058.  
  1059. Requests a special operation on an I/O [input/output] device, such
  1060. as ejecting a tape or formatting a disk.  Like most routines, returns
  1061. a true value if successful and a false value [and sets C<$^E> and
  1062. C<regLastError()>] on failure.
  1063.  
  1064. C<$hDevice> is a Win32 native file handle to a device [return value
  1065. from C<CreateFile>].
  1066.  
  1067. C<$uIoControlCode> is an unsigned value [a C<IOCTL_*> constant]
  1068. indicating the type query or other operation to be performed.
  1069.  
  1070. C<$pInBuf> is C<[]> [for C<NULL>] or a data structure packed into a
  1071. string.  The type of data structure depends on the C<$uIoControlCode>
  1072. value.  C<$lInBuf> is C<0> or the length of the structure in
  1073. C<$pInBuf>.  If C<$pInBuf> is not C<[]> and C<$lInBuf> is C<0>, then
  1074. C<$lInBuf> will automatically be set to C<length($pInBuf)> for you.
  1075.  
  1076. C<$opOutBuf> is C<[]> [for C<NULL>] or will be set to contain a
  1077. returned data structure packed into a string.  C<$lOutBuf> indicates
  1078. how much space to allocate in C<$opOutBuf> for C<DeviceIoControl> to
  1079. store the data structure.  If C<$lOutBuf> is a number and C<$opOutBuf>
  1080. already has a buffer allocated for it that is larger than C<$lOutBuf>
  1081. bytes, then this larger buffer size will be passed to C<DeviceIoControl>.
  1082. However, you can force a specific buffer size to be passed to
  1083. C<DeviceIoControl> by prepending a C<"="> to the front of C<$lOutBuf>.
  1084.  
  1085. C<$olRetBytes> is C<[]> or is a scalar to receive the number of bytes
  1086. written to C<$opOutBuf>.  Even when C<$olRetBytes> is C<[]>, a valid
  1087. pointer to a C<DWORD> [and not C<NULL>] is passed to C<DeviceIoControl>.
  1088. In this case, C<[]> just means that you don't care about the value
  1089. that might be written to C<$olRetBytes>, which is usually the case
  1090. since you can usually use C<length($opOutBuf)> instead.
  1091.  
  1092. C<$pOverlapped> is C<[]> or is a C<OVERLAPPED> structure packed into
  1093. a string.  This is only useful if C<$hDevice> was opened with the
  1094. C<FILE_FLAG_OVERLAPPED> flag set.
  1095.  
  1096. =item FdGetOsFHandle
  1097.  
  1098. =item C<$hNativeHandle= FdGetOsFHandle( $ivFd )>
  1099.  
  1100. C<FdGetOsFHandle> simply calls C<_get_osfhandle()>.  It was renamed
  1101. to better fit in with the rest the function names of this module,
  1102. in particular to distinguish it from C<GetOsFHandle>.  It takes an
  1103. integer file descriptor [as from Perl's C<fileno>] and returns the
  1104. Win32 native file handle associated with that file descriptor or
  1105. C<INVALID_HANDLE_VALUE> if C<$ivFd> is not an open file descriptor.
  1106.  
  1107. When you call Perl's C<open> to set a Perl file handle [like C<STDOUT>],
  1108. Perl calls C's C<fopen> to set a stdio C<FILE *>.  C's C<fopen> calls
  1109. something like Unix's C<open>, that is, Win32's C<_sopen>, to get an
  1110. integer file descriptor [where 0 is for C<STDIN>, 1 for C<STDOUT>, etc.].
  1111. Win32's C<_sopen> calls C<CreateFile> to set a C<HANDLE>, a Win32 native
  1112. file handle.  So every Perl file handle [like C<STDOUT>] has an integer
  1113. file descriptor associated with it that you can get via C<fileno>.  And,
  1114. under Win32, every file descriptor has a Win32 native file handle
  1115. associated with it.  C<FdGetOsFHandle> lets you get access to that.
  1116.  
  1117. C<$hNativeHandle> is set to C<INVALID_HANDLE_VALUE> [and
  1118. C<lastFileError()> and C<$^E> are set] if C<FdGetOsFHandle> fails. 
  1119. See also C<GetOsFHandle> which provides a friendlier interface.
  1120.  
  1121. =item fileConstant
  1122.  
  1123. =item C<$value= fileConstant( $sConstantName )>
  1124.  
  1125. Fetch the value of a constant.  Returns C<undef> if C<$sConstantName>
  1126. is not the name of a constant supported by this module.  Never sets
  1127. C<$!> nor C<$^E>.
  1128.  
  1129. This function is rarely used since you will usually get the value of a
  1130. constant by having that constant imported into your package by listing
  1131. the constant name in the C<use Win32API::File> statement and then
  1132. simply using the constant name in your code [perhaps followed by
  1133. C<()>].  This function is useful for verifying constant names not in
  1134. Perl code, for example, after prompting a user to type in a constant
  1135. name.
  1136.  
  1137. =item fileLastError
  1138.  
  1139. =item C<$svError= fileLastError();>
  1140.  
  1141. =item C<fileLastError( $uError );>
  1142.  
  1143. Returns the last error encountered by a routine from this module. 
  1144. It is just like C<$^E> except it isn't changed by anything except
  1145. routines from this module.  Ideally you could just use C<$^E>, but
  1146. current versions of Perl often overwrite C<$^E> before you get a
  1147. chance to check it and really old versions of Perl don't really
  1148. support C<$^E> under Win32.
  1149.  
  1150. Just like C<$^E>, in a numeric context C<fileLastError()> returns
  1151. the numeric error value while in a string context it returns a
  1152. text description of the error [actually it returns a Perl scalar
  1153. that contains both values so C<$x= fileLastError()> causes C<$x>
  1154. to give different values in string vs. numeric contexts].  On old
  1155. versions of Perl where C<$^E> isn't tied to C<GetLastError()>,
  1156. C<fileLastError> simply returns the number of the error and you'll
  1157. need to use <Win32::FormatMessage> to get the error string.
  1158.  
  1159. The last form sets the error returned by future calls to
  1160. C<fileLastError()> and should not be used often.  C<$uError> must
  1161. be a numeric error code.  Also returns the dual-valued version
  1162. of C<$uError>.
  1163.  
  1164. =item GetDriveType
  1165.  
  1166. =item C<$uDriveType= GetDriveType( $sRootPath )>
  1167.  
  1168. Takes a string giving the path to the root directory of a file system
  1169. [called a "drive" because every file system is assigned a "drive letter"]
  1170. and returns an unsigned value indicating the type of drive the file
  1171. system is on.  The return value should be one of:
  1172.  
  1173. =over
  1174.  
  1175. =item C<DRIVE_UNKNOWN>
  1176.  
  1177. None of the following.
  1178.  
  1179. =item C<DRIVE_NO_ROOT_DIR>
  1180.  
  1181. A "drive" that does not have a file system.  This can be a drive letter
  1182. that hasn't been defined or a drive letter assigned to a partition
  1183. that hasn't been formatted yet.
  1184.  
  1185. =item C<DRIVE_REMOVABLE>
  1186.  
  1187. A floppy diskette drive or other removable media drive, but not a CD-ROM
  1188. drive.
  1189.  
  1190. =item C<DRIVE_FIXED>
  1191.  
  1192. An ordinary hard disk partition.
  1193.  
  1194. =item C<DRIVE_REMOTE>
  1195.  
  1196. A network share.
  1197.  
  1198. =item C<DRIVE_CDROM>
  1199.  
  1200. A CD-ROM drive.
  1201.  
  1202. =item C<DRIVE_RAMDISK>
  1203.  
  1204. A "ram disk" or memory-resident virtual file system used for high-speed
  1205. access to small amounts of temporary file space.
  1206.  
  1207. =back
  1208.  
  1209. =item GetFileType
  1210.  
  1211. =item C<$uFileType= GetFileType( $hFile )>
  1212.  
  1213. Takes a Win32 native file handle and returns a C<FILE_TYPE_*> constant
  1214. indicating the type of the file opened on that handle:
  1215.  
  1216. =over
  1217.  
  1218. =item C<FILE_TYPE_UNKNOWN>
  1219.  
  1220. None of the below.  Often a special device.
  1221.  
  1222. =item C<FILE_TYPE_DISK>
  1223.  
  1224. An ordinary disk file.
  1225.  
  1226. =item C<FILE_TYPE_CHAR>
  1227.  
  1228. What Unix would call a "character special file", that is, a device that
  1229. works on character streams such as a printer port or a console.
  1230.  
  1231. =item C<FILE_TYPE_PIPE>
  1232.  
  1233. Either a named or anonymous pipe.
  1234.  
  1235. =back
  1236.  
  1237. =item GetLogicalDrives
  1238.  
  1239. =item C<$uDriveBits= GetLogicalDrives()>
  1240.  
  1241. Returns an unsigned value with one bit set for each drive letter currently
  1242. defined.  If "A:" is currently a valid drive letter, then the C<1> bit
  1243. will be set in C<$uDriveBits>.  If "B:" is valid, then the C<2> bit will
  1244. be set.  If "Z:" is valid, then the C<2**26> [C<0x4000000>] bit will be
  1245. set.
  1246.  
  1247. =item GetLogicalDriveStrings
  1248.  
  1249. =item C<$olOutLength= GetLogicalDriveStrings( $lBufSize, $osBuffer )>
  1250.  
  1251. For each currently defined drive letter, a C<'\0'>-terminated string
  1252. of the path to the root of its file system is constructed.  All of
  1253. these strings are concatenated into a single larger string and an
  1254. extra terminating C<'\0'> is added.  This larger string is returned
  1255. in C<$osBuffer>.  Note that this includes drive letters that have
  1256. been defined but that have no file system, such as drive letters
  1257. assigned to unformatted partitions.
  1258.  
  1259. C<$lBufSize> is the size of the buffer to allocate to store this
  1260. list of strings.  C<26*4+1> is always sufficient and should usually
  1261. be used.
  1262.  
  1263. C<$osBuffer> is a scalar to be set to contain the constructed string.
  1264.  
  1265. C<$olOutLength> is the number of bytes actually written to C<$osBuffer>
  1266. but C<length($osBuffer)> can also be used to determine this.
  1267.  
  1268. For example, on a poorly equipped computer,
  1269.  
  1270.     GetLogicalDriveStrings( 4*26+1, $osBuffer );
  1271.  
  1272. might set C<$osBuffer> to the 9-character string, C<"A:\\\0C:\\\0\0">.
  1273.  
  1274. =item GetHandleInformation
  1275.  
  1276. =item C<GetHandleInformation( $hObject, $ouFlags )>
  1277.  
  1278. Retrieves the flags associated with a Win32 native file handle or object
  1279. handle.
  1280.  
  1281. C<$hObject> is an open Win32 native file handle or an open Win32 native
  1282. handle to some other type of object.
  1283.  
  1284. C<$ouFlags> will be set to an unsigned value having zero or more of
  1285. the bits C<HANDLE_FLAG_INHERIT> and C<HANDLE_FLAG_PROTECT_FROM_CLOSE>
  1286. set.  See the C<":HANDLE_FLAG_"> export class for the meanings of these
  1287. bits.
  1288.  
  1289. =item GetOsFHandle
  1290.  
  1291. =item C<$hNativeHandle= GetOsFHandle( FILE )>
  1292.  
  1293. Takes a Perl file handle [like C<STDIN>] and returns the Win32 native
  1294. file handle associated with it.  See C<FdGetOsFHandle> for more
  1295. information about Win32 native file handles.
  1296.  
  1297. C<$hNativeHandle> is set to a false value [and C<lastFileError()> and
  1298. C<$^E> are set] if C<GetOsFHandle> fails.    C<GetOsFHandle> returns
  1299. C<"0 but true"> in the impossible(?) case of the handle having a value
  1300. of C<0>.
  1301.  
  1302. =item GetVolumeInformation
  1303.  
  1304. =item C<GetVolumeInformation( $sRootPath, $osVolName, $lVolName, $ouSerialNum, $ouMaxNameLen, $ouFsFlags, $osFsType, $lFsType )>
  1305.  
  1306. Gets information about a file system volume, returning a true
  1307. value if successful.  On failure, returns a false value and sets
  1308. C<fileLastError()> and C<$^E>.
  1309.  
  1310. C<$sRootPath> is a string specifying the path to the root of the file system,
  1311. for example, C<"C:/">.
  1312.  
  1313. C<$osVolName> is a scalar to be set to the string representing the
  1314. volume name, also called the file system label.  C<$lVolName> is the
  1315. number of bytes to allocate for the C<$osVolName> buffer [see
  1316. L<Buffer Sizes> for more information].
  1317.  
  1318. C<$ouSerialNum> is C<[]> [for C<NULL>] or will be set to the numeric
  1319. value of the volume's serial number.
  1320.  
  1321. C<$ouMaxNameLen> is C<[]> [for C<NULL>] or will be set to the maximum
  1322. length allowed for a file name or directory name within the file system.
  1323.  
  1324. C<$osFsType> is a scalar to be set to the string representing the
  1325. file system type, such as C<"FAT"> or C<"NTFS">.  C<$lFsType> is the
  1326. number of bytes to allocate for the C<$osFsType> buffer [see
  1327. L<Buffer Sizes> for more information].
  1328.  
  1329. C<$ouFsFlags> is C<[]> [for C<NULL>] or will be set to an unsigned integer
  1330. with bits set indicating properties of the file system:
  1331.  
  1332. =over
  1333.  
  1334. =item C<FS_CASE_IS_PRESERVED>
  1335.  
  1336. The file system preserves the case of file names [usually true].
  1337. That is, it doesn't change the case of file names such as forcing
  1338. them to upper- or lower-case.
  1339.  
  1340. =item C<FS_CASE_SENSITIVE>
  1341.  
  1342. The file system supports the ability to not ignore the case of file
  1343. names [but might ignore case the way you are using it].  That is, the
  1344. file system has the ability to force you to get the letter case of a
  1345. file's name exactly right to be able to open it.  This is true for
  1346. "NTFS" file systems, even though case in file names is usually still
  1347. ignored.
  1348.  
  1349. =item C<FS_UNICODE_STORED_ON_DISK>
  1350.  
  1351. The file system preserves Unicode in file names [true for "NTFS"].
  1352.  
  1353. =item C<FS_PERSISTENT_ACLS>
  1354.  
  1355. The file system supports setting Access Control Lists on files [true
  1356. for "NTFS"].
  1357.  
  1358. =item C<FS_FILE_COMPRESSION>
  1359.  
  1360. The file system supports compression on a per-file basis [true for
  1361. "NTFS"].
  1362.  
  1363. =item C<FS_VOL_IS_COMPRESSED>
  1364.  
  1365. The entire file system is compressed such as via "DoubleSpace".
  1366.  
  1367. =back
  1368.  
  1369. =item IsRecognizedPartition
  1370.  
  1371. =item C<IsRecognizedPartition( $ivPartitionType )>
  1372.  
  1373. Takes a partition type and returns whether that partition type is
  1374. supported under Win32.  C<$ivPartitonType> is an integer value as from
  1375. the operating system byte of a hard disk's DOS-compatible partition
  1376. table [that is, a partition table for x86-based Win32, not, for
  1377. example, one used with Windows NT for Alpha processors].  For example,
  1378. the C<PartitionType> member of the C<PARTITION_INFORMATION> structure.
  1379.  
  1380. Common values for C<$ivPartitionType> include C<PARTITION_FAT_12==1>,
  1381. C<PARTITION_FAT_16==4>, C<PARTITION_EXTENDED==5>, C<PARTITION_FAT32==0xB>.
  1382.  
  1383. =item IsContainerPartition
  1384.  
  1385. =item C<IsContainerPartition( $ivPartitionType )>
  1386.  
  1387. Takes a partition type and returns whether that partition is a
  1388. "container" partition that is supported under Win32, that is, whether
  1389. it is an "extended" partition that can contain "logical" partitions. 
  1390. C<$ivPartitonType> is as for C<IsRecognizedPartition>.
  1391.  
  1392. =item MoveFile
  1393.  
  1394. =item C<MoveFile( $sOldName, $sNewName )>
  1395.  
  1396. Renames a file or directory.  C<$sOldName> is the name of the existing
  1397. file or directory that is to be renamed.  C<$sNewName> is the new name
  1398. to give the file or directory.  Returns a true value if the move
  1399. succeeds.  For failure, returns a false value and sets
  1400. C<fileLastErorr()> and C<$^E> to the reason for the failure.
  1401.  
  1402. Files can be "renamed" between file systems and the file contents and
  1403. some attributes will be moved.  Directories can only be renamed within
  1404. one file system.  If there is already a file or directory named
  1405. C<$sNewName>, then C<MoveFile> will fail.
  1406.  
  1407. =item MoveFileEx
  1408.  
  1409. =item C<MoveFileEx( $sOldName, $sNewName, $uFlags )>
  1410.  
  1411. Renames a file or directory.  C<$sOldName> is the name of the existing
  1412. file or directory that is to be renamed.  C<$sNewName> is the new name
  1413. to give the file or directory.  Returns a true value if the move
  1414. succeeds.  For failure, returns a false value and sets
  1415. C<fileLastErorr()> and C<$^E> to the reason for the failure.
  1416.  
  1417. C<$uFlags> is an unsigned value with zero or more of the following bits set:
  1418.  
  1419. =over
  1420.  
  1421. =item C<MOVEFILE_REPLACE_EXISTING>
  1422.  
  1423. If this bit is set and a file [but not a directory] named C<$sNewName>
  1424. already exists, then it will be replaced by C<$sOldName>.  If this bit
  1425. is not set then C<MoveFileEx> will fail rather than replace an existing
  1426. C<$sNewName>.
  1427.  
  1428. =item C<MOVEFILE_COPY_ALLOWED>
  1429.  
  1430. Allows files [but not directories] to be moved between file systems
  1431. by copying the C<$sOldName> file data and some attributes to
  1432. C<$sNewName> and then deleting C<$sOldName>.  If this bit is not set
  1433. [or if C<$sOldName> denotes a directory] and C<$sNewName> refers to a
  1434. different file system than C<$sOldName>, then C<MoveFileEx> will fail.
  1435.  
  1436. =item C<MOVEFILE_DELAY_UNTIL_REBOOT>
  1437.  
  1438. Preliminary verifications are made and then an entry is added to the
  1439. Registry to cause the rename [or delete] operation to be done the
  1440. next time this copy of the operating system is booted [right after
  1441. any automatic file system checks have completed].  This is not
  1442. supported under Windows 95.
  1443.  
  1444. When this bit is set, C<$sNewName> can be C<[]> [for C<NULL>] to
  1445. indicate that C<$sOldName> should be deleted during the next boot
  1446. rather than renamed.
  1447.  
  1448. Setting both the C<MOVEFILE_COPY_ALLOWED> and
  1449. C<MOVEFILE_DELAY_UNTIL_REBOOT> bits will cause C<MoveFileEx> to fail.
  1450.  
  1451. =item C<MOVEFILE_WRITE_THROUGH>
  1452.  
  1453. Ensures that C<MoveFileEx> won't return until the operation has
  1454. finished and been flushed to disk.   This is not supported under
  1455. Windows 95.  Only affects file renames to another file system,
  1456. forcing a buffer flush at the end of the copy operation.
  1457.  
  1458. =back
  1459.  
  1460. =item OsFHandleOpen
  1461.  
  1462. =item C<OsFHandleOpen( FILE, $hNativeHandle, $sMode )>
  1463.  
  1464. Opens a Perl file handle based on an already open Win32 native
  1465. file handle [much like C's C<fdopen()> does with a file descriptor].
  1466. Returns a true value if the open operation succeeded.  For failure,
  1467. returns a false value and sets C<$!> [and possibly C<fileLastError()>
  1468. and C<$^E>] to the reason for the failure.
  1469.  
  1470. C<FILE> is a Perl file handle [in any of the supported forms, a
  1471. bareword, a string, a typeglob, or a reference to a typeglob] that
  1472. will be opened.  If C<FILE> is already open, it will automatically
  1473. be closed before it is reopened.
  1474.  
  1475. C<$hNativeHandle> is an open Win32 native file handle, probably the
  1476. return value from C<CreateFile> or C<createFile>.
  1477.  
  1478. C<$sMode> is string of zero or more letters from C<"rwatb">.  These
  1479. are translated into a combination C<O_RDONLY> [C<"r">], C<O_WRONLY>
  1480. [C<"w">], C<O_RDWR> [C<"rw">], C<O_APPEND> [C<"a">], C<O_TEXT>
  1481. [C<"t">], and C<O_BINARY> [C<"b">] flags [see the L<Fcntl> module]
  1482. that is passed to C<OsFHandleOpenFd>.   Currently only C<O_APPEND>
  1483. and C<O_TEXT> have any significance.
  1484.  
  1485. Also, a C<"r"> and/or C<"w"> in C<$sMode> is used to decide how the
  1486. file descriptor is converted into a Perl file handle, even though this
  1487. doesn't appear to make a difference.  One of the following is used:
  1488.  
  1489.     open( FILE, "<&=".$ivFd )    # "r" w/o "w"
  1490.     open( FILE, ">&=".$ivFd )    # "w" w/o "r"
  1491.     open( FILE, "+<&=".$ivFd )    # both "r" and "w"
  1492.  
  1493. C<OsFHandleOpen> eventually calls the Win32-specific C routine
  1494. C<_open_osfhandle()> or Perl's "improved" version called
  1495. C<win32_open_osfhandle()>.  Prior to Perl5.005, C's
  1496. C<_open_osfhandle()> is called which will fail if
  1497. C<GetFileType($hNativeHandle)> would return C<FILE_TYPE_UNKNOWN>.  For
  1498. Perl5.005 and later, C<OsFHandleOpen> calls C<win32_open_osfhandle()>
  1499. from the Perl DLL which doesn't have this restriction.
  1500.  
  1501. =item OsFHandleOpenFd
  1502.  
  1503. =item C<$ivFD= OsFHandleOpenFd( $hNativeHandle, $uMode )>
  1504.  
  1505. Opens a file descriptor [C<$ivFD>] based on an already open Win32
  1506. native file handle, C<$hNativeHandle>.  This just calls the
  1507. Win32-specific C routine C<_open_osfhandle()> or Perl's "improved"
  1508. version called C<win32_open_osfhandle()>.  Prior to Perl5.005, C's
  1509. C<_open_osfhandle()> is called which will fail if
  1510. C<GetFileType($hNativeHandle)> would return C<FILE_TYPE_UNKNOWN>.  
  1511. For Perl5.005 and later, C<OsFHandleOpenFd> calls
  1512. C<win32_open_osfhandle()> from the Perl DLL which doesn't have this
  1513. restriction.
  1514.  
  1515. C<$uMode> the logical combination of zero or more C<O_*> constants
  1516. exported by the C<Fcntl> module.  Currently only C<O_APPEND> and
  1517. C<O_TEXT> have any significance.
  1518.  
  1519. C<$ivFD> will be non-negative if the open operation was successful. 
  1520. For failure, C<-1> is returned and C<$!> [and possibly
  1521. C<fileLastError()> and C<$^E>] is set to the reason for the failure.
  1522.  
  1523. =item QueryDosDevice
  1524.  
  1525. =item C<$olTargetLen= QueryDosDevice( $sDosDeviceName, $osTargetPath, $lTargetBuf )>
  1526.  
  1527. Looks up the definition of a given "DOS" device name, yielding the
  1528. active Windows NT native device name along with any currently dormant
  1529. definitions.
  1530.  
  1531. C<$sDosDeviceName> is the name of the "DOS" device whose definitions
  1532. we want.  For example, C<"C:">, C<"COM1">, or C<"PhysicalDrive0">.
  1533. If C<$sDosDeviceName> is C<[]> [for C<NULL>], the list of all DOS
  1534. device names is returned instead.
  1535.  
  1536. C<$osTargetPath> will be assigned a string containing the list of
  1537. definitions.  The definitions are each C<'\0'>-terminate and are
  1538. concatenated into the string, most recent first, with an extra C<'\0'>
  1539. at the end of the whole string [see C<GetLogicalDriveStrings> for
  1540. a sample of this format].
  1541.  
  1542. C<$lTargetBuf> is the size [in bytes] of the buffer to allocate for
  1543. C<$osTargetPath>.  See L<Buffer Sizes> for more information.
  1544.  
  1545. C<$olTargetLen> is set to the number of bytes written to
  1546. C<$osTargetPath> but you can also use C<length($osTargetPath)>
  1547. to determine this.
  1548.  
  1549. For failure, C<0> is returned and C<fileLastError()> and C<$^E> are
  1550. set to the reason for the failure.
  1551.  
  1552. =item ReadFile
  1553.  
  1554. =item C<ReadFile( $hFile, $opBuffer, $lBytes, $olBytesRead, $pOverlapped )>
  1555.  
  1556. Reads bytes from a file or file-like device.  Returns a true value if
  1557. the read operation was successful.  For failure, returns a false value
  1558. and sets C<fileLastError()> and C<$^E> for the reason for the failure.
  1559.  
  1560. C<$hFile> is a Win32 native file handle that is already open to the
  1561. file or device to read from.
  1562.  
  1563. C<$opBuffer> will be set to a string containing the bytes read.
  1564.  
  1565. C<$lBytes> is the number of bytes you would like to read. 
  1566. C<$opBuffer> is automatically initialized to have a buffer large
  1567. enough to hold that many bytes.  Unlike other buffer sizes, C<$lBytes>
  1568. does not need to have a C<"="> prepended to it to prevent a larger
  1569. value to be passed to the underlying Win32 C<ReadFile> API.  However,
  1570. a leading C<"="> will be silently ignored, even if Perl warnings are
  1571. enabled.
  1572.  
  1573. If C<$olBytesRead> is not C<[]>, it will be set to the actual number
  1574. of bytes read, though C<length($opBuffer)> can also be used to
  1575. determine this.
  1576.  
  1577. C<$pOverlapped> is C<[]> or is a C<OVERLAPPED> structure packed
  1578. into a string.  This is only useful if C<$hFile> was opened with
  1579. the C<FILE_FLAG_OVERLAPPED> flag set.
  1580.  
  1581. =item SetErrorMode
  1582.  
  1583. =item C<$uOldMode= SetErrorMode( $uNewMode )>
  1584.  
  1585. Sets the mode controlling system error handling B<and> returns the
  1586. previous mode value.  Both C<$uOldMode> and C<$uNewMode> will have
  1587. zero or more of the following bits set:
  1588.  
  1589. =over
  1590.  
  1591. =item C<SEM_FAILCRITICALERRORS>
  1592.  
  1593. If set, indicates that when a critical error is encountered, the call
  1594. that triggered the error fails immediately.  Normally this bit is not
  1595. set, which means that a critical error causes a dialogue box to appear
  1596. notifying the desktop user that some application has triggered a
  1597. critical error.   The dialogue box allows the desktop user to decide
  1598. whether the critical error is returned to the process, is ignored, or
  1599. the offending operation is retried.
  1600.  
  1601. This affects the C<CreateFile> and C<GetVolumeInformation> calls.
  1602.  
  1603. Setting this bit is useful for allowing you to check whether a floppy
  1604. diskette is in the floppy drive.
  1605.  
  1606. =item C<SEM_NOALIGNMENTFAULTEXCEPT>
  1607.  
  1608. If set, this causes memory access misalignment faults to be
  1609. automatically fixed in a manner invisible to the process.  This flag
  1610. is ignored on x86-based versions of Windows NT.  This flag is not
  1611. supported on Windows 95.
  1612.  
  1613. =item C<SEM_NOGPFAULTERRORBOX>
  1614.  
  1615. If set, general protection faults do not generate a dialogue box but
  1616. can instead be handled by the process via an exception handler.  This
  1617. bit should not be set by programs that don't know how to handle such
  1618. faults.
  1619.  
  1620. =item C<SEM_NOOPENFILEERRORBOX>
  1621.  
  1622. If set, then when an attempt to continue reading from or writing to
  1623. an already open file [usually on a removable medium like a floppy
  1624. diskette] finds the file no longer available, the call will
  1625. immediately fail.  Normally this bit is not set, which means that
  1626. instead a dialogue box will appear notifying the desktop user that
  1627. some application has run into this problem.   The dialogue box allows
  1628. the desktop user to decide whether the failure is returned to the
  1629. process, is ignored, or the offending operation is retried.
  1630.  
  1631. This affects the C<ReadFile> and C<WriteFile> calls.
  1632.  
  1633. =back
  1634.  
  1635. =item SetFilePointer
  1636.  
  1637. =item C<$uNewPos= SetFilePointer( $hFile, $ivOffset, $ioivOffsetHigh, $uFromWhere )>
  1638.  
  1639. The native Win32 version of C<seek()>.  C<SetFilePointer> sets the
  1640. position within a file where the next read or write operation will
  1641. start from.
  1642.  
  1643. C<$hFile> is a Win32 native file handle.
  1644.  
  1645. C<$uFromWhere> is either C<FILE_BEGIN>, C<FILE_CURRENT>, or
  1646. C<FILE_END>, indicating that the new file position is being specified
  1647. relative to the beginning of the file, the current file pointer, or
  1648. the end of the file, respectively.
  1649.  
  1650. C<$ivOffset> is [if C<$ioivOffsetHigh> is C<[]>] the offset [in bytes]
  1651. to the new file position from the position specified via
  1652. C<$uFromWhere>.  If C<$ioivOffsetHigh> is not C<[]>, then C<$ivOffset>
  1653. is converted to an unsigned value to be used as the low-order 4 bytes
  1654. of the offset.
  1655.  
  1656. C<$ioivOffsetHigh> can be C<[]> [for C<NULL>] to indicate that you are
  1657. only specifying a 4-byte offset and the resulting file position will
  1658. be 0xFFFFFFFE or less [just under 4GB].  Otherwise C<$ioivOfffsetHigh>
  1659. starts out with the high-order 4 bytes [signed] of the offset and gets
  1660. set to the [unsigned] high-order 4 bytes of the resulting file position.
  1661.  
  1662. The underlying C<SetFilePointer> returns C<0xFFFFFFFF> to indicate
  1663. failure, but if C<$ioivOffsetHigh> is not C<[]>, you would also have
  1664. to check C<$^E> to determine whether C<0xFFFFFFFF> indicates an error
  1665. or not.  C<Win32API::File::SetFilePointer> does this checking for you
  1666. and returns a false value if and only if the underlying
  1667. C<SetFilePointer> failed.  For this reason, C<$uNewPos> is set to
  1668. C<"0 but true"> if you set the file pointer to the beginning of the
  1669. file [or any position with 0 for the low-order 4 bytes].
  1670.  
  1671. So the return value will be true if the seek operation was successful.
  1672. For failure, a false value is returned and C<fileLastError()> and
  1673. C<$^E> are set to the reason for the failure.
  1674.  
  1675. =item SetHandleInformation
  1676.  
  1677. =item C<SetHandleInformation( $hObject, $uMask, $uFlags )>
  1678.  
  1679. Sets the flags associated with a Win32 native file handle or object
  1680. handle.  Returns a true value if the operation was successful.  For
  1681. failure, returns a false value and sets C<fileLastError()> and C<$^E>
  1682. for the reason for the failure.
  1683.  
  1684. C<$hObject> is an open Win32 native file handle or an open Win32 native
  1685. handle to some other type of object.
  1686.  
  1687. C<$uMask> is an unsigned value having one or more of the bits
  1688. C<HANDLE_FLAG_INHERIT> and C<HANDLE_FLAG_PROTECT_FROM_CLOSE> set.
  1689. Only bits set in C<$uMask> will be modified by C<SetHandleInformation>.
  1690.  
  1691. C<$uFlags> is an unsigned value having zero or more of the bits
  1692. C<HANDLE_FLAG_INHERIT> and C<HANDLE_FLAG_PROTECT_FROM_CLOSE> set.
  1693. For each bit set in C<$uMask>, the cooresponding bit in the handle's
  1694. flags is set to the value of the cooresponding bit in C<$uFlags>.
  1695.  
  1696. If C<$uOldFlags> were the value of the handle's flags before the
  1697. call to C<SetHandleInformation>, then the value of the handle's
  1698. flags afterward would be:
  1699.  
  1700.     ( $uOldFlags & ~$uMask ) | ( $uFlags & $uMask )
  1701.  
  1702. [at least as far as the C<HANDLE_FLAG_INHERIT> and
  1703. C<HANDLE_FLAG_PROTECT_FROM_CLOSE> bits are concerned.]
  1704.  
  1705. See the C<":HANDLE_FLAG_"> export class for the meanings of these bits.
  1706.  
  1707. =item WriteFile
  1708.  
  1709. =item C<WriteFile( $hFile, $pBuffer, $lBytes, $ouBytesWritten, $pOverlapped )>
  1710.  
  1711. Write bytes to a file or file-like device.  Returns a true value if
  1712. the operation was successful.  For failure, returns a false value and
  1713. sets C<fileLastError()> and C<$^E> for the reason for the failure.
  1714.  
  1715. C<$hFile> is a Win32 native file handle that is already open to the
  1716. file or device to be written to.
  1717.  
  1718. C<$pBuffer> is a string containing the bytes to be written.
  1719.  
  1720. C<$lBytes> is the number of bytes you would like to write.  If
  1721. C<$pBuffer> is not at least C<$lBytes> long, C<WriteFile> croaks.  You
  1722. can specify C<0> for C<$lBytes> to write C<length($pBuffer)> bytes.
  1723. A leading C<"="> on C<$lBytes> will be silently ignored, even if Perl
  1724. warnings are enabled.
  1725.  
  1726. C<$ouBytesWritten> will be set to the actual number of bytes written
  1727. unless you specify it as C<[]>.
  1728.  
  1729. C<$pOverlapped> is C<[]> or is an C<OVERLAPPED> structure packed
  1730. into a string.  This is only useful if C<$hFile> was opened with
  1731. the C<FILE_FLAG_OVERLAPPED> flag set.
  1732.  
  1733. =back
  1734.  
  1735. =item C<":FuncA">
  1736.  
  1737. The ASCII-specific functions.  Each of these is just the same as the
  1738. version without the trailing "A".
  1739.  
  1740.     CopyFileA
  1741.     CreateFileA
  1742.     DefineDosDeviceA
  1743.     DeleteFileA
  1744.     GetDriveTypeA
  1745.     GetLogicalDriveStringsA
  1746.     GetVolumeInformationA
  1747.     MoveFileA
  1748.     MoveFileExA
  1749.     QueryDosDeviceA
  1750.  
  1751. =item C<":FuncW">
  1752.  
  1753. The wide-character-specific (Unicode) functions.  Each of these is
  1754. just the same as the version without the trailing "W" except that
  1755. strings are expected in Unicode and some lengths are measured as
  1756. number of C<WCHAR>s instead of number of bytes, as indicated below.
  1757.  
  1758. =over
  1759.  
  1760. =item CopyFileW
  1761.  
  1762. =item C<CopyFileW( $swOldFileName, $swNewFileName, $bFailIfExists )>
  1763.  
  1764. C<$swOldFileName> and C<$swNewFileName> are Unicode strings.
  1765.  
  1766. =item CreateFileW
  1767.  
  1768. =item C<$hObject= CreateFileW( $swPath, $uAccess, $uShare, $pSecAttr, $uCreate, $uFlags, $hModel )>
  1769.  
  1770. C<$swPath> is Unicode.
  1771.  
  1772. =item DefineDosDeviceW
  1773.  
  1774. =item C<DefineDosDeviceW( $uFlags, $swDosDeviceName, $swTargetPath )>
  1775.  
  1776. C<$swDosDeviceName> and C<$swTargetPath> are Unicode.
  1777.  
  1778. =item DeleteFileW
  1779.  
  1780. =item C<DeleteFileW( $swFileName )>
  1781.  
  1782. C<$swFileName> is Unicode.
  1783.  
  1784. =item GetDriveTypeW
  1785.  
  1786. =item C<$uDriveType= GetDriveTypeW( $swRootPath )>
  1787.  
  1788. C<$swRootPath> is Unicode.
  1789.  
  1790. =item GetLogicalDriveStringsW
  1791.  
  1792. =item C<$olwOutLength= GetLogicalDriveStringsW( $lwBufSize, $oswBuffer )>
  1793.  
  1794. Unicode is stored in C<$oswBuffer>.  C<$lwBufSize> and C<$olwOutLength>
  1795. are measured as number of C<WCHAR>s.
  1796.  
  1797. =item GetVolumeInformationW
  1798.  
  1799. =item C<GetVolumeInformationW( $swRootPath, $oswVolName, $lwVolName, $ouSerialNum, $ouMaxNameLen, $ouFsFlags, $oswFsType, $lwFsType )>
  1800.  
  1801. C<$swRootPath> is Unicode and Unicode is written to C<$oswVolName> and
  1802. C<$oswFsType>.  C<$lwVolName> and C<$lwFsType> are measures as number
  1803. of C<WCHAR>s.
  1804.  
  1805. =item MoveFileW
  1806.  
  1807. =item C<MoveFileW( $swOldName, $swNewName )>
  1808.  
  1809. C<$swOldName> and C<$swNewName> are Unicode.
  1810.  
  1811. =item MoveFileExW
  1812.  
  1813. =item C<MoveFileExW( $swOldName, $swNewName, $uFlags )>
  1814.  
  1815. C<$swOldName> and C<$swNewName> are Unicode.
  1816.  
  1817. =item QueryDosDeviceW
  1818.  
  1819. =item C<$olwTargetLen= QueryDosDeviceW( $swDeviceName, $oswTargetPath, $lwTargetBuf )>
  1820.  
  1821. C<$swDeviceName> is Unicode and Unicode is written to
  1822. C<$oswTargetPath>.  C<$lwTargetBuf> and C<$olwTargetLen> are measured
  1823. as number of  C<WCHAR>s.
  1824.  
  1825. =back
  1826.  
  1827. =item C<":Misc">
  1828.  
  1829. Miscellaneous constants.  Used for the C<$uCreate> argument of
  1830. C<CreateFile> or the C<$uFromWhere> argument of C<SetFilePointer>.
  1831. Plus C<INVALID_HANDLE_VALUE>, which you usually won't need to check
  1832. for since most routines translate it into a false value.
  1833.  
  1834.     CREATE_ALWAYS        CREATE_NEW        OPEN_ALWAYS
  1835.     OPEN_EXISTING        TRUNCATE_EXISTING    INVALID_HANDLE_VALUE
  1836.     FILE_BEGIN        FILE_CURRENT        FILE_END
  1837.  
  1838. =item C<":DDD_">
  1839.  
  1840. Constants for the C<$uFlags> argument of C<DefineDosDevice>.
  1841.  
  1842.     DDD_EXACT_MATCH_ON_REMOVE
  1843.     DDD_RAW_TARGET_PATH
  1844.     DDD_REMOVE_DEFINITION
  1845.  
  1846. =item C<":DRIVE_">
  1847.  
  1848. Constants returned by C<GetDriveType>.
  1849.  
  1850.     DRIVE_UNKNOWN        DRIVE_NO_ROOT_DIR    DRIVE_REMOVABLE
  1851.     DRIVE_FIXED        DRIVE_REMOTE        DRIVE_CDROM
  1852.     DRIVE_RAMDISK
  1853.  
  1854. =item C<":FILE_">
  1855.  
  1856. Specific types of access to files that can be requested via the
  1857. C<$uAccess> argument to C<CreateFile>.
  1858.  
  1859.     FILE_READ_DATA            FILE_LIST_DIRECTORY
  1860.     FILE_WRITE_DATA            FILE_ADD_FILE
  1861.     FILE_APPEND_DATA        FILE_ADD_SUBDIRECTORY
  1862.     FILE_CREATE_PIPE_INSTANCE    FILE_READ_EA
  1863.     FILE_WRITE_EA            FILE_EXECUTE
  1864.     FILE_TRAVERSE            FILE_DELETE_CHILD
  1865.     FILE_READ_ATTRIBUTES        FILE_WRITE_ATTRIBUTES
  1866.     FILE_ALL_ACCESS            FILE_GENERIC_READ
  1867.     FILE_GENERIC_WRITE        FILE_GENERIC_EXECUTE )],
  1868.  
  1869. =item C<":FILE_ATTRIBUTE_">
  1870.  
  1871. File attribute constants.  Returned by C<attrLetsToBits> and used in
  1872. the C<$uFlags> argument to C<CreateFile>.
  1873.  
  1874.     FILE_ATTRIBUTE_ARCHIVE        FILE_ATTRIBUTE_COMPRESSED
  1875.     FILE_ATTRIBUTE_HIDDEN        FILE_ATTRIBUTE_NORMAL
  1876.     FILE_ATTRIBUTE_OFFLINE        FILE_ATTRIBUTE_READONLY
  1877.     FILE_ATTRIBUTE_SYSTEM        FILE_ATTRIBUTE_TEMPORARY
  1878.  
  1879. =item C<":FILE_FLAG_">
  1880.  
  1881. File option flag constants.  Used in the C<$uFlags> argument to
  1882. C<CreateFile>.
  1883.  
  1884.     FILE_FLAG_BACKUP_SEMANTICS    FILE_FLAG_DELETE_ON_CLOSE
  1885.     FILE_FLAG_NO_BUFFERING        FILE_FLAG_OVERLAPPED
  1886.     FILE_FLAG_POSIX_SEMANTICS    FILE_FLAG_RANDOM_ACCESS
  1887.     FILE_FLAG_SEQUENTIAL_SCAN    FILE_FLAG_WRITE_THROUGH
  1888.  
  1889. =item C<":FILE_SHARE_">
  1890.  
  1891. File sharing constants.  Used in the C<$uShare> argument to
  1892. C<CreateFile>.
  1893.  
  1894.     FILE_SHARE_DELETE    FILE_SHARE_READ        FILE_SHARE_WRITE
  1895.  
  1896. =item C<":FILE_TYPE_">
  1897.  
  1898. File type constants.  Returned by C<GetFileType>.
  1899.  
  1900.     FILE_TYPE_CHAR        FILE_TYPE_DISK
  1901.     FILE_TYPE_PIPE        FILE_TYPE_UNKNOWN
  1902.  
  1903. =item C<":FS_">
  1904.  
  1905. File system characteristics constants.  Placed in the C<$ouFsFlags>
  1906. argument to C<GetVolumeInformation>.
  1907.  
  1908.     FS_CASE_IS_PRESERVED        FS_CASE_SENSITIVE
  1909.     FS_UNICODE_STORED_ON_DISK    FS_PERSISTENT_ACLS 
  1910.     FS_FILE_COMPRESSION        FS_VOL_IS_COMPRESSED
  1911.  
  1912. =item C<":HANDLE_FLAG_">
  1913.  
  1914. Flag bits modifying the behavior of an object handle and accessed via
  1915. C<GetHandleInformation> and C<SetHandleInformation>.
  1916.  
  1917. =over
  1918.  
  1919. =item HANDLE_FLAG_INHERIT
  1920.  
  1921. If this bit is set, then children of this process who inherit handles
  1922. [that is, processes created by calls to the Win32 C<CreateProcess> API
  1923. with the C<bInheritHandles> parameter specified as C<TRUE>], will inherit
  1924. this particular object handle.
  1925.  
  1926. =item HANDLE_FLAG_PROTECT_FROM_CLOSE
  1927.  
  1928. If this bit is set, then calls to C<CloseHandle> against this handle
  1929. will be ignored, leaving the handle open and usable.
  1930.  
  1931. =back
  1932.  
  1933. =item C<":IOCTL_STORAGE_">
  1934.  
  1935. I/O control operations for generic storage devices.  Used in the
  1936. C<$uIoControlCode> argument to C<DeviceIoControl>.  Includes
  1937. C<IOCTL_STORAGE_CHECK_VERIFY>, C<IOCTL_STORAGE_MEDIA_REMOVAL>,
  1938. C<IOCTL_STORAGE_EJECT_MEDIA>, C<IOCTL_STORAGE_LOAD_MEDIA>,
  1939. C<IOCTL_STORAGE_RESERVE>, C<IOCTL_STORAGE_RELEASE>,
  1940. C<IOCTL_STORAGE_FIND_NEW_DEVICES>, and
  1941. C<IOCTL_STORAGE_GET_MEDIA_TYPES>.
  1942.  
  1943. =over
  1944.  
  1945. =item C<IOCTL_STORAGE_CHECK_VERIFY>
  1946.  
  1947. Verify that a device's media is accessible.  C<$pInBuf> and C<$opOutBuf>
  1948. should both be C<[]>.  If C<DeviceIoControl> returns a true value, then
  1949. the media is currently accessible.
  1950.  
  1951. =item C<IOCTL_STORAGE_MEDIA_REMOVAL>
  1952.  
  1953. Allows the device's media to be locked or unlocked.  C<$opOutBuf> should
  1954. be C<[]>.  C<$pInBuf> should be a C<PREVENT_MEDIA_REMOVAL> data structure,
  1955. which is simply an interger containing a boolean value:
  1956.  
  1957.     $pInBuf= pack( "i", $bPreventMediaRemoval );
  1958.  
  1959. =item C<IOCTL_STORAGE_EJECT_MEDIA>
  1960.  
  1961. Requests that the device eject the media.  C<$pInBuf> and C<$opOutBuf>
  1962. should both be C<[]>.  
  1963.  
  1964. =item C<IOCTL_STORAGE_LOAD_MEDIA>
  1965.  
  1966. Requests that the device load the media.  C<$pInBuf> and C<$opOutBuf>
  1967. should both be C<[]>.
  1968.  
  1969. =item C<IOCTL_STORAGE_RESERVE>
  1970.  
  1971. Requests that the device be reserved.  C<$pInBuf> and C<$opOutBuf>
  1972. should both be C<[]>.
  1973.  
  1974. =item C<IOCTL_STORAGE_RELEASE>
  1975.  
  1976. Releases a previous device reservation.  C<$pInBuf> and C<$opOutBuf>
  1977. should both be C<[]>.
  1978.  
  1979. =item C<IOCTL_STORAGE_FIND_NEW_DEVICES>
  1980.  
  1981. No documentation on this IOCTL operation was found.
  1982.  
  1983. =item C<IOCTL_STORAGE_GET_MEDIA_TYPES>
  1984.  
  1985. Requests information about the type of media supported by the device. 
  1986. C<$pInBuf> should be C<[]>.  C<$opOutBuf> will be set to contain a
  1987. vector of C<DISK_GEOMETRY> data structures, which can be decoded via:
  1988.  
  1989.     # Calculate the number of DISK_GEOMETRY structures returned:
  1990.     my $cStructs= length($opOutBuf)/(4+4+4+4+4+4);
  1991.     my @fields= unpack( "L l I L L L" x $cStructs, $opOutBuf )
  1992.     my( @ucCylsLow, @ivcCylsHigh, @uMediaType, @uTracksPerCyl,
  1993.       @uSectsPerTrack, @uBytesPerSect )= ();
  1994.     while(  @fields  ) {
  1995.     push( @ucCylsLow, unshift @fields );
  1996.     push( @ivcCylsHigh, unshift @fields );
  1997.     push( @uMediaType, unshift @fields );
  1998.     push( @uTracksPerCyl, unshift @fields );
  1999.     push( @uSectsPerTrack, unshift @fields );
  2000.     push( @uBytesPerSect, unshift @fields );
  2001.     }
  2002.  
  2003. For the C<$i>th type of supported media, the following variables will
  2004. contain the following data.
  2005.  
  2006. =over
  2007.  
  2008. =item C<$ucCylsLow[$i]>
  2009.  
  2010. The low-order 4 bytes of the total number of cylinders.
  2011.  
  2012. =item C<$ivcCylsHigh[$i]> 
  2013.  
  2014. The high-order 4 bytes of the total number of cylinders.
  2015.  
  2016. =item C<$uMediaType[$i]>
  2017.  
  2018. A code for the type of media.  See the C<":MEDIA_TYPE"> export class.
  2019.  
  2020. =item C<$uTracksPerCyl[$i]>
  2021.  
  2022. The number of tracks in each cylinder.
  2023.  
  2024. =item C<$uSectsPerTrack[$i]>
  2025.  
  2026. The number of sectors in each track.
  2027.  
  2028. =item C<$uBytesPerSect[$i]>
  2029.  
  2030. The number of bytes in each sector.
  2031.  
  2032. =back
  2033.  
  2034. =back
  2035.  
  2036. =item C<":IOCTL_DISK_">
  2037.  
  2038. I/O control operations for disk devices.  Used in the C<$uIoControlCode>
  2039. argument to C<DeviceIoControl>.  Most of these are to be used on
  2040. physical drive devices like C<"//./PhysicalDrive0">.  However,
  2041. C<IOCTL_DISK_GET_PARTITION_INFO> and C<IOCTL_DISK_SET_PARTITION_INFO>
  2042. should only be used on a single-partition device like C<"//./C:">.  Also,
  2043. C<IOCTL_DISK_GET_MEDIA_TYPES> is documented as having been superceded but
  2044. is still useful when used on a floppy device like C<"//./A:">.
  2045.  
  2046. Includes C<IOCTL_DISK_FORMAT_TRACKS>, C<IOCTL_DISK_FORMAT_TRACKS_EX>,
  2047. C<IOCTL_DISK_GET_DRIVE_GEOMETRY>, C<IOCTL_DISK_GET_DRIVE_LAYOUT>,
  2048. C<IOCTL_DISK_GET_MEDIA_TYPES>, C<IOCTL_DISK_GET_PARTITION_INFO>,
  2049. C<IOCTL_DISK_HISTOGRAM_DATA>, C<IOCTL_DISK_HISTOGRAM_RESET>,
  2050. C<IOCTL_DISK_HISTOGRAM_STRUCTURE>, C<IOCTL_DISK_IS_WRITABLE>,
  2051. C<IOCTL_DISK_LOGGING>, C<IOCTL_DISK_PERFORMANCE>,
  2052. C<IOCTL_DISK_REASSIGN_BLOCKS>, C<IOCTL_DISK_REQUEST_DATA>,
  2053. C<IOCTL_DISK_REQUEST_STRUCTURE>, C<IOCTL_DISK_SET_DRIVE_LAYOUT>,
  2054. C<IOCTL_DISK_SET_PARTITION_INFO>, and C<IOCTL_DISK_VERIFY>.
  2055.  
  2056. =over
  2057.  
  2058. =item C<IOCTL_DISK_GET_DRIVE_GEOMETRY>
  2059.  
  2060. Request information about the size and geometry of the disk.  C<$pInBuf>
  2061. should be C<[]>.  C<$opOutBuf> will be set to a C<DISK_GEOMETRY> data
  2062. structure which can be decode via:
  2063.  
  2064.     ( $ucCylsLow, $ivcCylsHigh, $uMediaType, $uTracksPerCyl,
  2065.       $uSectsPerTrack, $uBytesPerSect )= unpack( "L l I L L L", $opOutBuf );
  2066.  
  2067. =over
  2068.  
  2069. =item C<$ucCylsLow>
  2070.  
  2071. The low-order 4 bytes of the total number of cylinders.
  2072.  
  2073. =item C<$ivcCylsHigh> 
  2074.  
  2075. The high-order 4 bytes of the total number of cylinders.
  2076.  
  2077. =item C<$uMediaType>
  2078.  
  2079. A code for the type of media.  See the C<":MEDIA_TYPE"> export class.
  2080.  
  2081. =item C<$uTracksPerCyl>
  2082.  
  2083. The number of tracks in each cylinder.
  2084.  
  2085. =item C<$uSectsPerTrack>
  2086.  
  2087. The number of sectors in each track.
  2088.  
  2089. =item C<$uBytesPerSect>
  2090.  
  2091. The number of bytes in each sector.
  2092.  
  2093. =back
  2094.  
  2095. =item C<IOCTL_DISK_GET_PARTITION_INFO>
  2096.  
  2097. Request information about the size and geometry of the partition. 
  2098. C<$pInBuf> should be C<[]>.  C<$opOutBuf> will be set to a
  2099. C<PARTITION_INFORMATION> data structure which can be decode via:
  2100.  
  2101.     ( $uStartLow, $ivStartHigh, $ucHiddenSects, $uPartitionSeqNumber,
  2102.       $uPartitionType, $bActive, $bRecognized, $bToRewrite )=
  2103.       unpack( "L l L L C c c c", $opOutBuf );
  2104.  
  2105. =over
  2106.  
  2107. =item C<$uStartLow> and C<$ivStartHigh>
  2108.  
  2109. The low-order and high-order [respectively] 4 bytes of the starting
  2110. offset of the partition, measured in bytes.
  2111.  
  2112. =item C<$ucHiddenSects>
  2113.  
  2114. The number of "hidden" sectors for this partition.  Actually this is
  2115. the number of sectors found prior to this partiton, that is, the
  2116. starting offset [as found in C<$uStartLow> and C<$ivStartHigh>]
  2117. divided by the number of bytes per sector.
  2118.  
  2119. =item C<$uPartitionSeqNumber>
  2120.  
  2121. The sequence number of this partition.  Partitions are numbered
  2122. starting as C<1> [with "partition 0" meaning the entire disk].  
  2123. Sometimes this field may be C<0> and you'll have to infer the
  2124. partition sequence number from how many partitions preceed it on
  2125. the disk.
  2126.  
  2127. =item C<$uPartitionType>
  2128.  
  2129. The type of partition.  See the C<":PARTITION_"> export class for a
  2130. list of known types.  See also C<IsRecognizedPartition> and
  2131. C<IsContainerPartition>.
  2132.  
  2133. =item C<$bActive>
  2134.  
  2135. C<1> for the active [boot] partition, C<0> otherwise.
  2136.  
  2137. =item C<$bRecognized>
  2138.  
  2139. Whether this type of partition is support under Win32.
  2140.  
  2141. =item C<$bToRewrite>
  2142.  
  2143. Whether to update this partition information.  This field is not used
  2144. by C<IOCTL_DISK_GET_PARTITION_INFO>.  For
  2145. C<IOCTL_DISK_SET_DRIVE_LAYOUT>, you must set this field to a true
  2146. value for any partitions you wish to have changed, added, or deleted.
  2147.  
  2148. =back
  2149.  
  2150. =item C<IOCTL_DISK_SET_PARTITION_INFO>
  2151.  
  2152. Change the type of the partition.  C<$opOutBuf> should be C<[]>.
  2153. C<$pInBuf> should be a C<SET_PARTITION_INFORMATION> data structure
  2154. which is just a single byte containing the new parition type [see
  2155. the C<":PARTITION_"> export class for a list of known types]:
  2156.  
  2157.     $pInBuf= pack( "C", $uPartitionType );
  2158.  
  2159. =item C<IOCTL_DISK_GET_DRIVE_LAYOUT>
  2160.  
  2161. Request information about the disk layout.  C<$pInBuf> should be C<[]>.
  2162. C<$opOutBuf> will be set to contain C<DRIVE_LAYOUT_INFORMATION>
  2163. structure including several C<PARTITION_INFORMATION> structures:
  2164.  
  2165.     my( $cPartitions, $uDiskSignature )= unpack( "L L", $opOutBuf );
  2166.     my @fields= unpack( "x8" . ( "L l L L C c c c" x $cPartitions ),
  2167.                 $opOutBuf );
  2168.     my( @uStartLow, @ivStartHigh, @ucHiddenSects,
  2169.       @uPartitionSeqNumber, @uPartitionType, @bActive,
  2170.       @bRecognized, @bToRewrite )= ();
  2171.     for(  1..$cPartition  ) {
  2172.     push( @uStartLow, unshift @fields );
  2173.     push( @ivStartHigh, unshift @fields );
  2174.     push( @ucHiddenSects, unshift @fields );
  2175.     push( @uPartitionSeqNumber, unshift @fields );
  2176.     push( @uPartitionType, unshift @fields );
  2177.     push( @bActive, unshift @fields );
  2178.     push( @bRecognized, unshift @fields );
  2179.     push( @bToRewrite, unshift @fields );
  2180.     }
  2181.  
  2182. =over
  2183.  
  2184. =item C<$cPartitions>
  2185.  
  2186. If the number of partitions on the disk.
  2187.  
  2188. =item C<$uDiskSignature>
  2189.  
  2190. Is the disk signature, a unique number assigned by Disk Administrator
  2191. [F<WinDisk.exe>] and used to identify the disk.  This allows drive
  2192. letters for partitions on that disk to remain constant even if the
  2193. SCSI Target ID of the disk gets changed.
  2194.  
  2195. =back
  2196.  
  2197. See C<IOCTL_DISK_GET_PARTITION_INFORMATION> for information on the
  2198. remaining these fields.
  2199.  
  2200. =item C<IOCTL_DISK_GET_MEDIA_TYPES>
  2201.  
  2202. Is supposed to be superseded by C<IOCTL_STORAGE_GET_MEDIA_TYPES> but
  2203. is still useful for determining the types of floppy diskette formats
  2204. that can be produced by a given floppy drive.  See
  2205. F<ex/FormatFloppy.plx> for an example.
  2206.  
  2207. =item C<IOCTL_DISK_SET_DRIVE_LAYOUT>
  2208.  
  2209. Change the partition layout of the disk.  C<$pOutBuf> should be C<[]>.
  2210. C<$pInBuf> should be a C<DISK_LAYOUT_INFORMATION> data structure
  2211. including several C<PARTITION_INFORMATION> data structures.
  2212.  
  2213.     # Already set:  $cPartitions, $uDiskSignature, @uStartLow, @ivStartHigh,
  2214.     #   @ucHiddenSects, @uPartitionSeqNumber, @uPartitionType, @bActive,
  2215.     #   @bRecognized, and @bToRewrite.
  2216.     my( @fields, $prtn )= ();
  2217.     for $prtn (  1..$cPartition  ) {
  2218.     push( @fields, $uStartLow[$prtn-1], $ivStartHigh[$prtn-1],
  2219.         $ucHiddenSects[$prtn-1], $uPartitionSeqNumber[$prtn-1],
  2220.         $uPartitionType[$prtn-1], $bActive[$prtn-1],
  2221.         $bRecognized[$prtn-1], $bToRewrite[$prtn-1] );
  2222.     }
  2223.     $pInBuf= pack( "L L" . ( "L l L L C c c c" x $cPartitions ),
  2224.            $cPartitions, $uDiskSignature, @fields );
  2225.  
  2226. To delete a partition, zero out all fields except for C<$bToRewrite>
  2227. which should be set to C<1>.  To add a partition, increment
  2228. C<$cPartitions> and add the information for the new partition
  2229. into the arrays, making sure that you insert C<1> into @bToRewrite.
  2230.  
  2231. See C<IOCTL_DISK_GET_DRIVE_LAYOUT> and
  2232. C<IOCTL_DISK_GET_PARITITON_INFORMATION> for descriptions of the
  2233. fields.
  2234.  
  2235. =item C<IOCTL_DISK_VERIFY>
  2236.  
  2237. Performs a logical format of [part of] the disk.  C<$opOutBuf> should
  2238. be C<[]>.  C<$pInBuf> should contain a C<VERIFY_INFORMATION> data
  2239. structure:
  2240.  
  2241.     $pInBuf= pack( "L l L",
  2242.            $uStartOffsetLow, $ivStartOffsetHigh, $uLength );
  2243.  
  2244. =over
  2245.  
  2246. =item C<$uStartOffsetLow> and C<$ivStartOffsetHigh>
  2247.  
  2248. The low-order and high-order [respectively] 4 bytes of the offset [in
  2249. bytes] where the formatting should begin.
  2250.  
  2251. =item C<$uLength>
  2252.  
  2253. The length [in bytes] of the section to be formatted.
  2254.  
  2255. =back
  2256.  
  2257. =item C<IOCTL_DISK_FORMAT_TRACKS>
  2258.  
  2259. Format a range of tracks on the disk.  C<$opOutBuf> should be C<[]>. 
  2260. C<$pInBuf> should contain a C<FORMAT_PARAMETERS> data structure:
  2261.  
  2262.     $pInBuf= pack( "L L L L L", $uMediaType,
  2263.            $uStartCyl, $uEndCyl, $uStartHead, $uEndHead );
  2264.  
  2265. C<$uMediaType> if the type of media to be formatted.  Mostly used to
  2266. specify the density to use when formatting a floppy diskette.  See the
  2267. C<":MEDIA_TYPE"> export class for more information.
  2268.  
  2269. The remaining fields specify the starting and ending cylinder and
  2270. head of the range of tracks to be formatted.
  2271.  
  2272. =item C<IOCTL_DISK_REASSIGN_BLOCKS>
  2273.  
  2274. Reassign a list of disk blocks to the disk's spare-block pool. 
  2275. C<$opOutBuf> should be C<[]>.  C<$pInBuf> should be a
  2276. C<REASSIGN_BLOCKS> data structure:
  2277.  
  2278.     $pInBuf= pack( "S S L*", 0, $cBlocks, @uBlockNumbers );
  2279.  
  2280. =item C<IOCTL_DISK_PERFORMANCE>
  2281.  
  2282. Request information about disk performance.  C<$pInBuf> should be C<[]>.
  2283. C<$opOutBuf> will be set to contain a C<DISK_PERFORMANCE> data structure:
  2284.  
  2285.     my( $ucBytesReadLow, $ivcBytesReadHigh,
  2286.     $ucBytesWrittenLow, $ivcBytesWrittenHigh,
  2287.     $uReadTimeLow, $ivReadTimeHigh,
  2288.     $uWriteTimeLow, $ivWriteTimeHigh,
  2289.     $ucReads, $ucWrites, $uQueueDepth )=
  2290.     unpack( "L l L l L l L l L L L", $opOutBuf );
  2291.  
  2292. =item C<IOCTL_DISK_IS_WRITABLE>
  2293.  
  2294. No documentation on this IOCTL operation was found.
  2295.  
  2296. =item C<IOCTL_DISK_LOGGING>
  2297.  
  2298. Control disk logging.  Little documentation for this IOCTL operation
  2299. was found.  It makes use of a C<DISK_LOGGING> data structure:
  2300.  
  2301. =over
  2302.  
  2303. =item DISK_LOGGING_START
  2304.  
  2305. Start logging each disk request in a buffer internal to the disk device
  2306. driver of size C<$uLogBufferSize>:
  2307.  
  2308.     $pInBuf= pack( "C L L", 0, 0, $uLogBufferSize );
  2309.  
  2310. =item DISK_LOGGING_STOP
  2311.  
  2312. Stop loggin each disk request:
  2313.  
  2314.     $pInBuf= pack( "C L L", 1, 0, 0 );
  2315.  
  2316. =item DISK_LOGGING_DUMP
  2317.  
  2318. Copy the interal log into the supplied buffer:
  2319.  
  2320.     $pLogBuffer= ' ' x $uLogBufferSize
  2321.     $pInBuf= pack( "C P L", 2, $pLogBuffer, $uLogBufferSize );
  2322.  
  2323.     ( $uByteOffsetLow[$i], $ivByteOffsetHigh[$i],
  2324.       $uStartTimeLow[$i], $ivStartTimeHigh[$i],
  2325.       $uEndTimeLog[$i], $ivEndTimeHigh[$i],
  2326.       $hVirtualAddress[$i], $ucBytes[$i],
  2327.       $uDeviceNumber[$i], $bWasReading[$i] )=
  2328.       unpack( "x".(8+8+8+4+4+1+1+2)." L l L l L l L L C c x2", $pLogBuffer );
  2329.  
  2330. =item DISK_LOGGING_BINNING
  2331.  
  2332. Keep statics grouped into bins based on request sizes.
  2333.  
  2334.     $pInBuf= pack( "C P L", 3, $pUnknown, $uUnknownSize );
  2335.  
  2336. =back
  2337.  
  2338. =item C<IOCTL_DISK_FORMAT_TRACKS_EX>
  2339.  
  2340. No documentation on this IOCTL is included.
  2341.  
  2342. =item C<IOCTL_DISK_HISTOGRAM_STRUCTURE>
  2343.  
  2344. No documentation on this IOCTL is included.
  2345.  
  2346. =item C<IOCTL_DISK_HISTOGRAM_DATA>
  2347.  
  2348. No documentation on this IOCTL is included.
  2349.  
  2350. =item C<IOCTL_DISK_HISTOGRAM_RESET>
  2351.  
  2352. No documentation on this IOCTL is included.
  2353.  
  2354. =item C<IOCTL_DISK_REQUEST_STRUCTURE>
  2355.  
  2356. No documentation on this IOCTL operation was found.
  2357.  
  2358. =item C<IOCTL_DISK_REQUEST_DATA>
  2359.  
  2360. No documentation on this IOCTL operation was found.
  2361.  
  2362. =back
  2363.  
  2364. =item C<":GENERIC_">
  2365.  
  2366. Constants specifying generic access permissions that are not specific
  2367. to one type of object.
  2368.  
  2369.     GENERIC_ALL            GENERIC_EXECUTE
  2370.     GENERIC_READ            GENERIC_WRITE
  2371.  
  2372. =item C<":MEDIA_TYPE">
  2373.  
  2374. Different classes of media that a device can support.  Used in the
  2375. C<$uMediaType> field of a C<DISK_GEOMETRY> structure.
  2376.  
  2377. =over
  2378.  
  2379. =item C<Unknown>
  2380.  
  2381. Format is unknown.
  2382.  
  2383. =item C<F5_1Pt2_512>
  2384.  
  2385. 5.25" floppy, 1.2MB [really 1,200KB] total space, 512 bytes/sector.
  2386.  
  2387. =item C<F3_1Pt44_512>
  2388.  
  2389. 3.5" floppy, 1.44MB [really 1,440KB] total space, 512 bytes/sector.
  2390.  
  2391. =item C<F3_2Pt88_512>
  2392.  
  2393. 3.5" floppy, 2.88MB [really 2,880KB] total space, 512 bytes/sector.
  2394.  
  2395. =item C<F3_20Pt8_512>
  2396.  
  2397. 3.5" floppy, 20.8MB total space, 512 bytes/sector.
  2398.  
  2399. =item C<F3_720_512>
  2400.  
  2401. 3.5" floppy, 720KB total space, 512 bytes/sector.
  2402.  
  2403. =item C<F5_360_512>
  2404.  
  2405. 5.25" floppy, 360KB total space, 512 bytes/sector.
  2406.  
  2407. =item C<F5_320_512>
  2408.  
  2409. 5.25" floppy, 320KB total space, 512 bytes/sector.
  2410.  
  2411. =item C<F5_320_1024>
  2412.  
  2413. 5.25" floppy, 320KB total space, 1024 bytes/sector.
  2414.  
  2415. =item C<F5_180_512>
  2416.  
  2417. 5.25" floppy, 180KB total space, 512 bytes/sector.
  2418.  
  2419. =item C<F5_160_512>
  2420.  
  2421. 5.25" floppy, 160KB total space, 512 bytes/sector.
  2422.  
  2423. =item C<RemovableMedia>
  2424.  
  2425. Some type of removable media other than a floppy diskette.
  2426.  
  2427. =item C<FixedMedia>
  2428.  
  2429. A fixed hard disk.
  2430.  
  2431. =item C<F3_120M_512>
  2432.  
  2433. 3.5" floppy, 120MB total space.
  2434.  
  2435. =back
  2436.  
  2437. =item C<":MOVEFILE_">
  2438.  
  2439. Constants for use in C<$uFlags> arguments to C<MoveFileEx>.
  2440.  
  2441.     MOVEFILE_COPY_ALLOWED        MOVEFILE_DELAY_UNTIL_REBOOT
  2442.     MOVEFILE_REPLACE_EXISTING    MOVEFILE_WRITE_THROUGH
  2443.  
  2444. =item C<":SECURITY_">
  2445.  
  2446. Security quality of service values that can be used in the C<$uFlags>
  2447. argument to C<CreateFile> if opening the client side of a named pipe.
  2448.  
  2449.     SECURITY_ANONYMOUS        SECURITY_CONTEXT_TRACKING
  2450.     SECURITY_DELEGATION        SECURITY_EFFECTIVE_ONLY
  2451.     SECURITY_IDENTIFICATION        SECURITY_IMPERSONATION
  2452.     SECURITY_SQOS_PRESENT
  2453.  
  2454. =item C<":SEM_">
  2455.  
  2456. Constants to be used with C<SetErrorMode>.
  2457.  
  2458.     SEM_FAILCRITICALERRORS        SEM_NOGPFAULTERRORBOX
  2459.     SEM_NOALIGNMENTFAULTEXCEPT    SEM_NOOPENFILEERRORBOX
  2460.  
  2461. =item C<":PARTITION_">
  2462.  
  2463. Constants describing partition types.
  2464.  
  2465.     PARTITION_ENTRY_UNUSED        PARTITION_FAT_12
  2466.     PARTITION_XENIX_1        PARTITION_XENIX_2
  2467.     PARTITION_FAT_16        PARTITION_EXTENDED
  2468.     PARTITION_HUGE            PARTITION_IFS
  2469.     PARTITION_FAT32            PARTITION_FAT32_XINT13
  2470.     PARTITION_XINT13        PARTITION_XINT13_EXTENDED
  2471.     PARTITION_PREP            PARTITION_UNIX
  2472.     VALID_NTFT            PARTITION_NTFT
  2473.  
  2474. =item C<":ALL">
  2475.  
  2476. All of the above.
  2477.  
  2478. =back
  2479.  
  2480. =head1 BUGS
  2481.  
  2482. None known at this time.
  2483.  
  2484. =head1 AUTHOR
  2485.  
  2486. Tye McQueen, tye@metronet.com, http://www.metronet.com/~tye/.
  2487.  
  2488. =head1 SEE ALSO
  2489.  
  2490. The pyramids.
  2491.  
  2492. =cut
  2493.