1 # File.pm -- Low-level access to Win32 file/dir functions/constants.
3 package Win32API::File;
8 use Config qw( %Config );
9 use Fcntl qw( O_RDONLY O_RDWR O_WRONLY O_APPEND O_BINARY O_TEXT );
10 use vars qw( $VERSION @ISA );
11 use vars qw( @EXPORT @EXPORT_OK @EXPORT_FAIL %EXPORT_TAGS );
15 use base qw( Exporter DynaLoader Tie::Handle IO::File );
17 # Math::BigInt optimizations courtesy of Tels
20 $_64BITINT = defined($Config{use64bitint}) &&
21 ($Config{use64bitint} eq 'define');
23 require Math::BigInt unless $_64BITINT;
26 my $THIRTY_TWO = $_64BITINT ? 32 : Math::BigInt->new(32);
28 my $FFFFFFFF = $_64BITINT ? 0xFFFFFFFF : Math::BigInt->new(0xFFFFFFFF);
32 Func => [qw( attrLetsToBits createFile
33 fileConstant fileLastError getLogicalDrives
34 CloseHandle CopyFile CreateFile
35 DefineDosDevice DeleteFile DeviceIoControl
36 FdGetOsFHandle GetDriveType GetFileAttributes GetFileType
37 GetHandleInformation GetLogicalDrives GetLogicalDriveStrings
38 GetOsFHandle GetVolumeInformation IsRecognizedPartition
39 IsContainerPartition MoveFile MoveFileEx
40 OsFHandleOpen OsFHandleOpenFd QueryDosDevice
41 ReadFile SetErrorMode SetFilePointer
42 SetHandleInformation WriteFile GetFileSize
43 getFileSize setFilePointer GetOverlappedResult)],
45 CopyFileA CreateFileA DefineDosDeviceA
46 DeleteFileA GetDriveTypeA GetFileAttributesA GetLogicalDriveStringsA
47 GetVolumeInformationA MoveFileA MoveFileExA
50 CopyFileW CreateFileW DefineDosDeviceW
51 DeleteFileW GetDriveTypeW GetFileAttributesW GetLogicalDriveStringsW
52 GetVolumeInformationW MoveFileW MoveFileExW
55 CREATE_ALWAYS CREATE_NEW FILE_BEGIN
56 FILE_CURRENT FILE_END INVALID_HANDLE_VALUE
57 OPEN_ALWAYS OPEN_EXISTING TRUNCATE_EXISTING )],
59 DDD_EXACT_MATCH_ON_REMOVE DDD_RAW_TARGET_PATH
60 DDD_REMOVE_DEFINITION )],
62 DRIVE_UNKNOWN DRIVE_NO_ROOT_DIR DRIVE_REMOVABLE
63 DRIVE_FIXED DRIVE_REMOTE DRIVE_CDROM
66 FILE_READ_DATA FILE_LIST_DIRECTORY
67 FILE_WRITE_DATA FILE_ADD_FILE
68 FILE_APPEND_DATA FILE_ADD_SUBDIRECTORY
69 FILE_CREATE_PIPE_INSTANCE FILE_READ_EA
70 FILE_WRITE_EA FILE_EXECUTE
71 FILE_TRAVERSE FILE_DELETE_CHILD
72 FILE_READ_ATTRIBUTES FILE_WRITE_ATTRIBUTES
73 FILE_ALL_ACCESS FILE_GENERIC_READ
74 FILE_GENERIC_WRITE FILE_GENERIC_EXECUTE )],
75 FILE_ATTRIBUTE_ => [qw(
76 INVALID_FILE_ATTRIBUTES
77 FILE_ATTRIBUTE_DEVICE FILE_ATTRIBUTE_DIRECTORY
78 FILE_ATTRIBUTE_ENCRYPTED FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
79 FILE_ATTRIBUTE_REPARSE_POINT FILE_ATTRIBUTE_SPARSE_FILE
80 FILE_ATTRIBUTE_ARCHIVE FILE_ATTRIBUTE_COMPRESSED
81 FILE_ATTRIBUTE_HIDDEN FILE_ATTRIBUTE_NORMAL
82 FILE_ATTRIBUTE_OFFLINE FILE_ATTRIBUTE_READONLY
83 FILE_ATTRIBUTE_SYSTEM FILE_ATTRIBUTE_TEMPORARY )],
85 FILE_FLAG_BACKUP_SEMANTICS FILE_FLAG_DELETE_ON_CLOSE
86 FILE_FLAG_NO_BUFFERING FILE_FLAG_OVERLAPPED
87 FILE_FLAG_POSIX_SEMANTICS FILE_FLAG_RANDOM_ACCESS
88 FILE_FLAG_SEQUENTIAL_SCAN FILE_FLAG_WRITE_THROUGH
89 FILE_FLAG_OPEN_REPARSE_POINT )],
91 FILE_SHARE_DELETE FILE_SHARE_READ FILE_SHARE_WRITE )],
93 FILE_TYPE_CHAR FILE_TYPE_DISK FILE_TYPE_PIPE
96 FS_CASE_IS_PRESERVED FS_CASE_SENSITIVE
97 FS_UNICODE_STORED_ON_DISK FS_PERSISTENT_ACLS
98 FS_FILE_COMPRESSION FS_VOL_IS_COMPRESSED )],
100 FSCTL_SET_REPARSE_POINT FSCTL_GET_REPARSE_POINT
101 FSCTL_DELETE_REPARSE_POINT )],
103 HANDLE_FLAG_INHERIT HANDLE_FLAG_PROTECT_FROM_CLOSE )],
104 IOCTL_STORAGE_ => [qw(
105 IOCTL_STORAGE_CHECK_VERIFY IOCTL_STORAGE_MEDIA_REMOVAL
106 IOCTL_STORAGE_EJECT_MEDIA IOCTL_STORAGE_LOAD_MEDIA
107 IOCTL_STORAGE_RESERVE IOCTL_STORAGE_RELEASE
108 IOCTL_STORAGE_FIND_NEW_DEVICES IOCTL_STORAGE_GET_MEDIA_TYPES
111 IOCTL_DISK_FORMAT_TRACKS IOCTL_DISK_FORMAT_TRACKS_EX
112 IOCTL_DISK_GET_DRIVE_GEOMETRY IOCTL_DISK_GET_DRIVE_LAYOUT
113 IOCTL_DISK_GET_MEDIA_TYPES IOCTL_DISK_GET_PARTITION_INFO
114 IOCTL_DISK_HISTOGRAM_DATA IOCTL_DISK_HISTOGRAM_RESET
115 IOCTL_DISK_HISTOGRAM_STRUCTURE IOCTL_DISK_IS_WRITABLE
116 IOCTL_DISK_LOGGING IOCTL_DISK_PERFORMANCE
117 IOCTL_DISK_REASSIGN_BLOCKS IOCTL_DISK_REQUEST_DATA
118 IOCTL_DISK_REQUEST_STRUCTURE IOCTL_DISK_SET_DRIVE_LAYOUT
119 IOCTL_DISK_SET_PARTITION_INFO IOCTL_DISK_VERIFY )],
121 GENERIC_ALL GENERIC_EXECUTE
122 GENERIC_READ GENERIC_WRITE )],
124 Unknown F5_1Pt2_512 F3_1Pt44_512
125 F3_2Pt88_512 F3_20Pt8_512 F3_720_512
126 F5_360_512 F5_320_512 F5_320_1024
127 F5_180_512 F5_160_512 RemovableMedia
128 FixedMedia F3_120M_512 )],
130 MOVEFILE_COPY_ALLOWED MOVEFILE_DELAY_UNTIL_REBOOT
131 MOVEFILE_REPLACE_EXISTING MOVEFILE_WRITE_THROUGH )],
133 SECURITY_ANONYMOUS SECURITY_CONTEXT_TRACKING
134 SECURITY_DELEGATION SECURITY_EFFECTIVE_ONLY
135 SECURITY_IDENTIFICATION SECURITY_IMPERSONATION
136 SECURITY_SQOS_PRESENT )],
138 SEM_FAILCRITICALERRORS SEM_NOGPFAULTERRORBOX
139 SEM_NOALIGNMENTFAULTEXCEPT SEM_NOOPENFILEERRORBOX )],
141 PARTITION_ENTRY_UNUSED PARTITION_FAT_12
142 PARTITION_XENIX_1 PARTITION_XENIX_2
143 PARTITION_FAT_16 PARTITION_EXTENDED
144 PARTITION_HUGE PARTITION_IFS
145 PARTITION_FAT32 PARTITION_FAT32_XINT13
146 PARTITION_XINT13 PARTITION_XINT13_EXTENDED
147 PARTITION_PREP PARTITION_UNIX
148 VALID_NTFT PARTITION_NTFT )],
150 STD_INPUT_HANDLE STD_OUTPUT_HANDLE
156 foreach $key ( keys(%EXPORT_TAGS) ) {
157 push( @EXPORT_OK, @{$EXPORT_TAGS{$key}} );
158 #push( @EXPORT_FAIL, @{$EXPORT_TAGS{$key}} ) unless $key =~ /^Func/;
161 $EXPORT_TAGS{ALL}= \@EXPORT_OK;
163 bootstrap Win32API::File $VERSION;
165 # Preloaded methods go here.
167 # To convert C constants to Perl code in cFile.pc
168 # [instead of C or C++ code in cFile.h]:
169 # * Modify F<Makefile.PL> to add WriteMakeFile() =>
170 # CONST2PERL/postamble => [[ "Win32API::File" => ]] WRITE_PERL => 1.
171 # * Either comment out C<#include "cFile.h"> from F<File.xs>
172 # or make F<cFile.h> an empty file.
173 # * Make sure the following C<if> block is not commented out.
174 # * "nmake clean", "perl Makefile.PL", "nmake"
176 if( ! defined &GENERIC_READ ) {
177 require "Win32API/File/cFile.pc";
183 if( 1 != @_ || ! $name || $name =~ /\W/ ) {
185 Carp::croak( 'Usage: ',__PACKAGE__,'::fileConstant("CONST_NAME")' );
187 my $proto= prototype $name;
197 # We provide this for backwards compatibility:
201 my $value= fileConstant( $name );
202 if( defined $value ) {
211 # my $code= 'return _fileLastError(@_)';
212 # local( $!, $^E )= ( 1, 1 );
215 # local( $^E )= _fileLastError(@_);
220 # eval "sub fileLastError { $code }";
224 package Win32API::File::_error;
228 require Win32 unless defined &Win32::FormatMessage;
229 $_ = Win32::FormatMessage(Win32API::File::_fileLastError());
233 '0+' => sub { Win32API::File::_fileLastError() },
236 sub new { return bless {}, shift }
237 sub set { Win32API::File::_fileLastError($_[1]); return $_[0] }
239 package Win32API::File;
241 my $_error = Win32API::File::_error->new();
244 croak 'Usage: ',__PACKAGE__,'::fileLastError( [$setWin32ErrCode] )' if @_ > 1;
245 $_error->set($_[0]) if defined $_[0];
249 # Since we ISA DynaLoader which ISA AutoLoader, we ISA AutoLoader so we
250 # need this next chunk to prevent Win32API::File->nonesuch() from
251 # looking for "nonesuch.al" and producing confusing error messages:
252 use vars qw($AUTOLOAD);
256 "Can't locate method $AUTOLOAD via package Win32API::File" );
259 # Replace "&rout;" with "goto &rout;" when that is supported on Win32.
261 # Aliases for non-Unicode functions:
262 sub CopyFile { &CopyFileA; }
263 sub CreateFile { &CreateFileA; }
264 sub DefineDosDevice { &DefineDosDeviceA; }
265 sub DeleteFile { &DeleteFileA; }
266 sub GetDriveType { &GetDriveTypeA; }
267 sub GetFileAttributes { &GetFileAttributesA; }
268 sub GetLogicalDriveStrings { &GetLogicalDriveStringsA; }
269 sub GetVolumeInformation { &GetVolumeInformationA; }
270 sub MoveFile { &MoveFileA; }
271 sub MoveFileEx { &MoveFileExA; }
272 sub QueryDosDevice { &QueryDosDeviceA; }
276 croak 'Win32API::File Usage: ',
277 'OsFHandleOpen(FILE,$hNativeHandle,"rwatb")';
279 my( $fh, $osfh, $access )= @_;
281 if( $fh !~ /('|::)/ ) {
282 $fh= caller() . "::" . $fh;
288 if( $access =~ /r/i ) {
289 if( $access =~ /w/i ) {
297 if( $access =~ /w/i ) {
301 # croak qq<Win32API::File::OsFHandleOpen(): >,
302 # qq<Access ($access) missing both "r" and "w">;
307 $mode |= O_APPEND if $access =~ /a/i;
308 #$mode |= O_TEXT if $access =~ /t/i;
309 # Some versions of the Fcntl module are broken and won't autoload O_TEXT:
310 if( $access =~ /t/i ) {
311 my $o_text= eval "O_TEXT";
312 $o_text= 0x4000 if $@;
315 $mode |= O_BINARY if $access =~ /b/i;
316 my $fd = eval { OsFHandleOpenFd( $osfh, $mode ) };
318 return tie *{$fh}, __PACKAGE__, $osfh;
320 return undef if $fd < 0;
321 return open( $fh, $pref."&=".$fd );
326 croak 'Win32API::File Usage: $OsFHandle= GetOsFHandle(FILE)';
330 if( $file !~ /('|::)/ ) {
331 $file= caller() . "::" . $file;
334 # The eval "" is necessary in Perl 5.6, avoid it otherwise.
335 my $tied = !defined($^]) || $^] < 5.008
336 ? eval "tied *{$file}"
339 if (UNIVERSAL::isa($tied => __PACKAGE__)) {
340 return $tied->win32_handle;
345 my( $fd )= fileno($file);
346 if( ! defined( $fd ) ) {
347 if( $file =~ /^\d+\Z/ ) {
350 return (); # $! should be set by fileno().
353 my $h= FdGetOsFHandle( $fd );
354 if( INVALID_HANDLE_VALUE() == $h ) {
356 } elsif( "0" eq $h ) {
363 croak 'Win32API::File Usage: $size= getFileSize($hNativeHandle)'
369 my $low_size = GetFileSize($handle, $high_size);
371 my $retval = $_64BITINT ? $high_size : Math::BigInt->new($high_size);
373 $retval <<= $THIRTY_TWO;
374 $retval += $low_size;
380 croak 'Win32API::File Usage: $pos= setFilePointer($hNativeHandle, $posl, $from_where)'
383 my ($handle, $pos, $from_where) = @_;
385 my ($pos_low, $pos_high) = ($pos, 0);
388 $pos_low = ($pos & $FFFFFFFF);
389 $pos_high = (($pos >> $THIRTY_TWO) & $FFFFFFFF);
391 elsif (UNIVERSAL::isa($pos => 'Math::BigInt')) {
392 $pos_low = ($pos & $FFFFFFFF)->numify();
393 $pos_high = (($pos >> $THIRTY_TWO) & $FFFFFFFF)->numify();
396 my $retval = SetFilePointer($handle, $pos_low, $pos_high, $from_where);
398 if (defined $pos_high && $pos_high != 0) {
400 $retval = Math::BigInt->new($retval);
401 $pos_high = Math::BigInt->new($pos_high);
404 $retval += $pos_high << $THIRTY_TWO;
414 "a"=>FILE_ATTRIBUTE_ARCHIVE(), "c"=>FILE_ATTRIBUTE_COMPRESSED(),
415 "h"=>FILE_ATTRIBUTE_HIDDEN(), "o"=>FILE_ATTRIBUTE_OFFLINE(),
416 "r"=>FILE_ATTRIBUTE_READONLY(), "s"=>FILE_ATTRIBUTE_SYSTEM(),
417 "t"=>FILE_ATTRIBUTE_TEMPORARY() );
419 foreach( split(//,$lets) ) {
420 croak "Win32API::File::attrLetsToBits: Unknown attribute letter ($_)"
421 unless exists $a{$_};
427 use vars qw( @_createFile_Opts %_createFile_Opts );
428 @_createFile_Opts= qw( Access Create Share Attributes
429 Flags Security Model );
430 @_createFile_Opts{@_createFile_Opts}= (1) x @_createFile_Opts;
435 if( 2 <= @_ && "HASH" eq ref($_[$#_]) ) {
438 my( $sPath, $svAccess, $svShare )= @_;
439 if( @_ < 1 || 3 < @_ ) {
440 croak "Win32API::File::createFile() usage: \$hObject= createFile(\n",
441 " \$sPath, [\$svAccess_qrw_ktn_ce,[\$svShare_rwd,]]",
442 " [{Option=>\$Value}] )\n",
443 " options: @_createFile_Opts\nCalled";
445 my( $create, $flags, $sec, $model )= ( "", 0, [], 0 );
447 my @err= grep( ! $_createFile_Opts{$_}, keys(%$opts) );
448 @err and croak "_createFile: Invalid options (@err)";
449 $flags= $opts->{Flags} if exists( $opts->{Flags} );
450 $flags |= attrLetsToBits( $opts->{Attributes} )
451 if exists( $opts->{Attributes} );
452 $sec= $opts->{Security} if exists( $opts->{Security} );
453 $model= $opts->{Model} if exists( $opts->{Model} );
454 $svAccess= $opts->{Access} if exists( $opts->{Access} );
455 $create= $opts->{Create} if exists( $opts->{Create} );
456 $svShare= $opts->{Share} if exists( $opts->{Share} );
458 $svAccess= "r" unless defined($svAccess);
459 $svShare= "rw" unless defined($svShare);
460 if( $svAccess =~ /^[qrw ktn ce]*$/i ) {
461 ( my $c= $svAccess ) =~ tr/qrw QRW//d;
462 $create= $c if "" ne $c && "" eq $create;
463 local( $_ )= $svAccess;
465 $svAccess |= GENERIC_READ() if /r/i;
466 $svAccess |= GENERIC_WRITE() if /w/i;
467 } elsif( "?" eq $svAccess ) {
469 "Win32API::File::createFile: \$svAccess can use the following:\n",
470 " One or more of the following:\n",
471 "\tq -- Query access (same as 0)\n",
472 "\tr -- Read access (GENERIC_READ)\n",
473 "\tw -- Write access (GENERIC_WRITE)\n",
474 " At most one of the following:\n",
475 "\tk -- Keep if exists\n",
476 "\tt -- Truncate if exists\n",
477 "\tn -- New file only (fail if file already exists)\n",
478 " At most one of the following:\n",
479 "\tc -- Create if doesn't exist\n",
480 "\te -- Existing file only (fail if doesn't exist)\n",
481 " '' is the same as 'q k e'\n",
482 " 'r' is the same as 'r k e'\n",
483 " 'w' is the same as 'w t c'\n",
484 " 'rw' is the same as 'rw k c'\n",
485 " 'rt' or 'rn' implies 'c'.\n",
486 " Or \$svAccess can be numeric.\n", "Called from";
487 } elsif( $svAccess == 0 && $svAccess !~ /^[-+.]*0/ ) {
488 croak "Win32API::File::createFile: Invalid \$svAccess ($svAccess)";
490 if( $create =~ /^[ktn ce]*$/ ) {
491 local( $_ )= $create;
492 my( $k, $t, $n, $c, $e )= ( scalar(/k/i), scalar(/t/i),
493 scalar(/n/i), scalar(/c/i), scalar(/e/i) );
494 if( 1 < $k + $t + $n ) {
495 croak "Win32API::File::createFile: \$create must not use ",
496 qq<more than one of "k", "t", and "n" ($create)>;
499 croak "Win32API::File::createFile: \$create must not use ",
500 qq<both "c" and "e" ($create)>;
502 my $r= ( $svAccess & GENERIC_READ() ) == GENERIC_READ();
503 my $w= ( $svAccess & GENERIC_WRITE() ) == GENERIC_WRITE();
504 if( ! $k && ! $t && ! $n ) {
505 if( $w && ! $r ) { $t= 1;
509 if( $c || $w && ! $e ) { $create= OPEN_ALWAYS();
510 } else { $create= OPEN_EXISTING(); }
512 if( $e ) { $create= TRUNCATE_EXISTING();
513 } else { $create= CREATE_ALWAYS(); }
515 if( ! $e ) { $create= CREATE_NEW();
517 croak "Win32API::File::createFile: \$create must not use ",
518 qq<both "n" and "e" ($create)>;
521 } elsif( "?" eq $create ) {
522 croak 'Win32API::File::createFile: $create !~ /^[ktn ce]*$/;',
523 ' pass $svAccess as "?" for more information.';
524 } elsif( $create == 0 && $create ne "0" ) {
525 croak "Win32API::File::createFile: Invalid \$create ($create)";
527 if( $svShare =~ /^[drw]*$/ ) {
528 my %s= ( "d"=>FILE_SHARE_DELETE(), "r"=>FILE_SHARE_READ(),
529 "w"=>FILE_SHARE_WRITE() );
530 my @s= split(//,$svShare);
535 } elsif( $svShare == 0 && $svShare !~ /^[-+.]*0/ ) {
536 croak "Win32API::File::createFile: Invalid \$svShare ($svShare)";
539 $sPath, $svAccess, $svShare, $sec, $create, $flags, $model );
547 if( ! GetLogicalDriveStringsA( 256, $s ) ) {
550 if( ! defined($ref) ) {
551 return split( /\0/, $s );
552 } elsif( "ARRAY" ne ref($ref) ) {
553 croak 'Usage: C<@arr= getLogicalDrives()> ',
554 'or C<getLogicalDrives(\\@arr)>', "\n";
556 @$ref= split( /\0/, $s );
560 ###############################################################################
561 # Experimental Tied Handle and Object Oriented interface. #
562 ###############################################################################
566 $class = ref $class || $class;
568 my $self = IO::File::new($class);
569 tie *$self, __PACKAGE__;
571 $self->open(@_) if @_;
577 my ($class, $win32_handle) = @_;
578 $class = ref $class || $class;
581 _win32_handle => $win32_handle,
592 # This is called for getting the tied object from hard refs to glob refs in
593 # some cases, for reasons I don't quite grok.
595 sub FETCH { return $_[0] }
599 sub win32_handle{ $_[0]->{_win32_handle}||= $_[1] }
601 # Protected accessors
603 sub _buffer { $_[0]->{_buffer} ||= $_[1] }
604 sub _binmode { $_[0]->{_binmode} ||= $_[1] }
605 sub _fileno { $_[0]->{_fileno} ||= $_[1] }
606 sub _access { $_[0]->{_access} ||= $_[1] }
607 sub _append { $_[0]->{_append} ||= $_[1] }
614 croak "Only the two argument form of open is supported at this time" if @_;
615 # FIXME: this needs to parse the full Perl open syntax in $expr
617 my ($mixed, $mode, $path) =
618 ($expr =~ /^\s* (\+)? \s* (<|>|>>)? \s* (.*?) \s*$/x);
620 croak "Unsupported open mode" if not $path;
623 my $append = $mode eq '>>' ? 1 : 0;
627 } elsif($mode eq '>') {
631 my $w32_handle = createFile($path, $access);
633 $self->win32_handle($w32_handle);
635 $self->seek(1,2) if $append;
637 $self->_access($access);
638 $self->_append($append);
648 my ($self, $buf, $len, $offset, $overlap) = @_;
651 $buf = substr($buf, $offset);
655 $len = length($buf) if not defined $len;
657 $overlap = [] if not defined $overlap;;
659 my $bytes_written = 0;
662 $self->win32_handle, $buf, $len,
663 $bytes_written, $overlap
666 return $bytes_written;
672 my $buf = join defined $, ? $, : "" => @_;
674 $buf =~ s/\012/\015\012/sg unless $self->_binmode();
676 $buf .= $\ if defined $\;
678 $self->WRITE($buf, length($buf), 0);
683 my $into = \$_[0]; shift;
684 my ($len, $offset, $overlap) = @_;
686 my $buffer = defined $self->_buffer ? $self->_buffer : "";
687 my $buf_length = length($buffer);
690 $offset = 0 if not defined $offset;
692 if ($buf_length >= $len) {
693 $data = substr($buffer, 0, $len => "");
695 $self->_buffer($buffer);
697 if ($buf_length > 0) {
699 substr($$into, $offset) = $buffer;
700 $offset += $buf_length;
706 $self->win32_handle, $data, $len,
707 $bytes_read, $overlap
711 $$into = "" if not defined $$into;
713 substr($$into, $offset) = $data;
722 while ((index $line, $/) == -1) { # read until end of line marker
723 my $char = $self->GETC();
725 last if !defined $char || $char eq '';
730 return undef if $line eq '';
739 return $self->_fileno() if defined $self->_fileno();
741 return -1 if $^O eq 'cygwin';
743 # FIXME: We don't always open the handle, better to query the handle or to set
744 # the right access info at TIEHANDLE time.
746 my $access = $self->_access();
747 my $mode = $access eq 'rw' ? O_RDWR :
748 $access eq 'w' ? O_WRONLY : O_RDONLY;
750 $mode |= O_APPEND if $self->_append();
752 $mode |= O_TEXT if not $self->_binmode();
754 return $self->_fileno ( OsfHandleOpenFd (
755 $self->win32_handle, $mode
760 my ($self, $pos, $whence) = @_;
762 $whence = 0 if not defined $whence;
763 my @file_consts = map {
765 } qw(FILE_BEGIN FILE_CURRENT FILE_END);
767 my $from_where = $file_consts[$whence];
769 return setFilePointer($self->win32_handle, $pos, $from_where);
773 # SetFilePointer with position 0 at FILE_CURRENT will return position.
774 return $_[0]->SEEK(0, 1);
780 my $current = $self->TELL() + 0;
781 my $end = getFileSize($self->win32_handle) + 0;
783 return $current == $end;
791 if (defined $self->win32_handle) {
792 $retval = CloseHandle($self->win32_handle);
794 $self->win32_handle(undef);
800 # Only close the handle on explicit close, too many problems otherwise.
805 # End of Tie/OO Interface
807 # Autoload methods go after =cut, and are processed by the autosplit program.
814 Win32API::File - Low-level access to Win32 system API calls for files/dirs.
818 use Win32API::File 0.08 qw( :ALL );
820 MoveFile( $Source, $Destination )
821 or die "Can't move $Source to $Destination: ",fileLastError(),"\n";
822 MoveFileEx( $Source, $Destination, MOVEFILE_REPLACE_EXISTING() )
823 or die "Can't move $Source to $Destination: ",fileLastError(),"\n";
828 This provides fairly low-level access to the Win32 System API
829 calls dealing with files and directories.
831 To pass in C<NULL> as the pointer to an optional buffer, pass in
832 an empty list reference, C<[]>.
834 Beyond raw access to the API calls and related constants, this module
835 handles smart buffer allocation and translation of return codes.
837 All functions, unless otherwise noted, return a true value for success
838 and a false value for failure and set C<$^E> on failure.
840 =head2 Object Oriented/Tied Handle Interface
842 WARNING: this is new code, use at your own risk.
844 This version of C<Win32API::File> can be used like an C<IO::File> object:
846 my $file = Win32API::File->new("+> foo");
848 print $file "hello there\n";
853 It also supports tying via a win32 handle (for example, from C<createFile()>):
855 tie FILE, 'Win32API::File', $win32_handle;
858 It has not been extensively tested yet and buffered I/O is not yet implemented.
862 Nothing is exported by default. The following tags can be used to
863 have large sets of symbols exported: C<":Func">, C<":FuncA">,
864 C<":FuncW">, C<":Misc">, C<":DDD_">, C<":DRIVE_">, C<":FILE_">,
865 C<":FILE_ATTRIBUTE_">, C<":FILE_FLAG_">, C<":FILE_SHARE_">,
866 C<":FILE_TYPE_">, C<":FS_">, C<":FSCTL_">, C<":HANDLE_FLAG_">,
867 C<":IOCTL_STORAGE_">, C<":IOCTL_DISK_">, C<":GENERIC_">,
868 C<":MEDIA_TYPE">, C<":MOVEFILE_">, C<":SECURITY_">, C<":SEM_">,
869 and C<":PARTITION_">.
875 The basic function names: C<attrLetsToBits>, C<createFile>,
876 C<fileConstant>, C<fileLastError>, C<getLogicalDrives>,
877 C<setFilePointer>, C<getFileSize>,
878 C<CloseHandle>, C<CopyFile>, C<CreateFile>,
879 C<DefineDosDevice>, C<DeleteFile>, C<DeviceIoControl>,
880 C<FdGetOsFHandle>, C<GetDriveType>, C<GetFileAttributes>,
881 C<GetFileSize>, C<GetFileType>, C<GetHandleInformation>,
882 C<GetLogicalDrives>, C<GetLogicalDriveStrings>, C<GetOsFHandle>,
883 C<GetOverlappedResult>, C<GetVolumeInformation>, C<IsContainerPartition>,
884 C<IsRecognizedPartition>, C<MoveFile>, C<MoveFileEx>,
885 C<OsFHandleOpen>, C<OsFHandleOpenFd>, C<QueryDosDevice>,
886 C<ReadFile>, C<SetErrorMode>, C<SetFilePointer>,
887 C<SetHandleInformation>, and C<WriteFile>.
893 =item C<$uBits= attrLetsToBits( $sAttributeLetters )>
895 Converts a string of file attribute letters into an unsigned value with
896 the corresponding bits set. C<$sAttributeLetters> should contain zero
897 or more letters from C<"achorst">:
903 C<FILE_ATTRIBUTE_ARCHIVE>
907 C<FILE_ATTRIBUTE_COMPRESSED>
911 C<FILE_ATTRIBUTE_HIDDEN>
915 C<FILE_ATTRIBUTE_OFFLINE>
919 C<FILE_ATTRIBUTE_READONLY>
923 C<FILE_ATTRIBUTE_SYSTEM>
927 C<FILE_ATTRIBUTE_TEMPORARY>
933 =item C<$hObject= createFile( $sPath )>
935 =item C<$hObject= createFile( $sPath, $rvhvOptions )>
937 =item C<$hObject= createFile( $sPath, $svAccess )>
939 =item C<$hObject= createFile( $sPath, $svAccess, $rvhvOptions )>
941 =item C<$hObject= createFile( $sPath, $svAccess, $svShare )>
943 =item C<$hObject= createFile( $sPath, $svAccess, $svShare, $rvhvOptions )>
945 This is a Perl-friendly wrapper around C<CreateFile>.
947 On failure, C<$hObject> gets set to a false value and C<regLastError()>
948 and C<$^E> are set to the reason for the failure. Otherwise,
949 C<$hObject> gets set to a Win32 native file handle which is alwasy
950 a true value [returns C<"0 but true"> in the impossible(?) case of
951 the handle having a value of C<0>].
953 C<$sPath> is the path to the file [or device, etc.] to be opened. See
954 C<CreateFile> for more information on possible special values for
957 C<$svAccess> can be a number containing the bit mask representing
958 the specific type(s) of access to the file that you desire. See the
959 C<$uAccess> parameter to C<CreateFile> for more information on these
962 More likely, C<$svAccess> is a string describing the generic type of
963 access you desire and possibly the file creation options to use. In
964 this case, C<$svAccess> should contain zero or more characters from
965 C<"qrw"> [access desired], zero or one character each from C<"ktn">
966 and C<"ce">, and optional white space. These letters stand for,
967 respectively, "Query access", "Read access", "Write access", "Keep if
968 exists", "Truncate if exists", "New file only", "Create if none", and
969 "Existing file only". Case is ignored.
971 You can pass in C<"?"> for C<$svAccess> to have an error message
972 displayed summarizing its possible values. This is very handy when
973 doing on-the-fly programming using the Perl debugger:
975 Win32API::File::createFile: $svAccess can use the following:
976 One or more of the following:
977 q -- Query access (same as 0)
978 r -- Read access (GENERIC_READ)
979 w -- Write access (GENERIC_WRITE)
980 At most one of the following:
982 t -- Truncate if exists
983 n -- New file only (fail if file already exists)
984 At most one of the following:
985 c -- Create if doesn't exist
986 e -- Existing file only (fail if doesn't exist)
987 '' is the same as 'q k e'
988 'r' is the same as 'r k e'
989 'w' is the same as 'w t c'
990 'rw' is the same as 'rw k c'
991 'rt' or 'rn' implies 'c'.
992 Or $access can be numeric.
994 C<$svAccess> is designed to be "do what I mean", so you can skip
995 the rest of its explanation unless you are interested in the complex
996 details. Note that, if you want write access to a device, you need
997 to specify C<"k"> [and perhaps C<"e">, as in C<"w ke"> or C<"rw ke">]
998 since Win32 suggests C<OPEN_EXISTING> be used when opening a device.
1004 Stands for "Query access". This is really a no-op since you always have
1005 query access when you open a file. You can specify C<"q"> to document
1006 that you plan to query the file [or device, etc.]. This is especially
1007 helpful when you don't want read nor write access since something like
1008 C<"q"> or C<"q ke"> may be easier to understand than just C<""> or C<"ke">.
1012 Stands for "Read access". Sets the C<GENERIC_READ> bit(s) in the
1013 C<$uAccess> that is passed to C<CreateFile>. This is the default
1014 access if the C<$svAccess> parameter is missing [or if it is C<undef>
1015 and C<$rvhvOptions> doesn't specify an C<"Access"> option].
1019 Stands for "Write access". Sets the C<GENERIC_WRITE> bit(s) in the
1020 C<$uAccess> that is passed to C<CreateFile>.
1024 Stands for "Keep if exists". If the requested file exists, then it is
1025 opened. This is the default unless C<GENERIC_WRITE> access has been
1026 requested but C<GENERIC_READ> access has not been requested. Contrast
1027 with C<"t"> and C<"n">.
1031 Stands for "Truncate if exists". If the requested file exists, then
1032 it is truncated to zero length and then opened. This is the default if
1033 C<GENERIC_WRITE> access has been requested and C<GENERIC_READ> access
1034 has not been requested. Contrast with C<"k"> and C<"n">.
1038 Stands for "New file only". If the requested file exists, then it is
1039 not opened and the C<createFile> call fails. Contrast with C<"k"> and
1040 C<"t">. Can't be used with C<"e">.
1044 Stands for "Create if none". If the requested file does not
1045 exist, then it is created and then opened. This is the default
1046 if C<GENERIC_WRITE> access has been requested or if C<"t"> or
1047 C<"n"> was specified. Contrast with C<"e">.
1051 Stands for "Existing file only". If the requested file does not
1052 exist, then nothing is opened and the C<createFile> call fails. This
1053 is the default unless C<GENERIC_WRITE> access has been requested or
1054 C<"t"> or C<"n"> was specified. Contrast with C<"c">. Can't be
1059 The characters from C<"ktn"> and C<"ce"> are combined to determine the
1060 what value for C<$uCreate> to pass to C<CreateFile> [unless overridden
1061 by C<$rvhvOptions>]:
1075 C<TRUNCATE_EXISTING>
1091 C<$svShare> controls how the file is shared, that is, whether other
1092 processes can have read, write, and/or delete access to the file while
1093 we have it opened. C<$svShare> will usually be a string containing zero
1094 or more characters from C<"rwd"> but can also be a numeric bit mask.
1096 C<"r"> sets the C<FILE_SHARE_READ> bit which allows other processes to have
1097 read access to the file. C<"w"> sets the C<FILE_SHARE_WRITE> bit which
1098 allows other processes to have write access to the file. C<"d"> sets the
1099 C<FILE_SHARE_DELETE> bit which allows other processes to have delete access
1100 to the file [ignored under Windows 95].
1102 The default for C<$svShare> is C<"rw"> which provides the same sharing as
1103 using regular perl C<open()>.
1105 If another process currently has read, write, and/or delete access to
1106 the file and you don't allow that level of sharing, then your call to
1107 C<createFile> will fail. If you requested read, write, and/or delete
1108 access and another process already has the file open but doesn't allow
1109 that level of sharing, then your call to C<createFile> will fail. Once
1110 you have the file open, if another process tries to open it with read,
1111 write, and/or delete access and you don't allow that level of sharing,
1112 then that process won't be allowed to open the file.
1114 C<$rvhvOptions> is a reference to a hash where any keys must be from
1115 the list C<qw( Access Create Share Attributes Flags Security Model )>.
1116 The meaning of the value depends on the key name, as described below.
1117 Any option values in C<$rvhvOptions> override the settings from
1118 C<$svAccess> and C<$svShare> if they conflict.
1122 =item Flags => $uFlags
1124 C<$uFlags> is an unsigned value having any of the C<FILE_FLAG_*> or
1125 C<FILE_ATTRIBUTE_*> bits set. Any C<FILE_ATTRIBUTE_*> bits set via the
1126 C<Attributes> option are logically C<or>ed with these bits. Defaults
1129 If opening the client side of a named pipe, then you can also specify
1130 C<SECURITY_SQOS_PRESENT> along with one of the other C<SECURITY_*>
1131 constants to specify the security quality of service to be used.
1133 =item Attributes => $sAttributes
1135 A string of zero or more characters from C<"achorst"> [see C<attrLetsToBits>
1136 for more information] which are converted to C<FILE_ATTRIBUTE_*> bits to
1137 be set in the C<$uFlags> argument passed to C<CreateFile>.
1139 =item Security => $pSecurityAttributes
1141 C<$pSecurityAttributes> should contain a C<SECURITY_ATTRIBUTES> structure
1142 packed into a string or C<[]> [the default].
1144 =item Model => $hModelFile
1146 C<$hModelFile> should contain a handle opened with C<GENERIC_READ>
1147 access to a model file from which file attributes and extended attributes
1148 are to be copied. Or C<$hModelFile> can be C<0> [the default].
1150 =item Access => $sAccess
1152 =item Access => $uAccess
1154 C<$sAccess> should be a string of zero or more characters from
1155 C<"qrw"> specifying the type of access desired: "query" or C<0>,
1156 "read" or C<GENERIC_READ> [the default], or "write" or
1159 C<$uAccess> should be an unsigned value containing bits set to
1160 indicate the type of access desired. C<GENERIC_READ> is the default.
1162 =item Create => $sCreate
1164 =item Create => $uCreate
1166 C<$sCreate> should be a string constaing zero or one character from
1167 C<"ktn"> and zero or one character from C<"ce">. These stand for
1168 "Keep if exists", "Truncate if exists", "New file only", "Create if
1169 none", and "Existing file only". These are translated into a
1172 C<$uCreate> should be one of C<OPEN_ALWAYS>, C<OPEN_EXISTING>,
1173 C<TRUNCATE_EXISTING>, C<CREATE_ALWAYS>, or C<CREATE_NEW>.
1175 =item Share => $sShare
1177 =item Share => $uShare
1179 C<$sShare> should be a string with zero or more characters from
1180 C<"rwd"> that is translated into a C<$uShare> value. C<"rw"> is
1183 C<$uShare> should be an unsigned value having zero or more of the
1184 following bits set: C<FILE_SHARE_READ>, C<FILE_SHARE_WRITE>, and
1185 C<FILE_SHARE_DELETE>. C<FILE_SHARE_READ|FILE_SHARE_WRITE> is the
1192 $hFlop= createFile( "//./A:", "r", "r" )
1193 or die "Can't prevent others from writing to floppy: $^E\n";
1194 $hDisk= createFile( "//./C:", "rw ke", "" )
1195 or die "Can't get exclusive access to C: $^E\n";
1196 $hDisk= createFile( $sFilePath, "ke",
1197 { Access=>FILE_READ_ATTRIBUTES } )
1198 or die "Can't read attributes of $sFilePath: $^E\n";
1199 $hTemp= createFile( "$ENV{Temp}/temp.$$", "wn", "",
1200 { Attributes=>"hst", Flags=>FILE_FLAG_DELETE_ON_CLOSE() } )
1201 or die "Can't create temporary file, temp.$$: $^E\n";
1203 =item getLogicalDrives
1205 =item C<@roots= getLogicalDrives()>
1207 Returns the paths to the root directories of all logical drives
1208 currently defined. This includes all types of drive lettters, such
1209 as floppies, CD-ROMs, hard disks, and network shares. A typical
1210 return value on a poorly equipped computer would be C<("A:\\","C:\\")>.
1214 =item C<CloseHandle( $hObject )>
1216 Closes a Win32 native handle, such as one opened via C<CreateFile>.
1217 Like most routines, returns a true value if successful and a false
1218 value [and sets C<$^E> and C<regLastError()>] on failure.
1222 =item C<CopyFile( $sOldFileName, $sNewFileName, $bFailIfExists )>
1224 C<$sOldFileName> is the path to the file to be copied.
1225 C<$sNewFileName> is the path to where the file should be copied.
1226 Note that you can B<NOT> just specify a path to a directory in
1227 C<$sNewFileName> to copy the file to that directory using the
1230 If C<$bFailIfExists> is true and C<$sNewFileName> is the path to
1231 a file that already exists, then C<CopyFile> will fail. If
1232 C<$bFailIfExists> is falsea, then the copy of the C<$sOldFileNmae>
1233 file will overwrite the C<$sNewFileName> file if it already exists.
1235 Like most routines, returns a true value if successful and a false
1236 value [and sets C<$^E> and C<regLastError()>] on failure.
1240 =item C<$hObject= CreateFile( $sPath, $uAccess, $uShare, $pSecAttr, $uCreate, $uFlags, $hModel )>
1242 On failure, C<$hObject> gets set to a false value and C<$^E> and
1243 C<fileLastError()> are set to the reason for the failure. Otherwise,
1244 C<$hObject> gets set to a Win32 native file handle which is always a
1245 true value [returns C<"0 but true"> in the impossible(?) case of the
1246 handle having a value of C<0>].
1248 C<$sPath> is the path to the file [or device, etc.] to be opened.
1250 C<$sPath> can use C<"/"> or C<"\\"> as path delimiters and can even
1251 mix the two. We will usually only use C<"/"> in our examples since
1252 using C<"\\"> is usually harder to read.
1254 Under Windows NT, C<$sPath> can start with C<"//?/"> to allow the use
1255 of paths longer than C<MAX_PATH> [for UNC paths, replace the leading
1256 C<"//"> with C<"//?/UNC/">, as in C<"//?/UNC/Server/Share/Dir/File.Ext">].
1258 C<$sPath> can start with C<"//./"> to indicate that the rest of the
1259 path is the name of a "DOS device." You can use C<QueryDosDevice>
1260 to list all current DOS devices and can add or delete them with
1261 C<DefineDosDevice>. If you get the source-code distribution of this
1262 module from CPAN, then it includes an example script, F<ex/ListDevs.plx>
1263 that will list all current DOS devices and their "native" definition.
1264 Again, note that this doesn't work under Win95 nor Win98.
1266 The most common such DOS devices include:
1270 =item C<"//./PhysicalDrive0">
1272 Your entire first hard disk. Doesn't work under Windows 95. This
1273 allows you to read or write raw sectors of your hard disk and to use
1274 C<DeviceIoControl> to perform miscellaneous queries and operations
1275 to the hard disk. Writing raw sectors and certain other operations
1276 can seriously damage your files or the function of your computer.
1278 Locking this for exclusive access [by specifying C<0> for C<$uShare>]
1279 doesn't prevent access to the partitions on the disk nor their file
1280 systems. So other processes can still access any raw sectors within
1281 a partition and can use the file system on the disk as usual.
1285 Your F<C:> partition. Doesn't work under Windows 95. This allows
1286 you to read or write raw sectors of that partition and to use
1287 C<DeviceIoControl> to perform miscellaneous queries and operations
1288 to the partition. Writing raw sectors and certain other operations
1289 can seriously damage your files or the function of your computer.
1291 Locking this for exclusive access doesn't prevent access to the
1292 physical drive that the partition is on so other processes can
1293 still access the raw sectors that way. Locking this for exclusive
1294 access B<does> prevent other processes from opening the same raw
1295 partition and B<does> prevent access to the file system on it. It
1296 even prevents the current process from accessing the file system
1301 The raw floppy disk. Doesn't work under Windows 95. This allows
1302 you to read or write raw sectors of the floppy disk and to use
1303 C<DeviceIoControl> to perform miscellaneous queries and operations
1304 to the floopy disk or drive.
1306 Locking this for exclusive access prevents all access to the floppy.
1308 =item C<"//./PIPE/PipeName">
1310 A named pipe, created via C<CreateNamedPipe>.
1314 C<$uAccess> is an unsigned value with bits set indicating the
1315 type of access desired. Usually either C<0> ["query" access],
1316 C<GENERIC_READ>, C<GENERIC_WRITE>, C<GENERIC_READ|GENERIC_WRITE>,
1317 or C<GENERIC_ALL>. More specific types of access can be specified,
1318 such as C<FILE_APPEND_DATA> or C<FILE_READ_EA>.
1320 C<$uShare> controls how the file is shared, that is, whether other
1321 processes can have read, write, and/or delete access to the file while
1322 we have it opened. C<$uShare> is an unsigned value with zero or more
1323 of these bits set: C<FILE_SHARE_READ>, C<FILE_SHARE_WRITE>, and
1324 C<FILE_SHARE_DELETE>.
1326 If another process currently has read, write, and/or delete access to
1327 the file and you don't allow that level of sharing, then your call to
1328 C<CreateFile> will fail. If you requested read, write, and/or delete
1329 access and another process already has the file open but doesn't allow
1330 that level of sharing, thenn your call to C<createFile> will fail. Once
1331 you have the file open, if another process tries to open it with read,
1332 write, and/or delete access and you don't allow that level of sharing,
1333 then that process won't be allowed to open the file.
1335 C<$pSecAttr> should either be C<[]> [for C<NULL>] or a
1336 C<SECURITY_ATTRIBUTES> data structure packed into a string.
1337 For example, if C<$pSecDesc> contains a C<SECURITY_DESCRIPTOR>
1338 structure packed into a string, perhaps via:
1340 RegGetKeySecurity( $key, 4, $pSecDesc, 1024 );
1342 then you can set C<$pSecAttr> via:
1344 $pSecAttr= pack( "L P i", 12, $pSecDesc, $bInheritHandle );
1346 C<$uCreate> is one of the following values: C<OPEN_ALWAYS>,
1347 C<OPEN_EXISTING>, C<TRUNCATE_EXISTING>, C<CREATE_ALWAYS>, and
1350 C<$uFlags> is an unsigned value with zero or more bits set indicating
1351 attributes to associate with the file [C<FILE_ATTRIBUTE_*> values] or
1352 special options [C<FILE_FLAG_*> values].
1354 If opening the client side of a named pipe, then you can also set
1355 C<$uFlags> to include C<SECURITY_SQOS_PRESENT> along with one of the
1356 other C<SECURITY_*> constants to specify the security quality of
1359 C<$hModel> is C<0> [or C<[]>, both of which mean C<NULL>] or a Win32
1360 native handle opened with C<GENERIC_READ> access to a model file from
1361 which file attributes and extended attributes are to be copied if a
1362 new file gets created.
1366 $hFlop= CreateFile( "//./A:", GENERIC_READ(),
1367 FILE_SHARE_READ(), [], OPEN_EXISTING(), 0, [] )
1368 or die "Can't prevent others from writing to floppy: $^E\n";
1369 $hDisk= CreateFile( $sFilePath, FILE_READ_ATTRIBUTES(),
1370 FILE_SHARE_READ()|FILE_SHARE_WRITE(), [], OPEN_EXISTING(), 0, [] )
1371 or die "Can't read attributes of $sFilePath: $^E\n";
1372 $hTemp= CreateFile( "$ENV{Temp}/temp.$$", GENERIC_WRITE(), 0,
1373 CREATE_NEW(), FILE_FLAG_DELETE_ON_CLOSE()|attrLetsToBits("hst"), [] )
1374 or die "Can't create temporary file, temp.$$: $^E\n";
1376 =item DefineDosDevice
1378 =item C<DefineDosDevice( $uFlags, $sDosDeviceName, $sTargetPath )>
1380 Defines a new DOS device, overrides the current definition of a DOS
1381 device, or deletes a definition of a DOS device. Like most routines,
1382 returns a true value if successful and a false value [and sets C<$^E>
1383 and C<regLastError()>] on failure.
1385 C<$sDosDeviceName> is the name of a DOS device for which we'd like
1386 to add or delete a definition.
1388 C<$uFlags> is an unsigned value with zero or more of the following
1393 =item C<DDD_RAW_TARGET_PATH>
1395 Indicates that C<$sTargetPath> will be a raw Windows NT object name.
1396 This usually means that C<$sTargetPath> starts with C<"\\Device\\">.
1397 Note that you cannot use C<"/"> in place of C<"\\"> in raw target path
1400 =item C<DDD_REMOVE_DEFINITION>
1402 Requests that a definition be deleted. If C<$sTargetPath> is
1403 C<[]> [for C<NULL>], then the most recently added definition for
1404 C<$sDosDeviceName> is removed. Otherwise the most recently added
1405 definition matching C<$sTargetPath> is removed.
1407 If the last definition is removed, then the DOS device name is
1410 =item C<DDD_EXACT_MATCH_ON_REMOVE>
1412 When deleting a definition, this bit causes each C<$sTargetPath> to
1413 be compared to the full-length definition when searching for the most
1414 recently added match. If this bit is not set, then C<$sTargetPath>
1415 only needs to match a prefix of the definition.
1419 C<$sTargetPath> is the DOS device's specific definition that you
1420 wish to add or delete. For C<DDD_RAW_TARGET_PATH>, these usually
1421 start with C<"\\Device\\">. If the C<DDD_RAW_TARGET_PATH> bit is
1422 not set, then C<$sTargetPath> is just an ordinary path to some file
1423 or directory, providing the functionality of the B<subst> command.
1427 =item C<DeleteFile( $sFileName )>
1429 Deletes the named file. Compared to Perl's C<unlink>, C<DeleteFile>
1430 has the advantage of not deleting read-only files. For B<some>
1431 versions of Perl, C<unlink> silently calls C<chmod> whether it needs
1432 to or not before deleting the file so that files that you have
1433 protected by marking them as read-only are not always protected from
1436 Like most routines, returns a true value if successful and a false
1437 value [and sets C<$^E> and C<regLastError()>] on failure.
1439 =item DeviceIoControl
1441 =item C<DeviceIoControl( $hDevice, $uIoControlCode, $pInBuf, $lInBuf, $opOutBuf, $lOutBuf, $olRetBytes, $pOverlapped )>
1443 Requests a special operation on an I/O [input/output] device, such
1444 as ejecting a tape or formatting a disk. Like most routines, returns
1445 a true value if successful and a false value [and sets C<$^E> and
1446 C<regLastError()>] on failure.
1448 C<$hDevice> is a Win32 native file handle to a device [return value
1449 from C<CreateFile>].
1451 C<$uIoControlCode> is an unsigned value [a C<IOCTL_*> or C<FSCTL_*>
1452 constant] indicating the type query or other operation to be performed.
1454 C<$pInBuf> is C<[]> [for C<NULL>] or a data structure packed into a
1455 string. The type of data structure depends on the C<$uIoControlCode>
1456 value. C<$lInBuf> is C<0> or the length of the structure in
1457 C<$pInBuf>. If C<$pInBuf> is not C<[]> and C<$lInBuf> is C<0>, then
1458 C<$lInBuf> will automatically be set to C<length($pInBuf)> for you.
1460 C<$opOutBuf> is C<[]> [for C<NULL>] or will be set to contain a
1461 returned data structure packed into a string. C<$lOutBuf> indicates
1462 how much space to allocate in C<$opOutBuf> for C<DeviceIoControl> to
1463 store the data structure. If C<$lOutBuf> is a number and C<$opOutBuf>
1464 already has a buffer allocated for it that is larger than C<$lOutBuf>
1465 bytes, then this larger buffer size will be passed to C<DeviceIoControl>.
1466 However, you can force a specific buffer size to be passed to
1467 C<DeviceIoControl> by prepending a C<"="> to the front of C<$lOutBuf>.
1469 C<$olRetBytes> is C<[]> or is a scalar to receive the number of bytes
1470 written to C<$opOutBuf>. Even when C<$olRetBytes> is C<[]>, a valid
1471 pointer to a C<DWORD> [and not C<NULL>] is passed to C<DeviceIoControl>.
1472 In this case, C<[]> just means that you don't care about the value
1473 that might be written to C<$olRetBytes>, which is usually the case
1474 since you can usually use C<length($opOutBuf)> instead.
1476 C<$pOverlapped> is C<[]> or is a C<OVERLAPPED> structure packed into
1477 a string. This is only useful if C<$hDevice> was opened with the
1478 C<FILE_FLAG_OVERLAPPED> flag set.
1480 =item FdGetOsFHandle
1482 =item C<$hNativeHandle= FdGetOsFHandle( $ivFd )>
1484 C<FdGetOsFHandle> simply calls C<_get_osfhandle()>. It was renamed
1485 to better fit in with the rest the function names of this module,
1486 in particular to distinguish it from C<GetOsFHandle>. It takes an
1487 integer file descriptor [as from Perl's C<fileno>] and returns the
1488 Win32 native file handle associated with that file descriptor or
1489 C<INVALID_HANDLE_VALUE> if C<$ivFd> is not an open file descriptor.
1491 When you call Perl's C<open> to set a Perl file handle [like C<STDOUT>],
1492 Perl calls C's C<fopen> to set a stdio C<FILE *>. C's C<fopen> calls
1493 something like Unix's C<open>, that is, Win32's C<_sopen>, to get an
1494 integer file descriptor [where 0 is for C<STDIN>, 1 for C<STDOUT>, etc.].
1495 Win32's C<_sopen> calls C<CreateFile> to set a C<HANDLE>, a Win32 native
1496 file handle. So every Perl file handle [like C<STDOUT>] has an integer
1497 file descriptor associated with it that you can get via C<fileno>. And,
1498 under Win32, every file descriptor has a Win32 native file handle
1499 associated with it. C<FdGetOsFHandle> lets you get access to that.
1501 C<$hNativeHandle> is set to C<INVALID_HANDLE_VALUE> [and
1502 C<lastFileError()> and C<$^E> are set] if C<FdGetOsFHandle> fails.
1503 See also C<GetOsFHandle> which provides a friendlier interface.
1507 =item C<$value= fileConstant( $sConstantName )>
1509 Fetch the value of a constant. Returns C<undef> if C<$sConstantName>
1510 is not the name of a constant supported by this module. Never sets
1513 This function is rarely used since you will usually get the value of a
1514 constant by having that constant imported into your package by listing
1515 the constant name in the C<use Win32API::File> statement and then
1516 simply using the constant name in your code [perhaps followed by
1517 C<()>]. This function is useful for verifying constant names not in
1518 Perl code, for example, after prompting a user to type in a constant
1523 =item C<$svError= fileLastError();>
1525 =item C<fileLastError( $uError );>
1527 Returns the last error encountered by a routine from this module.
1528 It is just like C<$^E> except it isn't changed by anything except
1529 routines from this module. Ideally you could just use C<$^E>, but
1530 current versions of Perl often overwrite C<$^E> before you get a
1531 chance to check it and really old versions of Perl don't really
1532 support C<$^E> under Win32.
1534 Just like C<$^E>, in a numeric context C<fileLastError()> returns
1535 the numeric error value while in a string context it returns a
1536 text description of the error [actually it returns a Perl scalar
1537 that contains both values so C<$x= fileLastError()> causes C<$x>
1538 to give different values in string vs. numeric contexts].
1540 The last form sets the error returned by future calls to
1541 C<fileLastError()> and should not be used often. C<$uError> must
1542 be a numeric error code. Also returns the dual-valued version
1547 =item C<$uDriveType= GetDriveType( $sRootPath )>
1549 Takes a string giving the path to the root directory of a file system
1550 [called a "drive" because every file system is assigned a "drive letter"]
1551 and returns an unsigned value indicating the type of drive the file
1552 system is on. The return value should be one of:
1556 =item C<DRIVE_UNKNOWN>
1558 None of the following.
1560 =item C<DRIVE_NO_ROOT_DIR>
1562 A "drive" that does not have a file system. This can be a drive letter
1563 that hasn't been defined or a drive letter assigned to a partition
1564 that hasn't been formatted yet.
1566 =item C<DRIVE_REMOVABLE>
1568 A floppy diskette drive or other removable media drive, but not a CD-ROM
1571 =item C<DRIVE_FIXED>
1573 An ordinary hard disk partition.
1575 =item C<DRIVE_REMOTE>
1579 =item C<DRIVE_CDROM>
1583 =item C<DRIVE_RAMDISK>
1585 A "ram disk" or memory-resident virtual file system used for high-speed
1586 access to small amounts of temporary file space.
1590 =item GetFileAttributes
1592 =item C<$uAttrs = GetFileAttributes( $sPath )>
1594 Takes a path string and returns an unsigned value with attribute flags.
1595 If it fails, it returns INVALID_FILE_ATTRIBUTES, otherwise it can be
1596 one or more of the following values:
1600 =item C<FILE_ATTRIBUTE_ARCHIVE>
1602 The file or directory is an archive file or directory. Applications use
1603 this attribute to mark files for backup or removal.
1605 =item C<FILE_ATTRIBUTE_COMPRESSED>
1607 The file or directory is compressed. For a file, this means that all of
1608 the data in the file is compressed. For a directory, this means that
1609 compression is the default for newly created files and subdirectories.
1611 =item C<FILE_ATTRIBUTE_DEVICE>
1613 Reserved; do not use.
1615 =item C<FILE_ATTRIBUTE_DIRECTORY>
1617 The handle identifies a directory.
1619 =item C<FILE_ATTRIBUTE_ENCRYPTED>
1621 The file or directory is encrypted. For a file, this means that all data
1622 streams in the file are encrypted. For a directory, this means that
1623 encryption is the default for newly created files and subdirectories.
1625 =item C<FILE_ATTRIBUTE_HIDDEN>
1627 The file or directory is hidden. It is not included in an ordinary directory
1630 =item C<FILE_ATTRIBUTE_NORMAL>
1632 The file or directory has no other attributes set. This attribute is valid
1635 =item C<FILE_ATTRIBUTE_NOT_CONTENT_INDEXED>
1637 The file will not be indexed by the content indexing service.
1639 =item C<FILE_ATTRIBUTE_OFFLINE>
1641 The data of the file is not immediately available. This attribute indicates
1642 that the file data has been physically moved to offline storage. This
1643 attribute is used by Remote Storage, the hierarchical storage management
1644 software. Applications should not arbitrarily change this attribute.
1646 =item C<FILE_ATTRIBUTE_READONLY>
1648 The file or directory is read-only. Applications can read the file but cannot
1649 write to it or delete it. In the case of a directory, applications cannot
1652 =item C<FILE_ATTRIBUTE_REPARSE_POINT>
1654 The file or directory has an associated reparse point.
1656 =item C<FILE_ATTRIBUTE_SPARSE_FILE>
1658 The file is a sparse file.
1660 =item C<FILE_ATTRIBUTE_SYSTEM>
1662 The file or directory is part of, or is used exclusively by, the operating
1665 =item C<FILE_ATTRIBUTE_TEMPORARY>
1667 The file is being used for temporary storage. File systems avoid writing
1668 data back to mass storage if sufficient cache memory is available, because
1669 often the application deletes the temporary file shortly after the handle is
1670 closed. In that case, the system can entirely avoid writing the data.
1671 Otherwise, the data will be written after the handle is closed.
1677 =item C<$uFileType= GetFileType( $hFile )>
1679 Takes a Win32 native file handle and returns a C<FILE_TYPE_*> constant
1680 indicating the type of the file opened on that handle:
1684 =item C<FILE_TYPE_UNKNOWN>
1686 None of the below. Often a special device.
1688 =item C<FILE_TYPE_DISK>
1690 An ordinary disk file.
1692 =item C<FILE_TYPE_CHAR>
1694 What Unix would call a "character special file", that is, a device that
1695 works on character streams such as a printer port or a console.
1697 =item C<FILE_TYPE_PIPE>
1699 Either a named or anonymous pipe.
1705 =item C<$size= getFileSize( $hFile )>
1707 This is a Perl-friendly wrapper for the C<GetFileSize> (below) API call.
1709 It takes a Win32 native file handle and returns the size in bytes. Since the
1710 size can be a 64 bit value, on non 64 bit integer Perls the value returned will
1711 be an object of type C<Math::BigInt>.
1715 =item C<$iSizeLow= GetFileSize($win32Handle, $iSizeHigh)>
1717 Returns the size of a file pointed to by C<$win32Handle>, optionally storing
1718 the high order 32 bits into C<$iSizeHigh> if it is not C<[]>. If $iSizeHigh is
1719 C<[]>, a non-zero value indicates success. Otherwise, on failure the return
1720 value will be C<0xffffffff> and C<fileLastError()> will not be C<NO_ERROR>.
1722 =item GetOverlappedResult
1724 =item C<$bRetval= GetOverlappedResult( $win32Handle, $pOverlapped,
1725 $numBytesTransferred, $bWait )>
1727 Used for asynchronous IO in Win32 to get the result of a pending IO operation,
1728 such as when a file operation returns C<ERROR_IO_PENDING>. Returns a false
1729 value on failure. The C<$overlapped> structure and C<$numBytesTransferred>
1730 will be modified with the results of the operation.
1732 As far as creating the C<$pOverlapped> structure, you are currently on your own.
1734 See L<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getoverlappedresult.asp> for more information.
1736 =item GetLogicalDrives
1738 =item C<$uDriveBits= GetLogicalDrives()>
1740 Returns an unsigned value with one bit set for each drive letter currently
1741 defined. If "A:" is currently a valid drive letter, then the C<1> bit
1742 will be set in C<$uDriveBits>. If "B:" is valid, then the C<2> bit will
1743 be set. If "Z:" is valid, then the C<2**26> [C<0x4000000>] bit will be
1746 =item GetLogicalDriveStrings
1748 =item C<$olOutLength= GetLogicalDriveStrings( $lBufSize, $osBuffer )>
1750 For each currently defined drive letter, a C<'\0'>-terminated string
1751 of the path to the root of its file system is constructed. All of
1752 these strings are concatenated into a single larger string and an
1753 extra terminating C<'\0'> is added. This larger string is returned
1754 in C<$osBuffer>. Note that this includes drive letters that have
1755 been defined but that have no file system, such as drive letters
1756 assigned to unformatted partitions.
1758 C<$lBufSize> is the size of the buffer to allocate to store this
1759 list of strings. C<26*4+1> is always sufficient and should usually
1762 C<$osBuffer> is a scalar to be set to contain the constructed string.
1764 C<$olOutLength> is the number of bytes actually written to C<$osBuffer>
1765 but C<length($osBuffer)> can also be used to determine this.
1767 For example, on a poorly equipped computer,
1769 GetLogicalDriveStrings( 4*26+1, $osBuffer );
1771 might set C<$osBuffer> to the 9-character string, C<"A:\\\0C:\\\0\0">.
1773 =item GetHandleInformation
1775 =item C<GetHandleInformation( $hObject, $ouFlags )>
1777 Retrieves the flags associated with a Win32 native file handle or object
1780 C<$hObject> is an open Win32 native file handle or an open Win32 native
1781 handle to some other type of object.
1783 C<$ouFlags> will be set to an unsigned value having zero or more of
1784 the bits C<HANDLE_FLAG_INHERIT> and C<HANDLE_FLAG_PROTECT_FROM_CLOSE>
1785 set. See the C<":HANDLE_FLAG_"> export class for the meanings of these
1790 =item C<$hNativeHandle= GetOsFHandle( FILE )>
1792 Takes a Perl file handle [like C<STDIN>] and returns the Win32 native
1793 file handle associated with it. See C<FdGetOsFHandle> for more
1794 information about Win32 native file handles.
1796 C<$hNativeHandle> is set to a false value [and C<lastFileError()> and
1797 C<$^E> are set] if C<GetOsFHandle> fails. C<GetOsFHandle> returns
1798 C<"0 but true"> in the impossible(?) case of the handle having a value
1801 =item GetVolumeInformation
1803 =item C<GetVolumeInformation( $sRootPath, $osVolName, $lVolName, $ouSerialNum, $ouMaxNameLen, $ouFsFlags, $osFsType, $lFsType )>
1805 Gets information about a file system volume, returning a true
1806 value if successful. On failure, returns a false value and sets
1807 C<fileLastError()> and C<$^E>.
1809 C<$sRootPath> is a string specifying the path to the root of the file system,
1810 for example, C<"C:/">.
1812 C<$osVolName> is a scalar to be set to the string representing the
1813 volume name, also called the file system label. C<$lVolName> is the
1814 number of bytes to allocate for the C<$osVolName> buffer [see
1815 L<Buffer Sizes> for more information].
1817 C<$ouSerialNum> is C<[]> [for C<NULL>] or will be set to the numeric
1818 value of the volume's serial number.
1820 C<$ouMaxNameLen> is C<[]> [for C<NULL>] or will be set to the maximum
1821 length allowed for a file name or directory name within the file system.
1823 C<$osFsType> is a scalar to be set to the string representing the
1824 file system type, such as C<"FAT"> or C<"NTFS">. C<$lFsType> is the
1825 number of bytes to allocate for the C<$osFsType> buffer [see
1826 L<Buffer Sizes> for more information].
1828 C<$ouFsFlags> is C<[]> [for C<NULL>] or will be set to an unsigned integer
1829 with bits set indicating properties of the file system:
1833 =item C<FS_CASE_IS_PRESERVED>
1835 The file system preserves the case of file names [usually true].
1836 That is, it doesn't change the case of file names such as forcing
1837 them to upper- or lower-case.
1839 =item C<FS_CASE_SENSITIVE>
1841 The file system supports the ability to not ignore the case of file
1842 names [but might ignore case the way you are using it]. That is, the
1843 file system has the ability to force you to get the letter case of a
1844 file's name exactly right to be able to open it. This is true for
1845 "NTFS" file systems, even though case in file names is usually still
1848 =item C<FS_UNICODE_STORED_ON_DISK>
1850 The file system preserves Unicode in file names [true for "NTFS"].
1852 =item C<FS_PERSISTENT_ACLS>
1854 The file system supports setting Access Control Lists on files [true
1857 =item C<FS_FILE_COMPRESSION>
1859 The file system supports compression on a per-file basis [true for
1862 =item C<FS_VOL_IS_COMPRESSED>
1864 The entire file system is compressed such as via "DoubleSpace".
1868 =item IsRecognizedPartition
1870 =item C<IsRecognizedPartition( $ivPartitionType )>
1872 Takes a partition type and returns whether that partition type is
1873 supported under Win32. C<$ivPartitonType> is an integer value as from
1874 the operating system byte of a hard disk's DOS-compatible partition
1875 table [that is, a partition table for x86-based Win32, not, for
1876 example, one used with Windows NT for Alpha processors]. For example,
1877 the C<PartitionType> member of the C<PARTITION_INFORMATION> structure.
1879 Common values for C<$ivPartitionType> include C<PARTITION_FAT_12==1>,
1880 C<PARTITION_FAT_16==4>, C<PARTITION_EXTENDED==5>, C<PARTITION_FAT32==0xB>.
1882 =item IsContainerPartition
1884 =item C<IsContainerPartition( $ivPartitionType )>
1886 Takes a partition type and returns whether that partition is a
1887 "container" partition that is supported under Win32, that is, whether
1888 it is an "extended" partition that can contain "logical" partitions.
1889 C<$ivPartitonType> is as for C<IsRecognizedPartition>.
1893 =item C<MoveFile( $sOldName, $sNewName )>
1895 Renames a file or directory. C<$sOldName> is the name of the existing
1896 file or directory that is to be renamed. C<$sNewName> is the new name
1897 to give the file or directory. Returns a true value if the move
1898 succeeds. For failure, returns a false value and sets
1899 C<fileLastErorr()> and C<$^E> to the reason for the failure.
1901 Files can be "renamed" between file systems and the file contents and
1902 some attributes will be moved. Directories can only be renamed within
1903 one file system. If there is already a file or directory named
1904 C<$sNewName>, then C<MoveFile> will fail.
1908 =item C<MoveFileEx( $sOldName, $sNewName, $uFlags )>
1910 Renames a file or directory. C<$sOldName> is the name of the existing
1911 file or directory that is to be renamed. C<$sNewName> is the new name
1912 to give the file or directory. Returns a true value if the move
1913 succeeds. For failure, returns a false value and sets
1914 C<fileLastErorr()> and C<$^E> to the reason for the failure.
1916 C<$uFlags> is an unsigned value with zero or more of the following bits set:
1920 =item C<MOVEFILE_REPLACE_EXISTING>
1922 If this bit is set and a file [but not a directory] named C<$sNewName>
1923 already exists, then it will be replaced by C<$sOldName>. If this bit
1924 is not set then C<MoveFileEx> will fail rather than replace an existing
1927 =item C<MOVEFILE_COPY_ALLOWED>
1929 Allows files [but not directories] to be moved between file systems
1930 by copying the C<$sOldName> file data and some attributes to
1931 C<$sNewName> and then deleting C<$sOldName>. If this bit is not set
1932 [or if C<$sOldName> denotes a directory] and C<$sNewName> refers to a
1933 different file system than C<$sOldName>, then C<MoveFileEx> will fail.
1935 =item C<MOVEFILE_DELAY_UNTIL_REBOOT>
1937 Preliminary verifications are made and then an entry is added to the
1938 Registry to cause the rename [or delete] operation to be done the
1939 next time this copy of the operating system is booted [right after
1940 any automatic file system checks have completed]. This is not
1941 supported under Windows 95.
1943 When this bit is set, C<$sNewName> can be C<[]> [for C<NULL>] to
1944 indicate that C<$sOldName> should be deleted during the next boot
1945 rather than renamed.
1947 Setting both the C<MOVEFILE_COPY_ALLOWED> and
1948 C<MOVEFILE_DELAY_UNTIL_REBOOT> bits will cause C<MoveFileEx> to fail.
1950 =item C<MOVEFILE_WRITE_THROUGH>
1952 Ensures that C<MoveFileEx> won't return until the operation has
1953 finished and been flushed to disk. This is not supported under
1954 Windows 95. Only affects file renames to another file system,
1955 forcing a buffer flush at the end of the copy operation.
1961 =item C<OsFHandleOpen( FILE, $hNativeHandle, $sMode )>
1963 Opens a Perl file handle based on an already open Win32 native
1964 file handle [much like C's C<fdopen()> does with a file descriptor].
1965 Returns a true value if the open operation succeeded. For failure,
1966 returns a false value and sets C<$!> [and possibly C<fileLastError()>
1967 and C<$^E>] to the reason for the failure.
1969 C<FILE> is a Perl file handle [in any of the supported forms, a
1970 bareword, a string, a typeglob, or a reference to a typeglob] that
1971 will be opened. If C<FILE> is already open, it will automatically
1972 be closed before it is reopened.
1974 C<$hNativeHandle> is an open Win32 native file handle, probably the
1975 return value from C<CreateFile> or C<createFile>.
1977 C<$sMode> is string of zero or more letters from C<"rwatb">. These
1978 are translated into a combination C<O_RDONLY> [C<"r">], C<O_WRONLY>
1979 [C<"w">], C<O_RDWR> [C<"rw">], C<O_APPEND> [C<"a">], C<O_TEXT>
1980 [C<"t">], and C<O_BINARY> [C<"b">] flags [see the L<Fcntl> module]
1981 that is passed to C<OsFHandleOpenFd>. Currently only C<O_APPEND>
1982 and C<O_TEXT> have any significance.
1984 Also, a C<"r"> and/or C<"w"> in C<$sMode> is used to decide how the
1985 file descriptor is converted into a Perl file handle, even though this
1986 doesn't appear to make a difference. One of the following is used:
1988 open( FILE, "<&=".$ivFd ) # "r" w/o "w"
1989 open( FILE, ">&=".$ivFd ) # "w" w/o "r"
1990 open( FILE, "+<&=".$ivFd ) # both "r" and "w"
1992 C<OsFHandleOpen> eventually calls the Win32-specific C routine
1993 C<_open_osfhandle()> or Perl's "improved" version called
1994 C<win32_open_osfhandle()>. Prior to Perl5.005, C's
1995 C<_open_osfhandle()> is called which will fail if
1996 C<GetFileType($hNativeHandle)> would return C<FILE_TYPE_UNKNOWN>. For
1997 Perl5.005 and later, C<OsFHandleOpen> calls C<win32_open_osfhandle()>
1998 from the Perl DLL which doesn't have this restriction.
2000 =item OsFHandleOpenFd
2002 =item C<$ivFD= OsFHandleOpenFd( $hNativeHandle, $uMode )>
2004 Opens a file descriptor [C<$ivFD>] based on an already open Win32
2005 native file handle, C<$hNativeHandle>. This just calls the
2006 Win32-specific C routine C<_open_osfhandle()> or Perl's "improved"
2007 version called C<win32_open_osfhandle()>. Prior to Perl5.005 and in Cygwin
2008 Perl, C's C<_open_osfhandle()> is called which will fail if
2009 C<GetFileType($hNativeHandle)> would return C<FILE_TYPE_UNKNOWN>. For
2010 Perl5.005 and later, C<OsFHandleOpenFd> calls C<win32_open_osfhandle()> from
2011 the Perl DLL which doesn't have this restriction.
2013 C<$uMode> the logical combination of zero or more C<O_*> constants
2014 exported by the C<Fcntl> module. Currently only C<O_APPEND> and
2015 C<O_TEXT> have any significance.
2017 C<$ivFD> will be non-negative if the open operation was successful.
2018 For failure, C<-1> is returned and C<$!> [and possibly
2019 C<fileLastError()> and C<$^E>] is set to the reason for the failure.
2021 =item QueryDosDevice
2023 =item C<$olTargetLen= QueryDosDevice( $sDosDeviceName, $osTargetPath, $lTargetBuf )>
2025 Looks up the definition of a given "DOS" device name, yielding the
2026 active Windows NT native device name along with any currently dormant
2029 C<$sDosDeviceName> is the name of the "DOS" device whose definitions
2030 we want. For example, C<"C:">, C<"COM1">, or C<"PhysicalDrive0">.
2031 If C<$sDosDeviceName> is C<[]> [for C<NULL>], the list of all DOS
2032 device names is returned instead.
2034 C<$osTargetPath> will be assigned a string containing the list of
2035 definitions. The definitions are each C<'\0'>-terminate and are
2036 concatenated into the string, most recent first, with an extra C<'\0'>
2037 at the end of the whole string [see C<GetLogicalDriveStrings> for
2038 a sample of this format].
2040 C<$lTargetBuf> is the size [in bytes] of the buffer to allocate for
2041 C<$osTargetPath>. See L<Buffer Sizes> for more information.
2043 C<$olTargetLen> is set to the number of bytes written to
2044 C<$osTargetPath> but you can also use C<length($osTargetPath)>
2047 For failure, C<0> is returned and C<fileLastError()> and C<$^E> are
2048 set to the reason for the failure.
2052 =item C<ReadFile( $hFile, $opBuffer, $lBytes, $olBytesRead, $pOverlapped )>
2054 Reads bytes from a file or file-like device. Returns a true value if
2055 the read operation was successful. For failure, returns a false value
2056 and sets C<fileLastError()> and C<$^E> for the reason for the failure.
2058 C<$hFile> is a Win32 native file handle that is already open to the
2059 file or device to read from.
2061 C<$opBuffer> will be set to a string containing the bytes read.
2063 C<$lBytes> is the number of bytes you would like to read.
2064 C<$opBuffer> is automatically initialized to have a buffer large
2065 enough to hold that many bytes. Unlike other buffer sizes, C<$lBytes>
2066 does not need to have a C<"="> prepended to it to prevent a larger
2067 value to be passed to the underlying Win32 C<ReadFile> API. However,
2068 a leading C<"="> will be silently ignored, even if Perl warnings are
2071 If C<$olBytesRead> is not C<[]>, it will be set to the actual number
2072 of bytes read, though C<length($opBuffer)> can also be used to
2075 C<$pOverlapped> is C<[]> or is a C<OVERLAPPED> structure packed
2076 into a string. This is only useful if C<$hFile> was opened with
2077 the C<FILE_FLAG_OVERLAPPED> flag set.
2081 =item C<$uOldMode= SetErrorMode( $uNewMode )>
2083 Sets the mode controlling system error handling B<and> returns the
2084 previous mode value. Both C<$uOldMode> and C<$uNewMode> will have
2085 zero or more of the following bits set:
2089 =item C<SEM_FAILCRITICALERRORS>
2091 If set, indicates that when a critical error is encountered, the call
2092 that triggered the error fails immediately. Normally this bit is not
2093 set, which means that a critical error causes a dialogue box to appear
2094 notifying the desktop user that some application has triggered a
2095 critical error. The dialogue box allows the desktop user to decide
2096 whether the critical error is returned to the process, is ignored, or
2097 the offending operation is retried.
2099 This affects the C<CreateFile> and C<GetVolumeInformation> calls.
2101 Setting this bit is useful for allowing you to check whether a floppy
2102 diskette is in the floppy drive.
2104 =item C<SEM_NOALIGNMENTFAULTEXCEPT>
2106 If set, this causes memory access misalignment faults to be
2107 automatically fixed in a manner invisible to the process. This flag
2108 is ignored on x86-based versions of Windows NT. This flag is not
2109 supported on Windows 95.
2111 =item C<SEM_NOGPFAULTERRORBOX>
2113 If set, general protection faults do not generate a dialogue box but
2114 can instead be handled by the process via an exception handler. This
2115 bit should not be set by programs that don't know how to handle such
2118 =item C<SEM_NOOPENFILEERRORBOX>
2120 If set, then when an attempt to continue reading from or writing to
2121 an already open file [usually on a removable medium like a floppy
2122 diskette] finds the file no longer available, the call will
2123 immediately fail. Normally this bit is not set, which means that
2124 instead a dialogue box will appear notifying the desktop user that
2125 some application has run into this problem. The dialogue box allows
2126 the desktop user to decide whether the failure is returned to the
2127 process, is ignored, or the offending operation is retried.
2129 This affects the C<ReadFile> and C<WriteFile> calls.
2133 =item setFilePointer
2135 =item C<$uNewPos = setFilePointer( $hFile, $ivOffset, $uFromWhere )>
2137 This is a perl-friendly wrapper for the SetFilePointer API (below).
2138 C<$ivOffset> can be a 64 bit integer or C<Math::BigInt> object if your Perl
2139 doesn't have 64 bit integers. The return value is the new offset and will
2140 likewise be a 64 bit integer or a C<Math::BigInt> object.
2142 =item SetFilePointer
2144 =item C<$uNewPos = SetFilePointer( $hFile, $ivOffset, $ioivOffsetHigh, $uFromWhere )>
2146 The native Win32 version of C<seek()>. C<SetFilePointer> sets the
2147 position within a file where the next read or write operation will
2150 C<$hFile> is a Win32 native file handle.
2152 C<$uFromWhere> is either C<FILE_BEGIN>, C<FILE_CURRENT>, or
2153 C<FILE_END>, indicating that the new file position is being specified
2154 relative to the beginning of the file, the current file pointer, or
2155 the end of the file, respectively.
2157 C<$ivOffset> is [if C<$ioivOffsetHigh> is C<[]>] the offset [in bytes]
2158 to the new file position from the position specified via
2159 C<$uFromWhere>. If C<$ioivOffsetHigh> is not C<[]>, then C<$ivOffset>
2160 is converted to an unsigned value to be used as the low-order 4 bytes
2163 C<$ioivOffsetHigh> can be C<[]> [for C<NULL>] to indicate that you are
2164 only specifying a 4-byte offset and the resulting file position will
2165 be 0xFFFFFFFE or less [just under 4GB]. Otherwise C<$ioivOfffsetHigh>
2166 starts out with the high-order 4 bytes [signed] of the offset and gets
2167 set to the [unsigned] high-order 4 bytes of the resulting file position.
2169 The underlying C<SetFilePointer> returns C<0xFFFFFFFF> to indicate
2170 failure, but if C<$ioivOffsetHigh> is not C<[]>, you would also have
2171 to check C<$^E> to determine whether C<0xFFFFFFFF> indicates an error
2172 or not. C<Win32API::File::SetFilePointer> does this checking for you
2173 and returns a false value if and only if the underlying
2174 C<SetFilePointer> failed. For this reason, C<$uNewPos> is set to
2175 C<"0 but true"> if you set the file pointer to the beginning of the
2176 file [or any position with 0 for the low-order 4 bytes].
2178 So the return value will be true if the seek operation was successful.
2179 For failure, a false value is returned and C<fileLastError()> and
2180 C<$^E> are set to the reason for the failure.
2182 =item SetHandleInformation
2184 =item C<SetHandleInformation( $hObject, $uMask, $uFlags )>
2186 Sets the flags associated with a Win32 native file handle or object
2187 handle. Returns a true value if the operation was successful. For
2188 failure, returns a false value and sets C<fileLastError()> and C<$^E>
2189 for the reason for the failure.
2191 C<$hObject> is an open Win32 native file handle or an open Win32 native
2192 handle to some other type of object.
2194 C<$uMask> is an unsigned value having one or more of the bits
2195 C<HANDLE_FLAG_INHERIT> and C<HANDLE_FLAG_PROTECT_FROM_CLOSE> set.
2196 Only bits set in C<$uMask> will be modified by C<SetHandleInformation>.
2198 C<$uFlags> is an unsigned value having zero or more of the bits
2199 C<HANDLE_FLAG_INHERIT> and C<HANDLE_FLAG_PROTECT_FROM_CLOSE> set.
2200 For each bit set in C<$uMask>, the cooresponding bit in the handle's
2201 flags is set to the value of the corresponding bit in C<$uFlags>.
2203 If C<$uOldFlags> were the value of the handle's flags before the
2204 call to C<SetHandleInformation>, then the value of the handle's
2205 flags afterward would be:
2207 ( $uOldFlags & ~$uMask ) | ( $uFlags & $uMask )
2209 [at least as far as the C<HANDLE_FLAG_INHERIT> and
2210 C<HANDLE_FLAG_PROTECT_FROM_CLOSE> bits are concerned.]
2212 See the C<":HANDLE_FLAG_"> export class for the meanings of these bits.
2216 =item C<WriteFile( $hFile, $pBuffer, $lBytes, $ouBytesWritten, $pOverlapped )>
2218 Write bytes to a file or file-like device. Returns a true value if
2219 the operation was successful. For failure, returns a false value and
2220 sets C<fileLastError()> and C<$^E> for the reason for the failure.
2222 C<$hFile> is a Win32 native file handle that is already open to the
2223 file or device to be written to.
2225 C<$pBuffer> is a string containing the bytes to be written.
2227 C<$lBytes> is the number of bytes you would like to write. If
2228 C<$pBuffer> is not at least C<$lBytes> long, C<WriteFile> croaks. You
2229 can specify C<0> for C<$lBytes> to write C<length($pBuffer)> bytes.
2230 A leading C<"="> on C<$lBytes> will be silently ignored, even if Perl
2231 warnings are enabled.
2233 C<$ouBytesWritten> will be set to the actual number of bytes written
2234 unless you specify it as C<[]>.
2236 C<$pOverlapped> is C<[]> or is an C<OVERLAPPED> structure packed
2237 into a string. This is only useful if C<$hFile> was opened with
2238 the C<FILE_FLAG_OVERLAPPED> flag set.
2244 The ASCII-specific functions. Each of these is just the same as the
2245 version without the trailing "A".
2253 GetLogicalDriveStringsA
2254 GetVolumeInformationA
2261 The wide-character-specific (Unicode) functions. Each of these is
2262 just the same as the version without the trailing "W" except that
2263 strings are expected in Unicode and some lengths are measured as
2264 number of C<WCHAR>s instead of number of bytes, as indicated below.
2270 =item C<CopyFileW( $swOldFileName, $swNewFileName, $bFailIfExists )>
2272 C<$swOldFileName> and C<$swNewFileName> are Unicode strings.
2276 =item C<$hObject= CreateFileW( $swPath, $uAccess, $uShare, $pSecAttr, $uCreate, $uFlags, $hModel )>
2278 C<$swPath> is Unicode.
2280 =item DefineDosDeviceW
2282 =item C<DefineDosDeviceW( $uFlags, $swDosDeviceName, $swTargetPath )>
2284 C<$swDosDeviceName> and C<$swTargetPath> are Unicode.
2288 =item C<DeleteFileW( $swFileName )>
2290 C<$swFileName> is Unicode.
2294 =item C<$uDriveType= GetDriveTypeW( $swRootPath )>
2296 C<$swRootPath> is Unicode.
2298 =item GetFileAttributesW
2300 =item C<$uAttrs= GetFileAttributesW( $swPath )>
2302 C<$swPath> is Unicode.
2304 =item GetLogicalDriveStringsW
2306 =item C<$olwOutLength= GetLogicalDriveStringsW( $lwBufSize, $oswBuffer )>
2308 Unicode is stored in C<$oswBuffer>. C<$lwBufSize> and C<$olwOutLength>
2309 are measured as number of C<WCHAR>s.
2311 =item GetVolumeInformationW
2313 =item C<GetVolumeInformationW( $swRootPath, $oswVolName, $lwVolName, $ouSerialNum, $ouMaxNameLen, $ouFsFlags, $oswFsType, $lwFsType )>
2315 C<$swRootPath> is Unicode and Unicode is written to C<$oswVolName> and
2316 C<$oswFsType>. C<$lwVolName> and C<$lwFsType> are measures as number
2321 =item C<MoveFileW( $swOldName, $swNewName )>
2323 C<$swOldName> and C<$swNewName> are Unicode.
2327 =item C<MoveFileExW( $swOldName, $swNewName, $uFlags )>
2329 C<$swOldName> and C<$swNewName> are Unicode.
2331 =item QueryDosDeviceW
2333 =item C<$olwTargetLen= QueryDosDeviceW( $swDeviceName, $oswTargetPath, $lwTargetBuf )>
2335 C<$swDeviceName> is Unicode and Unicode is written to
2336 C<$oswTargetPath>. C<$lwTargetBuf> and C<$olwTargetLen> are measured
2337 as number of C<WCHAR>s.
2343 Miscellaneous constants. Used for the C<$uCreate> argument of
2344 C<CreateFile> or the C<$uFromWhere> argument of C<SetFilePointer>.
2345 Plus C<INVALID_HANDLE_VALUE>, which you usually won't need to check
2346 for since most routines translate it into a false value.
2348 CREATE_ALWAYS CREATE_NEW OPEN_ALWAYS
2349 OPEN_EXISTING TRUNCATE_EXISTING INVALID_HANDLE_VALUE
2350 FILE_BEGIN FILE_CURRENT FILE_END
2354 Constants for the C<$uFlags> argument of C<DefineDosDevice>.
2356 DDD_EXACT_MATCH_ON_REMOVE
2358 DDD_REMOVE_DEFINITION
2362 Constants returned by C<GetDriveType>.
2364 DRIVE_UNKNOWN DRIVE_NO_ROOT_DIR DRIVE_REMOVABLE
2365 DRIVE_FIXED DRIVE_REMOTE DRIVE_CDROM
2370 Specific types of access to files that can be requested via the
2371 C<$uAccess> argument to C<CreateFile>.
2373 FILE_READ_DATA FILE_LIST_DIRECTORY
2374 FILE_WRITE_DATA FILE_ADD_FILE
2375 FILE_APPEND_DATA FILE_ADD_SUBDIRECTORY
2376 FILE_CREATE_PIPE_INSTANCE FILE_READ_EA
2377 FILE_WRITE_EA FILE_EXECUTE
2378 FILE_TRAVERSE FILE_DELETE_CHILD
2379 FILE_READ_ATTRIBUTES FILE_WRITE_ATTRIBUTES
2380 FILE_ALL_ACCESS FILE_GENERIC_READ
2381 FILE_GENERIC_WRITE FILE_GENERIC_EXECUTE )],
2383 =item C<":FILE_ATTRIBUTE_">
2385 File attribute constants. Returned by C<attrLetsToBits> and used in
2386 the C<$uFlags> argument to C<CreateFile>.
2388 FILE_ATTRIBUTE_ARCHIVE FILE_ATTRIBUTE_COMPRESSED
2389 FILE_ATTRIBUTE_HIDDEN FILE_ATTRIBUTE_NORMAL
2390 FILE_ATTRIBUTE_OFFLINE FILE_ATTRIBUTE_READONLY
2391 FILE_ATTRIBUTE_SYSTEM FILE_ATTRIBUTE_TEMPORARY
2393 In addition, C<GetFileAttributes> can return these constants (or
2394 INVALID_FILE_ATTRIBUTES in case of an error).
2396 FILE_ATTRIBUTE_DEVICE FILE_ATTRIBUTE_DIRECTORY
2397 FILE_ATTRIBUTE_ENCRYPTED FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
2398 FILE_ATTRIBUTE_REPARSE_POINT FILE_ATTRIBUTE_SPARSE_FILE
2400 =item C<":FILE_FLAG_">
2402 File option flag constants. Used in the C<$uFlags> argument to
2405 FILE_FLAG_BACKUP_SEMANTICS FILE_FLAG_DELETE_ON_CLOSE
2406 FILE_FLAG_NO_BUFFERING FILE_FLAG_OVERLAPPED
2407 FILE_FLAG_POSIX_SEMANTICS FILE_FLAG_RANDOM_ACCESS
2408 FILE_FLAG_SEQUENTIAL_SCAN FILE_FLAG_WRITE_THROUGH
2409 FILE_FLAG_OPEN_REPARSE_POINT
2411 =item C<":FILE_SHARE_">
2413 File sharing constants. Used in the C<$uShare> argument to
2416 FILE_SHARE_DELETE FILE_SHARE_READ FILE_SHARE_WRITE
2418 =item C<":FILE_TYPE_">
2420 File type constants. Returned by C<GetFileType>.
2422 FILE_TYPE_CHAR FILE_TYPE_DISK
2423 FILE_TYPE_PIPE FILE_TYPE_UNKNOWN
2427 File system characteristics constants. Placed in the C<$ouFsFlags>
2428 argument to C<GetVolumeInformation>.
2430 FS_CASE_IS_PRESERVED FS_CASE_SENSITIVE
2431 FS_UNICODE_STORED_ON_DISK FS_PERSISTENT_ACLS
2432 FS_FILE_COMPRESSION FS_VOL_IS_COMPRESSED
2434 =item C<":HANDLE_FLAG_">
2436 Flag bits modifying the behavior of an object handle and accessed via
2437 C<GetHandleInformation> and C<SetHandleInformation>.
2441 =item HANDLE_FLAG_INHERIT
2443 If this bit is set, then children of this process who inherit handles
2444 [that is, processes created by calls to the Win32 C<CreateProcess> API
2445 with the C<bInheritHandles> parameter specified as C<TRUE>], will inherit
2446 this particular object handle.
2448 =item HANDLE_FLAG_PROTECT_FROM_CLOSE
2450 If this bit is set, then calls to C<CloseHandle> against this handle
2451 will be ignored, leaving the handle open and usable.
2455 =item C<":IOCTL_STORAGE_">
2457 I/O control operations for generic storage devices. Used in the
2458 C<$uIoControlCode> argument to C<DeviceIoControl>. Includes
2459 C<IOCTL_STORAGE_CHECK_VERIFY>, C<IOCTL_STORAGE_MEDIA_REMOVAL>,
2460 C<IOCTL_STORAGE_EJECT_MEDIA>, C<IOCTL_STORAGE_LOAD_MEDIA>,
2461 C<IOCTL_STORAGE_RESERVE>, C<IOCTL_STORAGE_RELEASE>,
2462 C<IOCTL_STORAGE_FIND_NEW_DEVICES>, and
2463 C<IOCTL_STORAGE_GET_MEDIA_TYPES>.
2467 =item C<IOCTL_STORAGE_CHECK_VERIFY>
2469 Verify that a device's media is accessible. C<$pInBuf> and C<$opOutBuf>
2470 should both be C<[]>. If C<DeviceIoControl> returns a true value, then
2471 the media is currently accessible.
2473 =item C<IOCTL_STORAGE_MEDIA_REMOVAL>
2475 Allows the device's media to be locked or unlocked. C<$opOutBuf> should
2476 be C<[]>. C<$pInBuf> should be a C<PREVENT_MEDIA_REMOVAL> data structure,
2477 which is simply an integer containing a boolean value:
2479 $pInBuf= pack( "i", $bPreventMediaRemoval );
2481 =item C<IOCTL_STORAGE_EJECT_MEDIA>
2483 Requests that the device eject the media. C<$pInBuf> and C<$opOutBuf>
2484 should both be C<[]>.
2486 =item C<IOCTL_STORAGE_LOAD_MEDIA>
2488 Requests that the device load the media. C<$pInBuf> and C<$opOutBuf>
2489 should both be C<[]>.
2491 =item C<IOCTL_STORAGE_RESERVE>
2493 Requests that the device be reserved. C<$pInBuf> and C<$opOutBuf>
2494 should both be C<[]>.
2496 =item C<IOCTL_STORAGE_RELEASE>
2498 Releases a previous device reservation. C<$pInBuf> and C<$opOutBuf>
2499 should both be C<[]>.
2501 =item C<IOCTL_STORAGE_FIND_NEW_DEVICES>
2503 No documentation on this IOCTL operation was found.
2505 =item C<IOCTL_STORAGE_GET_MEDIA_TYPES>
2507 Requests information about the type of media supported by the device.
2508 C<$pInBuf> should be C<[]>. C<$opOutBuf> will be set to contain a
2509 vector of C<DISK_GEOMETRY> data structures, which can be decoded via:
2511 # Calculate the number of DISK_GEOMETRY structures returned:
2512 my $cStructs= length($opOutBuf)/(4+4+4+4+4+4);
2513 my @fields= unpack( "L l I L L L" x $cStructs, $opOutBuf )
2514 my( @ucCylsLow, @ivcCylsHigh, @uMediaType, @uTracksPerCyl,
2515 @uSectsPerTrack, @uBytesPerSect )= ();
2517 push( @ucCylsLow, unshift @fields );
2518 push( @ivcCylsHigh, unshift @fields );
2519 push( @uMediaType, unshift @fields );
2520 push( @uTracksPerCyl, unshift @fields );
2521 push( @uSectsPerTrack, unshift @fields );
2522 push( @uBytesPerSect, unshift @fields );
2525 For the C<$i>th type of supported media, the following variables will
2526 contain the following data.
2530 =item C<$ucCylsLow[$i]>
2532 The low-order 4 bytes of the total number of cylinders.
2534 =item C<$ivcCylsHigh[$i]>
2536 The high-order 4 bytes of the total number of cylinders.
2538 =item C<$uMediaType[$i]>
2540 A code for the type of media. See the C<":MEDIA_TYPE"> export class.
2542 =item C<$uTracksPerCyl[$i]>
2544 The number of tracks in each cylinder.
2546 =item C<$uSectsPerTrack[$i]>
2548 The number of sectors in each track.
2550 =item C<$uBytesPerSect[$i]>
2552 The number of bytes in each sector.
2558 =item C<":IOCTL_DISK_">
2560 I/O control operations for disk devices. Used in the C<$uIoControlCode>
2561 argument to C<DeviceIoControl>. Most of these are to be used on
2562 physical drive devices like C<"//./PhysicalDrive0">. However,
2563 C<IOCTL_DISK_GET_PARTITION_INFO> and C<IOCTL_DISK_SET_PARTITION_INFO>
2564 should only be used on a single-partition device like C<"//./C:">. Also,
2565 C<IOCTL_DISK_GET_MEDIA_TYPES> is documented as having been superseded but
2566 is still useful when used on a floppy device like C<"//./A:">.
2568 Includes C<IOCTL_DISK_FORMAT_TRACKS>, C<IOCTL_DISK_FORMAT_TRACKS_EX>,
2569 C<IOCTL_DISK_GET_DRIVE_GEOMETRY>, C<IOCTL_DISK_GET_DRIVE_LAYOUT>,
2570 C<IOCTL_DISK_GET_MEDIA_TYPES>, C<IOCTL_DISK_GET_PARTITION_INFO>,
2571 C<IOCTL_DISK_HISTOGRAM_DATA>, C<IOCTL_DISK_HISTOGRAM_RESET>,
2572 C<IOCTL_DISK_HISTOGRAM_STRUCTURE>, C<IOCTL_DISK_IS_WRITABLE>,
2573 C<IOCTL_DISK_LOGGING>, C<IOCTL_DISK_PERFORMANCE>,
2574 C<IOCTL_DISK_REASSIGN_BLOCKS>, C<IOCTL_DISK_REQUEST_DATA>,
2575 C<IOCTL_DISK_REQUEST_STRUCTURE>, C<IOCTL_DISK_SET_DRIVE_LAYOUT>,
2576 C<IOCTL_DISK_SET_PARTITION_INFO>, and C<IOCTL_DISK_VERIFY>.
2580 =item C<IOCTL_DISK_GET_DRIVE_GEOMETRY>
2582 Request information about the size and geometry of the disk. C<$pInBuf>
2583 should be C<[]>. C<$opOutBuf> will be set to a C<DISK_GEOMETRY> data
2584 structure which can be decode via:
2586 ( $ucCylsLow, $ivcCylsHigh, $uMediaType, $uTracksPerCyl,
2587 $uSectsPerTrack, $uBytesPerSect )= unpack( "L l I L L L", $opOutBuf );
2593 The low-order 4 bytes of the total number of cylinders.
2595 =item C<$ivcCylsHigh>
2597 The high-order 4 bytes of the total number of cylinders.
2599 =item C<$uMediaType>
2601 A code for the type of media. See the C<":MEDIA_TYPE"> export class.
2603 =item C<$uTracksPerCyl>
2605 The number of tracks in each cylinder.
2607 =item C<$uSectsPerTrack>
2609 The number of sectors in each track.
2611 =item C<$uBytesPerSect>
2613 The number of bytes in each sector.
2617 =item C<IOCTL_DISK_GET_PARTITION_INFO>
2619 Request information about the size and geometry of the partition.
2620 C<$pInBuf> should be C<[]>. C<$opOutBuf> will be set to a
2621 C<PARTITION_INFORMATION> data structure which can be decode via:
2623 ( $uStartLow, $ivStartHigh, $ucHiddenSects, $uPartitionSeqNumber,
2624 $uPartitionType, $bActive, $bRecognized, $bToRewrite )=
2625 unpack( "L l L L C c c c", $opOutBuf );
2629 =item C<$uStartLow> and C<$ivStartHigh>
2631 The low-order and high-order [respectively] 4 bytes of the starting
2632 offset of the partition, measured in bytes.
2634 =item C<$ucHiddenSects>
2636 The number of "hidden" sectors for this partition. Actually this is
2637 the number of sectors found prior to this partition, that is, the
2638 starting offset [as found in C<$uStartLow> and C<$ivStartHigh>]
2639 divided by the number of bytes per sector.
2641 =item C<$uPartitionSeqNumber>
2643 The sequence number of this partition. Partitions are numbered
2644 starting as C<1> [with "partition 0" meaning the entire disk].
2645 Sometimes this field may be C<0> and you'll have to infer the
2646 partition sequence number from how many partitions precede it on
2649 =item C<$uPartitionType>
2651 The type of partition. See the C<":PARTITION_"> export class for a
2652 list of known types. See also C<IsRecognizedPartition> and
2653 C<IsContainerPartition>.
2657 C<1> for the active [boot] partition, C<0> otherwise.
2659 =item C<$bRecognized>
2661 Whether this type of partition is support under Win32.
2663 =item C<$bToRewrite>
2665 Whether to update this partition information. This field is not used
2666 by C<IOCTL_DISK_GET_PARTITION_INFO>. For
2667 C<IOCTL_DISK_SET_DRIVE_LAYOUT>, you must set this field to a true
2668 value for any partitions you wish to have changed, added, or deleted.
2672 =item C<IOCTL_DISK_SET_PARTITION_INFO>
2674 Change the type of the partition. C<$opOutBuf> should be C<[]>.
2675 C<$pInBuf> should be a C<SET_PARTITION_INFORMATION> data structure
2676 which is just a single byte containing the new parition type [see
2677 the C<":PARTITION_"> export class for a list of known types]:
2679 $pInBuf= pack( "C", $uPartitionType );
2681 =item C<IOCTL_DISK_GET_DRIVE_LAYOUT>
2683 Request information about the disk layout. C<$pInBuf> should be C<[]>.
2684 C<$opOutBuf> will be set to contain C<DRIVE_LAYOUT_INFORMATION>
2685 structure including several C<PARTITION_INFORMATION> structures:
2687 my( $cPartitions, $uDiskSignature )= unpack( "L L", $opOutBuf );
2688 my @fields= unpack( "x8" . ( "L l L L C c c c" x $cPartitions ),
2690 my( @uStartLow, @ivStartHigh, @ucHiddenSects,
2691 @uPartitionSeqNumber, @uPartitionType, @bActive,
2692 @bRecognized, @bToRewrite )= ();
2693 for( 1..$cPartition ) {
2694 push( @uStartLow, unshift @fields );
2695 push( @ivStartHigh, unshift @fields );
2696 push( @ucHiddenSects, unshift @fields );
2697 push( @uPartitionSeqNumber, unshift @fields );
2698 push( @uPartitionType, unshift @fields );
2699 push( @bActive, unshift @fields );
2700 push( @bRecognized, unshift @fields );
2701 push( @bToRewrite, unshift @fields );
2706 =item C<$cPartitions>
2708 If the number of partitions on the disk.
2710 =item C<$uDiskSignature>
2712 Is the disk signature, a unique number assigned by Disk Administrator
2713 [F<WinDisk.exe>] and used to identify the disk. This allows drive
2714 letters for partitions on that disk to remain constant even if the
2715 SCSI Target ID of the disk gets changed.
2719 See C<IOCTL_DISK_GET_PARTITION_INFORMATION> for information on the
2720 remaining these fields.
2722 =item C<IOCTL_DISK_GET_MEDIA_TYPES>
2724 Is supposed to be superseded by C<IOCTL_STORAGE_GET_MEDIA_TYPES> but
2725 is still useful for determining the types of floppy diskette formats
2726 that can be produced by a given floppy drive. See
2727 F<ex/FormatFloppy.plx> for an example.
2729 =item C<IOCTL_DISK_SET_DRIVE_LAYOUT>
2731 Change the partition layout of the disk. C<$pOutBuf> should be C<[]>.
2732 C<$pInBuf> should be a C<DISK_LAYOUT_INFORMATION> data structure
2733 including several C<PARTITION_INFORMATION> data structures.
2735 # Already set: $cPartitions, $uDiskSignature, @uStartLow, @ivStartHigh,
2736 # @ucHiddenSects, @uPartitionSeqNumber, @uPartitionType, @bActive,
2737 # @bRecognized, and @bToRewrite.
2738 my( @fields, $prtn )= ();
2739 for $prtn ( 1..$cPartition ) {
2740 push( @fields, $uStartLow[$prtn-1], $ivStartHigh[$prtn-1],
2741 $ucHiddenSects[$prtn-1], $uPartitionSeqNumber[$prtn-1],
2742 $uPartitionType[$prtn-1], $bActive[$prtn-1],
2743 $bRecognized[$prtn-1], $bToRewrite[$prtn-1] );
2745 $pInBuf= pack( "L L" . ( "L l L L C c c c" x $cPartitions ),
2746 $cPartitions, $uDiskSignature, @fields );
2748 To delete a partition, zero out all fields except for C<$bToRewrite>
2749 which should be set to C<1>. To add a partition, increment
2750 C<$cPartitions> and add the information for the new partition
2751 into the arrays, making sure that you insert C<1> into @bToRewrite.
2753 See C<IOCTL_DISK_GET_DRIVE_LAYOUT> and
2754 C<IOCTL_DISK_GET_PARITITON_INFORMATION> for descriptions of the
2757 =item C<IOCTL_DISK_VERIFY>
2759 Performs a logical format of [part of] the disk. C<$opOutBuf> should
2760 be C<[]>. C<$pInBuf> should contain a C<VERIFY_INFORMATION> data
2763 $pInBuf= pack( "L l L",
2764 $uStartOffsetLow, $ivStartOffsetHigh, $uLength );
2768 =item C<$uStartOffsetLow> and C<$ivStartOffsetHigh>
2770 The low-order and high-order [respectively] 4 bytes of the offset [in
2771 bytes] where the formatting should begin.
2775 The length [in bytes] of the section to be formatted.
2779 =item C<IOCTL_DISK_FORMAT_TRACKS>
2781 Format a range of tracks on the disk. C<$opOutBuf> should be C<[]>.
2782 C<$pInBuf> should contain a C<FORMAT_PARAMETERS> data structure:
2784 $pInBuf= pack( "L L L L L", $uMediaType,
2785 $uStartCyl, $uEndCyl, $uStartHead, $uEndHead );
2787 C<$uMediaType> if the type of media to be formatted. Mostly used to
2788 specify the density to use when formatting a floppy diskette. See the
2789 C<":MEDIA_TYPE"> export class for more information.
2791 The remaining fields specify the starting and ending cylinder and
2792 head of the range of tracks to be formatted.
2794 =item C<IOCTL_DISK_REASSIGN_BLOCKS>
2796 Reassign a list of disk blocks to the disk's spare-block pool.
2797 C<$opOutBuf> should be C<[]>. C<$pInBuf> should be a
2798 C<REASSIGN_BLOCKS> data structure:
2800 $pInBuf= pack( "S S L*", 0, $cBlocks, @uBlockNumbers );
2802 =item C<IOCTL_DISK_PERFORMANCE>
2804 Request information about disk performance. C<$pInBuf> should be C<[]>.
2805 C<$opOutBuf> will be set to contain a C<DISK_PERFORMANCE> data structure:
2807 my( $ucBytesReadLow, $ivcBytesReadHigh,
2808 $ucBytesWrittenLow, $ivcBytesWrittenHigh,
2809 $uReadTimeLow, $ivReadTimeHigh,
2810 $uWriteTimeLow, $ivWriteTimeHigh,
2811 $ucReads, $ucWrites, $uQueueDepth )=
2812 unpack( "L l L l L l L l L L L", $opOutBuf );
2814 =item C<IOCTL_DISK_IS_WRITABLE>
2816 No documentation on this IOCTL operation was found.
2818 =item C<IOCTL_DISK_LOGGING>
2820 Control disk logging. Little documentation for this IOCTL operation
2821 was found. It makes use of a C<DISK_LOGGING> data structure:
2825 =item DISK_LOGGING_START
2827 Start logging each disk request in a buffer internal to the disk device
2828 driver of size C<$uLogBufferSize>:
2830 $pInBuf= pack( "C L L", 0, 0, $uLogBufferSize );
2832 =item DISK_LOGGING_STOP
2834 Stop loggin each disk request:
2836 $pInBuf= pack( "C L L", 1, 0, 0 );
2838 =item DISK_LOGGING_DUMP
2840 Copy the internal log into the supplied buffer:
2842 $pLogBuffer= ' ' x $uLogBufferSize
2843 $pInBuf= pack( "C P L", 2, $pLogBuffer, $uLogBufferSize );
2845 ( $uByteOffsetLow[$i], $ivByteOffsetHigh[$i],
2846 $uStartTimeLow[$i], $ivStartTimeHigh[$i],
2847 $uEndTimeLog[$i], $ivEndTimeHigh[$i],
2848 $hVirtualAddress[$i], $ucBytes[$i],
2849 $uDeviceNumber[$i], $bWasReading[$i] )=
2850 unpack( "x".(8+8+8+4+4+1+1+2)." L l L l L l L L C c x2", $pLogBuffer );
2852 =item DISK_LOGGING_BINNING
2854 Keep statics grouped into bins based on request sizes.
2856 $pInBuf= pack( "C P L", 3, $pUnknown, $uUnknownSize );
2860 =item C<IOCTL_DISK_FORMAT_TRACKS_EX>
2862 No documentation on this IOCTL is included.
2864 =item C<IOCTL_DISK_HISTOGRAM_STRUCTURE>
2866 No documentation on this IOCTL is included.
2868 =item C<IOCTL_DISK_HISTOGRAM_DATA>
2870 No documentation on this IOCTL is included.
2872 =item C<IOCTL_DISK_HISTOGRAM_RESET>
2874 No documentation on this IOCTL is included.
2876 =item C<IOCTL_DISK_REQUEST_STRUCTURE>
2878 No documentation on this IOCTL operation was found.
2880 =item C<IOCTL_DISK_REQUEST_DATA>
2882 No documentation on this IOCTL operation was found.
2888 File system control operations. Used in the C<$uIoControlCode>
2889 argument to C<DeviceIoControl>.
2891 Includes C<FSCTL_SET_REPARSE_POINT>, C<FSCTL_GET_REPARSE_POINT>,
2892 C<FSCTL_DELETE_REPARSE_POINT>.
2896 =item C<FSCTL_SET_REPARSE_POINT>
2898 Sets reparse point data to be associated with $hDevice.
2900 =item C<FSCTL_GET_REPARSE_POINT>
2902 Retrieves the reparse point data associated with $hDevice.
2904 =item C<FSCTL_DELETE_REPARSE_POINT>
2906 Deletes the reparse point data associated with $hDevice.
2910 =item C<":GENERIC_">
2912 Constants specifying generic access permissions that are not specific
2913 to one type of object.
2915 GENERIC_ALL GENERIC_EXECUTE
2916 GENERIC_READ GENERIC_WRITE
2918 =item C<":MEDIA_TYPE">
2920 Different classes of media that a device can support. Used in the
2921 C<$uMediaType> field of a C<DISK_GEOMETRY> structure.
2929 =item C<F5_1Pt2_512>
2931 5.25" floppy, 1.2MB [really 1,200KB] total space, 512 bytes/sector.
2933 =item C<F3_1Pt44_512>
2935 3.5" floppy, 1.44MB [really 1,440KB] total space, 512 bytes/sector.
2937 =item C<F3_2Pt88_512>
2939 3.5" floppy, 2.88MB [really 2,880KB] total space, 512 bytes/sector.
2941 =item C<F3_20Pt8_512>
2943 3.5" floppy, 20.8MB total space, 512 bytes/sector.
2947 3.5" floppy, 720KB total space, 512 bytes/sector.
2951 5.25" floppy, 360KB total space, 512 bytes/sector.
2955 5.25" floppy, 320KB total space, 512 bytes/sector.
2957 =item C<F5_320_1024>
2959 5.25" floppy, 320KB total space, 1024 bytes/sector.
2963 5.25" floppy, 180KB total space, 512 bytes/sector.
2967 5.25" floppy, 160KB total space, 512 bytes/sector.
2969 =item C<RemovableMedia>
2971 Some type of removable media other than a floppy diskette.
2977 =item C<F3_120M_512>
2979 3.5" floppy, 120MB total space.
2983 =item C<":MOVEFILE_">
2985 Constants for use in C<$uFlags> arguments to C<MoveFileEx>.
2987 MOVEFILE_COPY_ALLOWED MOVEFILE_DELAY_UNTIL_REBOOT
2988 MOVEFILE_REPLACE_EXISTING MOVEFILE_WRITE_THROUGH
2990 =item C<":SECURITY_">
2992 Security quality of service values that can be used in the C<$uFlags>
2993 argument to C<CreateFile> if opening the client side of a named pipe.
2995 SECURITY_ANONYMOUS SECURITY_CONTEXT_TRACKING
2996 SECURITY_DELEGATION SECURITY_EFFECTIVE_ONLY
2997 SECURITY_IDENTIFICATION SECURITY_IMPERSONATION
2998 SECURITY_SQOS_PRESENT
3002 Constants to be used with C<SetErrorMode>.
3004 SEM_FAILCRITICALERRORS SEM_NOGPFAULTERRORBOX
3005 SEM_NOALIGNMENTFAULTEXCEPT SEM_NOOPENFILEERRORBOX
3007 =item C<":PARTITION_">
3009 Constants describing partition types.
3011 PARTITION_ENTRY_UNUSED PARTITION_FAT_12
3012 PARTITION_XENIX_1 PARTITION_XENIX_2
3013 PARTITION_FAT_16 PARTITION_EXTENDED
3014 PARTITION_HUGE PARTITION_IFS
3015 PARTITION_FAT32 PARTITION_FAT32_XINT13
3016 PARTITION_XINT13 PARTITION_XINT13_EXTENDED
3017 PARTITION_PREP PARTITION_UNIX
3018 VALID_NTFT PARTITION_NTFT
3020 =item C<":STD_HANDLE_">
3022 Constants for GetStdHandle and SetStdHandle
3036 None known at this time.
3040 Tye McQueen, tye@metronet.com, http://perlmonks.org/?node=tye.