home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / TieRegistry.pm < prev    next >
Text File  |  2004-01-12  |  123KB  |  3,801 lines

  1. # Win32/TieRegistry.pm -- Perl module to easily use a Registry
  2. # (on Win32 systems so far).
  3. # by Tye McQueen, tye@metronet.com, see http://www.metronet.com/~tye/.
  4.  
  5. #
  6. # Skip to "=head" line for user documentation.
  7. #
  8.  
  9.  
  10. package Win32::TieRegistry;
  11.  
  12.  
  13. use strict;
  14. use vars qw( $PACK $VERSION @ISA @EXPORT @EXPORT_OK );
  15.  
  16. $PACK= "Win32::TieRegistry";    # Used in error messages.
  17. $VERSION= '0.24';        # Released 2001-02-06
  18.  
  19.  
  20. use Carp;
  21.  
  22. require Tie::Hash;
  23. @ISA= qw(Tie::Hash);
  24.  
  25. # Required other modules:
  26. use Win32API::Registry 0.12 qw( :KEY_ :HKEY_ :REG_ );
  27.  
  28. #Optional other modules:
  29. use vars qw( $_NoMoreItems $_FileNotFound $_TooSmall $_MoreData $_SetDualVar );
  30.  
  31. if(  eval { require Win32::WinError }  ) {
  32.     $_NoMoreItems= Win32::WinError::constant("ERROR_NO_MORE_ITEMS",0);
  33.     $_FileNotFound= Win32::WinError::constant("ERROR_FILE_NOT_FOUND",0);
  34.     $_TooSmall= Win32::WinError::constant("ERROR_INSUFFICIENT_BUFFER",0);
  35.     $_MoreData= Win32::WinError::constant("ERROR_MORE_DATA",0);
  36. } else {
  37.     $_NoMoreItems= "^No more data";
  38.     $_FileNotFound= "cannot find the file";
  39.     $_TooSmall= " data area passed to ";
  40.     $_MoreData= "^more data is avail";
  41. }
  42. if(  $_SetDualVar= eval { require SetDualVar }  ) {
  43.     import SetDualVar;
  44. }
  45.  
  46.  
  47. #Implementation details:
  48. #    When opened:
  49. #    HANDLE        long; actual handle value
  50. #    MACHINE        string; name of remote machine ("" if local)
  51. #    PATH        list ref; machine-relative full path for this key:
  52. #            ["LMachine","System","Disk"]
  53. #            ["HKEY_LOCAL_MACHINE","System","Disk"]
  54. #    DELIM        char; delimiter used to separate subkeys (def="\\")
  55. #    OS_DELIM    char; always "\\" for Win32
  56. #    ACCESS        long; usually KEY_ALL_ACCESS, perhaps KEY_READ, etc.
  57. #    ROOTS        string; var name for "Lmachine"->HKEY_LOCAL_MACHINE map
  58. #    FLAGS        int; bits to control certain options
  59. #    Often:
  60. #    VALUES        ref to list of value names (data/type never cached)
  61. #    SUBKEYS        ref to list of subkey names
  62. #    SUBCLASSES    ref to list of subkey classes
  63. #    SUBTIMES    ref to list of subkey write times
  64. #    MEMBERS        ref to list of subkey_name.DELIM's, DELIM.value_name's
  65. #    MEMBHASH    hash ref to with MEMBERS as keys and 1's as values
  66. #    Once Key "Info" requested:
  67. #    Class CntSubKeys CntValues MaxSubKeyLen MaxSubClassLen
  68. #    MaxValNameLen MaxValDataLen SecurityLen LastWrite
  69. #    If is tied to a hash and iterating over key values:
  70. #    PREVIDX        int; index of last MEMBERS element return
  71. #    If is the key object returned by Load():
  72. #    UNLOADME    list ref; information about Load()ed key
  73. #    If is a subkey of a "loaded" key other than the one returned by Load():
  74. #    DEPENDON    obj ref; object that can't be destroyed before us
  75.  
  76.  
  77. #Package-local variables:
  78.  
  79. # Option flag bits:
  80. use vars qw( $Flag_ArrVal $Flag_TieVal $Flag_DualTyp $Flag_DualBin
  81.          $Flag_FastDel $Flag_HexDWord $Flag_Split $Flag_FixNulls );
  82. $Flag_ArrVal=    0x0001;
  83. $Flag_TieVal=    0x0002;
  84. $Flag_FastDel=    0x0004;
  85. $Flag_HexDWord=    0x0008;
  86. $Flag_Split=    0x0010;
  87. $Flag_DualTyp=    0x0020;
  88. $Flag_DualBin=    0x0040;
  89. $Flag_FixNulls=    0x0080;
  90.  
  91.  
  92. use vars qw( $RegObj %_Roots %RegHash $Registry );
  93.  
  94. # Short-hand for HKEY_* constants:
  95. %_Roots= (
  96.     "Classes" =>    HKEY_CLASSES_ROOT,
  97.     "CUser" =>        HKEY_CURRENT_USER,
  98.     "LMachine" =>    HKEY_LOCAL_MACHINE,
  99.     "Users" =>        HKEY_USERS,
  100.     "PerfData" =>    HKEY_PERFORMANCE_DATA,    # Too picky to be useful
  101.     "CConfig" =>    HKEY_CURRENT_CONFIG,
  102.     "DynData" =>    HKEY_DYN_DATA,        # Too picky to be useful
  103. );
  104.  
  105. # Basic master Registry object:
  106. $RegObj= {};
  107. @$RegObj{qw( HANDLE MACHINE PATH DELIM OS_DELIM ACCESS FLAGS ROOTS )}= (
  108.     "NONE", "", [], "\\", "\\",
  109.     KEY_READ|KEY_WRITE, $Flag_HexDWord|$Flag_FixNulls, "${PACK}::_Roots" );
  110. $RegObj->{FLAGS} |= $Flag_DualTyp|$Flag_DualBin   if  $_SetDualVar;
  111. bless $RegObj;
  112.  
  113. # Fill cache for master Registry object:
  114. @$RegObj{qw( VALUES SUBKEYS SUBCLASSES SUBTIMES )}= (
  115.     [],  [ keys(%_Roots) ],  [],  []  );
  116. grep( s#$#$RegObj->{DELIM}#,
  117.   @{ $RegObj->{MEMBERS}= [ @{$RegObj->{SUBKEYS}} ] } );
  118. @$RegObj{qw( Class MaxSubKeyLen MaxSubClassLen MaxValNameLen
  119.   MaxValDataLen SecurityLen LastWrite CntSubKeys CntValues )}=
  120.     ( "", 0, 0, 0, 0, 0, 0, 0, 0 );
  121.  
  122. # Create master Registry tied hash:
  123. $RegObj->Tie( \%RegHash );
  124.  
  125. # Create master Registry combination object and tied hash reference:
  126. $Registry= \%RegHash;
  127. bless $Registry;
  128.  
  129.  
  130. # Preloaded methods go here.
  131.  
  132.  
  133. # Map option names to name of subroutine that controls that option:
  134. use vars qw( @_opt_subs %_opt_subs );
  135. @_opt_subs= qw( Delimiter ArrayValues TieValues SplitMultis DWordsToHex
  136.     FastDelete FixSzNulls DualTypes DualBinVals AllowLoad AllowSave );
  137. @_opt_subs{@_opt_subs}= @_opt_subs;
  138.  
  139. sub import
  140. {
  141.     my $pkg= shift(@_);
  142.     my $level= $Exporter::ExportLevel;
  143.     my $expto= caller($level);
  144.     my @export= ();
  145.     my @consts= ();
  146.     my $registry= $Registry->Clone;
  147.     local( $_ );
  148.     while(  @_  ) {
  149.     $_= shift(@_);
  150.     if(  /^\$(\w+::)*\w+$/  ) {
  151.         push( @export, "ObjVar" )   if  /^\$RegObj$/;
  152.         push( @export, $_ );
  153.     } elsif(  /^\%(\w+::)*\w+$/  ) {
  154.         push( @export, $_ );
  155.     } elsif(  /^[$%]/  ) {
  156.         croak "${PACK}->import:  Invalid variable name ($_)";
  157.     } elsif(  /^:/  ||  /^(H?KEY|REG)_/  ) {
  158.         push( @consts, $_ );
  159.     } elsif(  ! @_  ) {
  160.         croak "${PACK}->import:  Missing argument after option ($_)";
  161.     } elsif(  exists $_opt_subs{$_}  ) {
  162.         $_= $_opt_subs{$_};
  163.         $registry->$_( shift(@_) );
  164.     } elsif(  /^TiedRef$/  ) {
  165.         $_= shift(@_);
  166.         if(  ! ref($_)  &&  /^(\$?)(\w+::)*\w+$/  ) {
  167.         $_= '$'.$_   unless  '$' eq $1;
  168.         } elsif(  "SCALAR" ne ref($_)  ) {
  169.         croak "${PACK}->import:  Invalid var after TiedRef ($_)";
  170.         }
  171.         push( @export, $_ );
  172.     } elsif(  /^TiedHash$/  ) {
  173.         $_= shift(@_);
  174.         if(  ! ref($_)  &&  /^(\%?)(\w+::)*\w+$/  ) {
  175.         $_= '%'.$_   unless  '%' eq $1;
  176.         } elsif(  "HASH" ne ref($_)  ) {
  177.         croak "${PACK}->import:  Invalid var after TiedHash ($_)";
  178.         }
  179.         push( @export, $_ );
  180.     } elsif(  /^ObjectRef$/  ) {
  181.         $_= shift(@_);
  182.         if(  ! ref($_)  &&  /^(\$?)(\w+::)*\w+$/  ) {
  183.         push( @export, "ObjVar" );
  184.         $_= '$'.$_   unless  '$' eq $1;
  185.         } elsif(  "SCALAR" eq ref($_)  ) {
  186.         push( @export, "ObjRef" );
  187.         } else {
  188.         croak "${PACK}->import:  Invalid var after ObjectRef ($_)";
  189.         }
  190.         push( @export, $_ );
  191.     } elsif(  /^ExportLevel$/  ) {
  192.         $level= shift(@_);
  193.         $expto= caller($level);
  194.     } elsif(  /^ExportTo$/  ) {
  195.         undef $level;
  196.         $expto= caller($level);
  197.     } else {
  198.         croak "${PACK}->import:  Invalid option ($_)";
  199.     }
  200.     }
  201.     Win32API::Registry->export( $expto, @consts )   if  @consts;
  202.     @export= ('$Registry')   unless  @export;
  203.     while(  @export  ) {
  204.     $_= shift( @export );
  205.     if(  /^\$((?:\w+::)*)(\w+)$/  ) {
  206.         my( $pack, $sym )= ( $1, $2 );
  207.         $pack= $expto   unless  defined($pack)  &&  "" ne $pack;
  208.         no strict 'refs';
  209.         *{"${pack}::$sym"}= \${"${pack}::$sym"};
  210.         ${"${pack}::$sym"}= $registry;
  211.     } elsif(  /^\%((?:\w+::)*)(\w+)$/  ) {
  212.         my( $pack, $sym )= ( $1, $2 );
  213.         $pack= $expto   unless  defined($pack)  &&  "" ne $pack;
  214.         no strict 'refs';
  215.         *{"${pack}::$sym"}= \%{"${pack}::$sym"};
  216.         $registry->Tie( \%{"${pack}::$sym"} );
  217.     } elsif(  "SCALAR" eq ref($_)  ) {
  218.         $$_= $registry;
  219.     } elsif(  "HASH" eq ref($_)  ) {
  220.         $registry->Tie( $_ );
  221.     } elsif(  /^ObjVar$/  ) {
  222.         $_= shift( @_ );
  223.         /^\$((?:\w+::)*)(\w+)$/;
  224.         my( $pack, $sym )= ( $1, $2 );
  225.         $pack= $expto   unless  defined($pack)  &&  "" ne $pack;
  226.         no strict 'refs';
  227.         *{"${pack}::$sym"}= \${"${pack}::$sym"};
  228.         ${"${pack}::$sym"}= $registry->ObjectRef;
  229.     } elsif(  /^ObjRef$/  ) {
  230.         ${shift(@_)}= $registry->ObjectRef;
  231.     } else {
  232.         die "Impossible var to export ($_)";
  233.     }
  234.     }
  235. }
  236.  
  237.  
  238. use vars qw( @_new_Opts %_new_Opts );
  239. @_new_Opts= qw( ACCESS DELIM MACHINE DEPENDON );
  240. @_new_Opts{@_new_Opts}= (1) x @_new_Opts;
  241.  
  242. sub _new
  243. {
  244.     my $this= shift( @_ );
  245.     $this= tied(%$this)   if  ref($this)  &&  tied(%$this);
  246.     my $class= ref($this) || $this;
  247.     my $self= {};
  248.     my( $handle, $rpath, $opts )= @_;
  249.     if(  @_ < 2  ||  "ARRAY" ne ref($rpath)  ||  3 < @_
  250.      ||  3 == @_ && "HASH" ne ref($opts)  ) {
  251.     croak "Usage:  ${PACK}->_new( \$handle, \\\@path, {OPT=>VAL,...} );\n",
  252.           "  options: @_new_Opts\nCalled";
  253.     }
  254.     @$self{qw( HANDLE PATH )}= ( $handle, $rpath );
  255.     @$self{qw( MACHINE ACCESS DELIM OS_DELIM ROOTS FLAGS )}=
  256.       ( $this->Machine, $this->Access, $this->Delimiter,
  257.         $this->OS_Delimiter, $this->_Roots, $this->_Flags );
  258.     if(  ref($opts)  ) {
  259.     my @err= grep( ! $_new_Opts{$_}, keys(%$opts) );
  260.     @err  and  croak "${PACK}->_new:  Invalid options (@err)";
  261.     @$self{ keys(%$opts) }= values(%$opts);
  262.     }
  263.     bless $self, $class;
  264.     return $self;
  265. }
  266.  
  267.  
  268. sub _split
  269. {
  270.     my $self= shift( @_ );
  271.     $self= tied(%$self)   if  tied(%$self);
  272.     my $path= shift( @_ );
  273.     my $delim= @_ ? shift(@_) : $self->Delimiter;
  274.     my $list= [ split( /\Q$delim/, $path ) ];
  275.     return $list;
  276. }
  277.  
  278.  
  279. sub _rootKey
  280. {
  281.     my $self= shift(@_);
  282.     $self= tied(%$self)   if  tied(%$self);
  283.     my $keyPath= shift(@_);
  284.     my $delim= @_ ? shift(@_) : $self->Delimiter;
  285.     my( $root, $subPath );
  286.     if(  "ARRAY" eq ref($keyPath)  ) {
  287.     $subPath= $keyPath;
  288.     } else {
  289.     $subPath= $self->_split( $keyPath, $delim );
  290.     }
  291.     $root= shift( @$subPath );
  292.     if(  $root =~ /^HKEY_/  ) {
  293.     my $handle= Win32API::Registry::constant($root,0);
  294.     $handle  or  croak "Invalid HKEY_ constant ($root): $!";
  295.     return( $self->_new( $handle, [$root], {DELIM=>$delim} ),
  296.             $subPath );
  297.     } elsif(  $root =~ /^([-+]|0x)?\d/  ) {
  298.     return( $self->_new( $root, [sprintf("0x%lX",$root)],
  299.                  {DELIM=>$delim} ),
  300.         $subPath );
  301.     } else {
  302.     my $roots= $self->Roots;
  303.     if(  $roots->{$root}  ) {
  304.         return( $self->_new( $roots->{$root}, [$root], {DELIM=>$delim} ),
  305.                 $subPath );
  306.     }
  307.     croak "No such root key ($root)";
  308.     }
  309. }
  310.  
  311.  
  312. sub _open
  313. {
  314.     my $this= shift(@_);
  315.     $this= tied(%$this)   if  ref($this)  &&  tied(%$this);
  316.     my $subPath= shift(@_);
  317.     my $sam= @_ ? shift(@_) : $this->Access;
  318.     my $subKey= join( $this->OS_Delimiter, @$subPath );
  319.     my $handle= 0;
  320.     $this->RegOpenKeyEx( $subKey, 0, $sam, $handle )
  321.       or  return ();
  322.     return $this->_new( $handle, [ @{$this->_Path}, @$subPath ],
  323.       { ACCESS=>$sam, ( defined($this->{UNLOADME}) ? ("DEPENDON",$this)
  324.     : defined($this->{DEPENDON}) ? ("DEPENDON",$this->{DEPENDON}) : () )
  325.       } );
  326. }
  327.  
  328.  
  329. sub ObjectRef
  330. {
  331.     my $self= shift(@_);
  332.     $self= tied(%$self)   if  tied(%$self);
  333.     return $self;
  334. }
  335.  
  336.  
  337. sub _constant
  338. {
  339.     my( $name, $desc )= @_;
  340.     my $value= Win32API::Registry::constant( $name, 0 );
  341.     my $func= (caller(1))[3];
  342.     if(  0 == $value  ) {
  343.     if(  $! =~ /invalid/i  ) {
  344.         croak "$func: Invalid $desc ($name)";
  345.     } elsif(  0 != $!  ) {
  346.         croak "$func: \u$desc ($name) not support on this platform";
  347.     }
  348.     }
  349.     return $value;
  350. }
  351.  
  352.  
  353. sub _connect
  354. {
  355.     my $this= shift(@_);
  356.     $this= tied(%$this)   if  ref($this)  &&  tied(%$this);
  357.     my $subPath= pop(@_);
  358.     $subPath= $this->_split( $subPath )   unless  ref($subPath);
  359.     my $machine= @_ ? shift(@_) : shift(@$subPath);
  360.     my $handle= 0;
  361.     my( $temp )= $this->_rootKey( [@$subPath] );
  362.     $temp->RegConnectRegistry( $machine, $temp->Handle, $handle )
  363.       or  return ();
  364.     my $self= $this->_new( $handle, [shift(@$subPath)], {MACHINE=>$machine} );
  365.     return( $self, $subPath );
  366. }
  367.  
  368.  
  369. use vars qw( @Connect_Opts %Connect_Opts );
  370. @Connect_Opts= qw(Access Delimiter);
  371. @Connect_Opts{@Connect_Opts}= (1) x @Connect_Opts;
  372.  
  373. sub Connect
  374. {
  375.     my $this= shift(@_);
  376.     my $tied=  ref($this)  &&  tied(%$this);
  377.     $this= tied(%$this)   if  $tied;
  378.     my( $machine, $key, $opts )= @_;
  379.     my $delim= "";
  380.     my $sam;
  381.     my $subPath;
  382.     if(  @_ < 2  ||  3 < @_
  383.      ||  3 == @_ && "HASH" ne ref($opts)  ) {
  384.     croak "Usage:  \$obj= ${PACK}->Connect(",
  385.           " \$Machine, \$subKey, { OPT=>VAL,... } );\n",
  386.           "  options: @Connect_Opts\nCalled";
  387.     }
  388.     if(  ref($opts)  ) {
  389.     my @err= grep( ! $Connect_Opts{$_}, keys(%$opts) );
  390.     @err  and  croak "${PACK}->Connect:  Invalid options (@err)";
  391.     }
  392.     $delim= "$opts->{Delimiter}"   if  defined($opts->{Delimiter});
  393.     $delim= $this->Delimiter   if  "" eq $delim;
  394.     $sam= defined($opts->{Access}) ? $opts->{Access} : $this->Access;
  395.     $sam= _constant($sam,"key access type")   if  $sam =~ /^KEY_/;
  396.     ( $this, $subPath )= $this->_connect( $machine, $key );
  397.     return ()   unless  defined($this);
  398.     my $self= $this->_open( $subPath, $sam );
  399.     return ()   unless  defined($self);
  400.     $self->Delimiter( $delim );
  401.     $self= $self->TiedRef   if  $tied;
  402.     return $self;
  403. }
  404.  
  405.  
  406. my @_newVirtual_keys= qw( MEMBERS VALUES SUBKEYS SUBTIMES SUBCLASSES
  407.     Class SecurityLen LastWrite CntValues CntSubKeys
  408.     MaxValNameLen MaxValDataLen MaxSubKeyLen MaxSubClassLen );
  409.  
  410. sub _newVirtual
  411. {
  412.     my $self= shift(@_);
  413.     my( $rPath, $root, $opts )= @_;
  414.     my $new= $self->_new( "NONE", $rPath, $opts )
  415.       or  return ();
  416.     @{$new}{@_newVirtual_keys}= @{$root->ObjectRef}{@_newVirtual_keys};
  417.     return $new;
  418. }
  419.  
  420.  
  421. #$key= new Win32::TieRegistry "LMachine/System/Disk";
  422. #$key= new Win32::TieRegistry "//Server1/LMachine/System/Disk";
  423. #Win32::TieRegistry->new( HKEY_LOCAL_MACHINE, {DELIM=>"/",ACCESS=>KEY_READ} );
  424. #Win32::TieRegistry->new( [ HKEY_LOCAL_MACHINE, ".../..." ], {DELIM=>$DELIM} );
  425. #$key->new( ... );
  426.  
  427. use vars qw( @new_Opts %new_Opts );
  428. @new_Opts= qw(Access Delimiter);
  429. @new_Opts{@new_Opts}= (1) x @new_Opts;
  430.  
  431. sub new
  432. {
  433.     my $this= shift( @_ );
  434.     $this= tied(%$this)   if  ref($this)  &&  tied(%$this);
  435.     if(  ! ref($this)  ) {
  436.     no strict "refs";
  437.     my $self= ${"${this}::Registry"};
  438.     croak "${this}->new failed since ${PACK}::new sees that ",
  439.       "\$${this}::Registry is not an object."
  440.       if  ! ref($self);
  441.     $this= $self->Clone;
  442.     }
  443.     my( $subKey, $opts )= @_;
  444.     my $delim= "";
  445.     my $dlen;
  446.     my $sam;
  447.     my $subPath;
  448.     if(  @_ < 1  ||  2 < @_
  449.      ||  2 == @_ && "HASH" ne ref($opts)  ) {
  450.     croak "Usage:  \$obj= ${PACK}->new( \$subKey, { OPT=>VAL,... } );\n",
  451.           "  options: @new_Opts\nCalled";
  452.     }
  453.     if(  defined($opts)  ) {
  454.     my @err= grep( ! $new_Opts{$_}, keys(%$opts) );
  455.     @err  and  die "${PACK}->new:  Invalid options (@err)";
  456.     }
  457.     $delim= "$opts->{Delimiter}"   if  defined($opts->{Delimiter});
  458.     $delim= $this->Delimiter   if  "" eq $delim;
  459.     $dlen= length($delim);
  460.     $sam= defined($opts->{Access}) ? $opts->{Access} : $this->Access;
  461.     $sam= _constant($sam,"key access type")   if  $sam =~ /^KEY_/;
  462.     if(  "ARRAY" eq ref($subKey)  ) {
  463.     $subPath= $subKey;
  464.     if(  "NONE" eq $this->Handle  &&  @$subPath  ) {
  465.         ( $this, $subPath )= $this->_rootKey( $subPath );
  466.     }
  467.     } elsif(  $delim x 2 eq substr($subKey,0,2*$dlen)  ) {
  468.     my $path= $this->_split( substr($subKey,2*$dlen), $delim );
  469.     my $mach= shift(@$path);
  470.     if(  ! @$path  ) {
  471.         return $this->_newVirtual( $path, $Registry,
  472.                 {MACHINE=>$mach,DELIM=>$delim,ACCESS=>$sam} );
  473.     }
  474.     ( $this, $subPath )= $this->_connect( $mach, $path );
  475.     return ()   if  ! defined($this);
  476.     if(  0 == @$subPath  ) {
  477.         $this->Delimiter( $delim );
  478.         return $this;
  479.     }
  480.     } elsif(  $delim eq substr($subKey,0,$dlen)  ) {
  481.     ( $this, $subPath )= $this->_rootKey( substr($subKey,$dlen), $delim );
  482.     } elsif(  "NONE" eq $this->Handle  &&  "" ne $subKey  ) {
  483.     my( $mach )= $this->Machine;
  484.     if(  $mach  ) {
  485.         ( $this, $subPath )= $this->_connect( $mach, $subKey );
  486.     } else {
  487.         ( $this, $subPath )= $this->_rootKey( $subKey, $delim );
  488.     }
  489.     } else {
  490.     $subPath= $this->_split( $subKey, $delim );
  491.     }
  492.     return ()   unless  defined($this);
  493.     if(  0 == @$subPath  &&  "NONE" eq $this->Handle  ) {
  494.     return $this->_newVirtual( $this->_Path, $this,
  495.                    { DELIM=>$delim, ACCESS=>$sam } );
  496.     }
  497.     my $self= $this->_open( $subPath, $sam );
  498.     return ()   unless  defined($self);
  499.     $self->Delimiter( $delim );
  500.     return $self;
  501. }
  502.  
  503.  
  504. sub Open
  505. {
  506.     my $self= shift(@_);
  507.     my $tied=  ref($self)  &&  tied(%$self);
  508.     $self= tied(%$self)   if  $tied;
  509.     $self= $self->new( @_ );
  510.     $self= $self->TiedRef   if  defined($self)  &&  $tied;
  511.     return $self;
  512. }
  513.  
  514.  
  515. sub Clone
  516. {
  517.     my $self= shift( @_ );
  518.     my $new= $self->Open("");
  519.     return $new;
  520. }
  521.  
  522.  
  523. { my @flush;
  524.     sub Flush
  525.     {
  526.     my $self= shift(@_);
  527.     $self= tied(%$self)   if  tied(%$self);
  528.     my( $flush )= @_;
  529.     @_  and  croak "Usage:  \$key->Flush( \$bFlush );";
  530.     return 0   if  "NONE" eq $self->Handle;
  531.     @flush= qw( VALUES SUBKEYS SUBCLASSES SUBTIMES MEMBERS Class
  532.             CntSubKeys CntValues MaxSubKeyLen MaxSubClassLen
  533.             MaxValNameLen MaxValDataLen SecurityLen LastWrite PREVIDX )
  534.       unless  @flush;
  535.     delete( @$self{@flush} );
  536.     if(  defined($flush)  &&  $flush  ) {
  537.         return $self->RegFlushKey();
  538.     } else {
  539.         return 1;
  540.     }
  541.     }
  542. }
  543.  
  544.  
  545. sub _DualVal
  546. {
  547.     my( $hRef, $num )= @_;
  548.     if(  $_SetDualVar  &&  $$hRef{$num}  ) {
  549.     &SetDualVar( $num, "$$hRef{$num}", 0+$num );
  550.     }
  551.     return $num;
  552. }
  553.  
  554.  
  555. use vars qw( @_RegDataTypes %_RegDataTypes );
  556. @_RegDataTypes= qw( REG_SZ REG_EXPAND_SZ REG_BINARY REG_LINK REG_MULTI_SZ
  557.             REG_DWORD_LITTLE_ENDIAN REG_DWORD_BIG_ENDIAN REG_DWORD
  558.             REG_RESOURCE_LIST REG_FULL_RESOURCE_DESCRIPTOR
  559.             REG_RESOURCE_REQUIREMENTS_LIST REG_NONE );
  560. # Make sure that REG_DWORD appears _after_ other REG_DWORD_*
  561. # items above and that REG_NONE appears _last_.
  562. foreach(  @_RegDataTypes  ) {
  563.     $_RegDataTypes{Win32API::Registry::constant($_,0)}= $_;
  564. }
  565.  
  566. sub GetValue
  567. {
  568.     my $self= shift(@_);
  569.     $self= tied(%$self)   if  tied(%$self);
  570.     1 == @_  or  croak "Usage:  (\$data,\$type)= \$key->GetValue('ValName');";
  571.     my( $valName )= @_;
  572.     my( $valType, $valData, $dLen )= (0,"",0);
  573.     return ()   if  "NONE" eq $self->Handle;
  574.     $self->RegQueryValueEx( $valName, [], $valType, $valData,
  575.       $dLen= ( defined($self->{MaxValDataLen}) ? $self->{MaxValDataLen} : 0 )
  576.     )  or  return ();
  577.     if(  REG_DWORD == $valType  ) {
  578.     my $val= unpack("L",$valData);
  579.     $valData= sprintf "0x%08.8lX", $val   if  $self->DWordsToHex;
  580.     &SetDualVar( $valData, $valData, $val )   if  $self->DualBinVals
  581.     } elsif(  REG_BINARY == $valType  &&  length($valData) <= 4  ) {
  582.     &SetDualVar( $valData, $valData, hex reverse unpack("h*",$valData) )
  583.       if  $self->DualBinVals;
  584.     } elsif(  ( REG_SZ == $valType || REG_EXPAND_SZ == $valType )
  585.           &&  $self->FixSzNulls  ) {
  586.     substr($valData,-1)= ""   if  "\0" eq substr($valData,-1);
  587.     } elsif(  REG_MULTI_SZ == $valType  &&  $self->SplitMultis  ) {
  588.     ## $valData =~ s/\0\0$//;    # Why does this often fail??
  589.     substr($valData,-2)= ""   if  "\0\0" eq substr($valData,-2);
  590.     $valData= [ split( /\0/, $valData, -1 ) ]
  591.     }
  592.     if(  ! wantarray  ) {
  593.     return $valData;
  594.     } elsif(  ! $self->DualTypes  ) {
  595.     return( $valData, $valType );
  596.     } else {
  597.     return(  $valData,  _DualVal( \%_RegDataTypes, $valType )  );
  598.     }
  599. }
  600.  
  601.  
  602. sub _ErrNum
  603. {
  604.     # return $^E;
  605.     return Win32::GetLastError();
  606. }
  607.  
  608.  
  609. sub _ErrMsg
  610. {
  611.     # return $^E;
  612.     return Win32::FormatMessage( Win32::GetLastError() );
  613. }
  614.  
  615. sub _Err
  616. {
  617.     my $err;
  618.     # return $^E;
  619.     return _ErrMsg   if  ! $_SetDualVar;
  620.     return &SetDualVar( $err, _ErrMsg, _ErrNum );
  621. }
  622.  
  623. sub _NoMoreItems
  624. {
  625.     return
  626.       $_NoMoreItems =~ /^\d/
  627.         ?  _ErrNum == $_NoMoreItems
  628.         :  _ErrMsg =~ /$_NoMoreItems/io;
  629. }
  630.  
  631.  
  632. sub _FileNotFound
  633. {
  634.     return
  635.       $_FileNotFound =~ /^\d/
  636.         ?  _ErrNum == $_FileNotFound
  637.         :  _ErrMsg =~ /$_FileNotFound/io;
  638. }
  639.  
  640.  
  641. sub _TooSmall
  642. {
  643.     return
  644.       $_TooSmall =~ /^\d/
  645.         ?  _ErrNum == $_TooSmall
  646.         :  _ErrMsg =~ /$_TooSmall/io;
  647. }
  648.  
  649.  
  650. sub _MoreData
  651. {
  652.     return
  653.       $_MoreData =~ /^\d/
  654.         ?  _ErrNum == $_MoreData
  655.         :  _ErrMsg =~ /$_MoreData/io;
  656. }
  657.  
  658.  
  659. sub _enumValues
  660. {
  661.     my $self= shift(@_);
  662.     $self= tied(%$self)   if  tied(%$self);
  663.     my( @names )= ();
  664.     my $pos= 0;
  665.     my $name= "";
  666.     my $nlen= 1+$self->Information("MaxValNameLen");
  667.     while(  $self->RegEnumValue($pos++,$name,$nlen,[],[],[],[])  ) {
  668.     push( @names, $name );
  669.     }
  670.     if(  ! _NoMoreItems()  ) {
  671.     return ();
  672.     }
  673.     $self->{VALUES}= \@names;
  674.     return 1;
  675. }
  676.  
  677.  
  678. sub ValueNames
  679. {
  680.     my $self= shift(@_);
  681.     $self= tied(%$self)   if  tied(%$self);
  682.     @_  and  croak "Usage:  \@names= \$key->ValueNames;";
  683.     $self->_enumValues   unless  $self->{VALUES};
  684.     return @{$self->{VALUES}};
  685. }
  686.  
  687.  
  688. sub _enumSubKeys
  689. {
  690.     my $self= shift(@_);
  691.     $self= tied(%$self)   if  tied(%$self);
  692.     my( @subkeys, @classes, @times )= ();
  693.     my $pos= 0;
  694.     my( $subkey, $class, $time )= ("","","");
  695.     my( $namSiz, $clsSiz )= $self->Information(
  696.                   qw( MaxSubKeyLen MaxSubClassLen ));
  697.     $namSiz++;  $clsSiz++;
  698.     while(  $self->RegEnumKeyEx(
  699.           $pos++, $subkey, $namSiz, [], $class, $clsSiz, $time )  ) {
  700.     push( @subkeys, $subkey );
  701.     push( @classes, $class );
  702.     push( @times, $time );
  703.     }
  704.     if(  ! _NoMoreItems()  ) {
  705.     return ();
  706.     }
  707.     $self->{SUBKEYS}= \@subkeys;
  708.     $self->{SUBCLASSES}= \@classes;
  709.     $self->{SUBTIMES}= \@times;
  710.     return 1;
  711. }
  712.  
  713.  
  714. sub SubKeyNames
  715. {
  716.     my $self= shift(@_);
  717.     $self= tied(%$self)   if  tied(%$self);
  718.     @_  and  croak "Usage:  \@names= \$key->SubKeyNames;";
  719.     $self->_enumSubKeys   unless  $self->{SUBKEYS};
  720.     return @{$self->{SUBKEYS}};
  721. }
  722.  
  723.  
  724. sub SubKeyClasses
  725. {
  726.     my $self= shift(@_);
  727.     @_  and  croak "Usage:  \@classes= \$key->SubKeyClasses;";
  728.     $self->_enumSubKeys   unless  $self->{SUBCLASSES};
  729.     return @{$self->{SUBCLASSES}};
  730. }
  731.  
  732.  
  733. sub SubKeyTimes
  734. {
  735.     my $self= shift(@_);
  736.     $self= tied(%$self)   if  tied(%$self);
  737.     @_  and  croak "Usage:  \@times= \$key->SubKeyTimes;";
  738.     $self->_enumSubKeys   unless  $self->{SUBTIMES};
  739.     return @{$self->{SUBTIMES}};
  740. }
  741.  
  742.  
  743. sub _MemberNames
  744. {
  745.     my $self= shift(@_);
  746.     $self= tied(%$self)   if  tied(%$self);
  747.     @_  and  croak "Usage:  \$arrayRef= \$key->_MemberNames;";
  748.     if(  ! $self->{MEMBERS}  ) {
  749.     $self->_enumValues   unless  $self->{VALUES};
  750.     $self->_enumSubKeys   unless  $self->{SUBKEYS};
  751.     my( @members )= (  map( $_.$self->{DELIM}, @{$self->{SUBKEYS}} ),
  752.                map( $self->{DELIM}.$_, @{$self->{VALUES}} )  );
  753.     $self->{MEMBERS}= \@members;
  754.     }
  755.     return $self->{MEMBERS};
  756. }
  757.  
  758.  
  759. sub _MembersHash
  760. {
  761.     my $self= shift(@_);
  762.     $self= tied(%$self)   if  tied(%$self);
  763.     @_  and  croak "Usage:  \$hashRef= \$key->_MembersHash;";
  764.     if(  ! $self->{MEMBHASH}  ) {
  765.     my $aRef= $self->_MemberNames;
  766.     $self->{MEMBHASH}= {};
  767.     @{$self->{MEMBHASH}}{@$aRef}= (1) x @$aRef;
  768.     }
  769.     return $self->{MEMBHASH};
  770. }
  771.  
  772.  
  773. sub MemberNames
  774. {
  775.     my $self= shift(@_);
  776.     $self= tied(%$self)   if  tied(%$self);
  777.     @_  and  croak "Usage:  \@members= \$key->MemberNames;";
  778.     return @{$self->_MemberNames};
  779. }
  780.  
  781.  
  782. sub Information
  783. {
  784.     my $self= shift(@_);
  785.     $self= tied(%$self)   if  tied(%$self);
  786.     my( $time, $nkeys, $nvals, $xsec, $xkey, $xcls, $xname, $xdata )=
  787.     ("",0,0,0,0,0,0,0);
  788.     my $clen= 8;
  789.     if(  ! $self->RegQueryInfoKey( [], [], $nkeys, $xkey, $xcls,
  790.                    $nvals, $xname, $xdata, $xsec, $time )  ) {
  791.     return ();
  792.     }
  793.     if(  defined($self->{Class})  ) {
  794.     $clen= length($self->{Class});
  795.     } else {
  796.     $self->{Class}= "";
  797.     }
  798.     while(  ! $self->RegQueryInfoKey( $self->{Class}, $clen,
  799.                       [],[],[],[],[],[],[],[],[])
  800.         &&  _MoreData  ) {
  801.     $clen *= 2;
  802.     }
  803.     my( %info );
  804.     @info{ qw( LastWrite CntSubKeys CntValues SecurityLen
  805.            MaxValDataLen MaxSubKeyLen MaxSubClassLen MaxValNameLen )
  806.     }=       ( $time,    $nkeys,    $nvals,   $xsec,
  807.                $xdata,       $xkey,       $xcls,         $xname );
  808.     if(  @_  ) {
  809.     my( %check );
  810.     @check{keys(%info)}= keys(%info);
  811.     my( @err )= grep( ! $check{$_}, @_ );
  812.     if(  @err  ) {
  813.         croak "${PACK}::Information- Invalid info requested (@err)";
  814.     }
  815.     return @info{@_};
  816.     } else {
  817.     return %info;
  818.     }
  819. }
  820.  
  821.  
  822. sub Delimiter
  823. {
  824.     my $self= shift(@_);
  825.     $self= tied(%$self)   if  tied(%$self);
  826.     $self= $RegObj   unless  ref($self);
  827.     my( $oldDelim )= $self->{DELIM};
  828.     if(  1 == @_  &&  "" ne "$_[0]"  ) {
  829.     delete $self->{MEMBERS};
  830.     delete $self->{MEMBHASH};
  831.     $self->{DELIM}= "$_[0]";
  832.     } elsif(  0 != @_  ) {
  833.     croak "Usage:  \$oldDelim= \$key->Delimiter(\$newDelim);";
  834.     }
  835.     return $oldDelim;
  836. }
  837.  
  838.  
  839. sub Handle
  840. {
  841.     my $self= shift(@_);
  842.     $self= tied(%$self)   if  tied(%$self);
  843.     @_  and  croak "Usage:  \$handle= \$key->Handle;";
  844.     $self= $RegObj   unless  ref($self);
  845.     return $self->{HANDLE};
  846. }
  847.  
  848.  
  849. sub Path
  850. {
  851.     my $self= shift(@_);
  852.     $self= tied(%$self)   if  tied(%$self);
  853.     @_  and  croak "Usage:  \$path= \$key->Path;";
  854.     my $delim= $self->{DELIM};
  855.     $self= $RegObj   unless  ref($self);
  856.     if(  "" eq $self->{MACHINE}  ) {
  857.     return(  $delim . join( $delim, @{$self->{PATH}} ) . $delim  );
  858.     } else {
  859.     return(  $delim x 2
  860.       . join( $delim, $self->{MACHINE}, @{$self->{PATH}} )
  861.       . $delim  );
  862.     }
  863. }
  864.  
  865.  
  866. sub _Path
  867. {
  868.     my $self= shift(@_);
  869.     $self= tied(%$self)   if  tied(%$self);
  870.     @_  and  croak "Usage:  \$arrRef= \$key->_Path;";
  871.     $self= $RegObj   unless  ref($self);
  872.     return $self->{PATH};
  873. }
  874.  
  875.  
  876. sub Machine
  877. {
  878.     my $self= shift(@_);
  879.     $self= tied(%$self)   if  tied(%$self);
  880.     @_  and  croak "Usage:  \$machine= \$key->Machine;";
  881.     $self= $RegObj   unless  ref($self);
  882.     return $self->{MACHINE};
  883. }
  884.  
  885.  
  886. sub Access
  887. {
  888.     my $self= shift(@_);
  889.     $self= tied(%$self)   if  tied(%$self);
  890.     @_  and  croak "Usage:  \$access= \$key->Access;";
  891.     $self= $RegObj   unless  ref($self);
  892.     return $self->{ACCESS};
  893. }
  894.  
  895.  
  896. sub OS_Delimiter
  897. {
  898.     my $self= shift(@_);
  899.     @_  and  croak "Usage:  \$backslash= \$key->OS_Delimiter;";
  900.     return $self->{OS_DELIM};
  901. }
  902.  
  903.  
  904. sub _Roots
  905. {
  906.     my $self= shift(@_);
  907.     $self= tied(%$self)   if  ref($self)  &&  tied(%$self);
  908.     @_  and  croak "Usage:  \$varName= \$key->_Roots;";
  909.     $self= $RegObj   unless  ref($self);
  910.     return $self->{ROOTS};
  911. }
  912.  
  913.  
  914. sub Roots
  915. {
  916.     my $self= shift(@_);
  917.     $self= tied(%$self)   if  ref($self)  &&  tied(%$self);
  918.     @_  and  croak "Usage:  \$hashRef= \$key->Roots;";
  919.     $self= $RegObj   unless  ref($self);
  920.     return eval "\\%$self->{ROOTS}";
  921. }
  922.  
  923.  
  924. sub TIEHASH
  925. {
  926.     my( $this )= shift(@_);
  927.     $this= tied(%$this)   if  ref($this)  &&  tied(%$this);
  928.     my( $key )= @_;
  929.     if(  1 == @_  &&  ref($key)  &&  "$key" =~ /=/  ) {
  930.     return $key;    # $key is already an object (blessed reference).
  931.     }
  932.     return $this->new( @_ );
  933. }
  934.  
  935.  
  936. sub Tie
  937. {
  938.     my $self= shift(@_);
  939.     $self= tied(%$self)   if  tied(%$self);
  940.     my( $hRef )= @_;
  941.     if(  1 != @_  ||  ! ref($hRef)  ||  "$hRef" !~ /(^|=)HASH\(/  ) {
  942.     croak "Usage: \$key->Tie(\\\%hash);";
  943.     }
  944.     return  tie %$hRef, ref($self), $self;
  945. }
  946.  
  947.  
  948. sub TiedRef
  949. {
  950.     my $self= shift(@_);
  951.     $self= tied(%$self)   if  tied(%$self);
  952.     my $hRef= @_ ? shift(@_) : {};
  953.     return ()   if  ! defined($self);
  954.     $self->Tie($hRef);
  955.     bless $hRef, ref($self);
  956.     return $hRef;
  957. }
  958.  
  959.  
  960. sub _Flags
  961. {
  962.     my $self= shift(@_);
  963.     $self= tied(%$self)   if  tied(%$self);
  964.     my $oldFlags= $self->{FLAGS};
  965.     if(  1 == @_  ) {
  966.     $self->{FLAGS}= shift(@_);
  967.     } elsif(  0 != @_  ) {
  968.     croak "Usage:  \$oldBits= \$key->_Flags(\$newBits);";
  969.     }
  970.     return $oldFlags;
  971. }
  972.  
  973.  
  974. sub ArrayValues
  975. {
  976.     my $self= shift(@_);
  977.     $self= tied(%$self)   if  tied(%$self);
  978.     my $oldFlag= $Flag_ArrVal == ( $Flag_ArrVal & $self->{FLAGS} );
  979.     if(  1 == @_  ) {
  980.     my $bool= shift(@_);
  981.     if(  $bool  ) {
  982.         $self->{FLAGS} |= $Flag_ArrVal;
  983.     } else {
  984.         $self->{FLAGS} &= ~( $Flag_ArrVal | $Flag_TieVal );
  985.     }
  986.     } elsif(  0 != @_  ) {
  987.     croak "Usage:  \$oldBool= \$key->ArrayValues(\$newBool);";
  988.     }
  989.     return $oldFlag;
  990. }
  991.  
  992.  
  993. sub TieValues
  994. {
  995.     my $self= shift(@_);
  996.     $self= tied(%$self)   if  tied(%$self);
  997.     my $oldFlag= $Flag_TieVal == ( $Flag_TieVal & $self->{FLAGS} );
  998.     if(  1 == @_  ) {
  999.     my $bool= shift(@_);
  1000.     if(  $bool  ) {
  1001.         croak "${PACK}->TieValues cannot be enabled with this version";
  1002.         $self->{FLAGS} |= $Flag_TieVal;
  1003.     } else {
  1004.         $self->{FLAGS} &= ~$Flag_TieVal;
  1005.     }
  1006.     } elsif(  0 != @_  ) {
  1007.     croak "Usage:  \$oldBool= \$key->TieValues(\$newBool);";
  1008.     }
  1009.     return $oldFlag;
  1010. }
  1011.  
  1012.  
  1013. sub FastDelete
  1014. {
  1015.     my $self= shift(@_);
  1016.     $self= tied(%$self)   if  tied(%$self);
  1017.     my $oldFlag= $Flag_FastDel == ( $Flag_FastDel & $self->{FLAGS} );
  1018.     if(  1 == @_  ) {
  1019.     my $bool= shift(@_);
  1020.     if(  $bool  ) {
  1021.         $self->{FLAGS} |= $Flag_FastDel;
  1022.     } else {
  1023.         $self->{FLAGS} &= ~$Flag_FastDel;
  1024.     }
  1025.     } elsif(  0 != @_  ) {
  1026.     croak "Usage:  \$oldBool= \$key->FastDelete(\$newBool);";
  1027.     }
  1028.     return $oldFlag;
  1029. }
  1030.  
  1031.  
  1032. sub SplitMultis
  1033. {
  1034.     my $self= shift(@_);
  1035.     $self= tied(%$self)   if  tied(%$self);
  1036.     my $oldFlag= $Flag_Split == ( $Flag_Split & $self->{FLAGS} );
  1037.     if(  1 == @_  ) {
  1038.     my $bool= shift(@_);
  1039.     if(  $bool  ) {
  1040.         $self->{FLAGS} |= $Flag_Split;
  1041.     } else {
  1042.         $self->{FLAGS} &= ~$Flag_Split;
  1043.     }
  1044.     } elsif(  0 != @_  ) {
  1045.     croak "Usage:  \$oldBool= \$key->SplitMultis(\$newBool);";
  1046.     }
  1047.     return $oldFlag;
  1048. }
  1049.  
  1050.  
  1051. sub DWordsToHex
  1052. {
  1053.     my $self= shift(@_);
  1054.     $self= tied(%$self)   if  tied(%$self);
  1055.     my $oldFlag= $Flag_HexDWord == ( $Flag_HexDWord & $self->{FLAGS} );
  1056.     if(  1 == @_  ) {
  1057.     my $bool= shift(@_);
  1058.     if(  $bool  ) {
  1059.         $self->{FLAGS} |= $Flag_HexDWord;
  1060.     } else {
  1061.         $self->{FLAGS} &= ~$Flag_HexDWord;
  1062.     }
  1063.     } elsif(  0 != @_  ) {
  1064.     croak "Usage:  \$oldBool= \$key->DWordsToHex(\$newBool);";
  1065.     }
  1066.     return $oldFlag;
  1067. }
  1068.  
  1069.  
  1070. sub FixSzNulls
  1071. {
  1072.     my $self= shift(@_);
  1073.     $self= tied(%$self)   if  tied(%$self);
  1074.     my $oldFlag= $Flag_FixNulls == ( $Flag_FixNulls & $self->{FLAGS} );
  1075.     if(  1 == @_  ) {
  1076.     my $bool= shift(@_);
  1077.     if(  $bool  ) {
  1078.         $self->{FLAGS} |= $Flag_FixNulls;
  1079.     } else {
  1080.         $self->{FLAGS} &= ~$Flag_FixNulls;
  1081.     }
  1082.     } elsif(  0 != @_  ) {
  1083.     croak "Usage:  \$oldBool= \$key->FixSzNulls(\$newBool);";
  1084.     }
  1085.     return $oldFlag;
  1086. }
  1087.  
  1088.  
  1089. sub DualTypes
  1090. {
  1091.     my $self= shift(@_);
  1092.     $self= tied(%$self)   if  tied(%$self);
  1093.     my $oldFlag= $Flag_DualTyp == ( $Flag_DualTyp & $self->{FLAGS} );
  1094.     if(  1 == @_  ) {
  1095.     my $bool= shift(@_);
  1096.     if(  $bool  ) {
  1097.         croak "${PACK}->DualTypes cannot be enabled since ",
  1098.           "SetDualVar module not installed"
  1099.           unless  $_SetDualVar;
  1100.         $self->{FLAGS} |= $Flag_DualTyp;
  1101.     } else {
  1102.         $self->{FLAGS} &= ~$Flag_DualTyp;
  1103.     }
  1104.     } elsif(  0 != @_  ) {
  1105.     croak "Usage:  \$oldBool= \$key->DualTypes(\$newBool);";
  1106.     }
  1107.     return $oldFlag;
  1108. }
  1109.  
  1110.  
  1111. sub DualBinVals
  1112. {
  1113.     my $self= shift(@_);
  1114.     $self= tied(%$self)   if  tied(%$self);
  1115.     my $oldFlag= $Flag_DualBin == ( $Flag_DualBin & $self->{FLAGS} );
  1116.     if(  1 == @_  ) {
  1117.     my $bool= shift(@_);
  1118.     if(  $bool  ) {
  1119.         croak "${PACK}->DualBinVals cannot be enabled since ",
  1120.           "SetDualVar module not installed"
  1121.           unless  $_SetDualVar;
  1122.         $self->{FLAGS} |= $Flag_DualBin;
  1123.     } else {
  1124.         $self->{FLAGS} &= ~$Flag_DualBin;
  1125.     }
  1126.     } elsif(  0 != @_  ) {
  1127.     croak "Usage:  \$oldBool= \$key->DualBinVals(\$newBool);";
  1128.     }
  1129.     return $oldFlag;
  1130. }
  1131.  
  1132.  
  1133. sub GetOptions
  1134. {
  1135.     my $self= shift(@_);
  1136.     $self= tied(%$self)   if  tied(%$self);
  1137.     my( $opt, $meth );
  1138.     if(  ! @_  ||  1 == @_  &&  "HASH" eq ref($_[0])  ) {
  1139.     my $href= @_ ? $_[0] : {};
  1140.     foreach $opt (  grep !/^Allow/, @_opt_subs  ) {
  1141.         $meth= $_opt_subs{$opt};
  1142.         $href->{$opt}=  $self->$meth();
  1143.     }
  1144.     return @_ ? $self : $href;
  1145.     }
  1146.     my @old;
  1147.     foreach $opt (  @_  ) {
  1148.     $meth= $_opt_subs{$opt};
  1149.     if(  defined $meth  ) {
  1150.         if(  $opt eq "AllowLoad"  ||  $opt eq "AllowSave"  ) {
  1151.         croak "${PACK}->GetOptions:  Getting current setting of $opt ",
  1152.               "not supported in this release";
  1153.         }
  1154.         push(  @old,  $self->$meth()  );
  1155.     } else {
  1156.         croak "${PACK}->GetOptions:  Invalid option ($opt) ",
  1157.           "not one of ( ", join(" ",grep !/^Allow/, @_opt_subs), " )";
  1158.     }
  1159.     }
  1160.     return wantarray ? @old : $old[-1];
  1161. }
  1162.  
  1163.  
  1164. sub SetOptions
  1165. {
  1166.     my $self= shift(@_);
  1167.     # Don't get object if hash ref so "ref" returns original ref.
  1168.     my( $opt, $meth, @old );
  1169.     while(  @_  ) {
  1170.     $opt= shift(@_);
  1171.     $meth= $_opt_subs{$opt};
  1172.     if(  ! @_  ) {
  1173.         croak "${PACK}->SetOptions:  Option value missing ",
  1174.           "after option name ($opt)";
  1175.     } elsif(  defined $meth  ) {
  1176.         push(  @old,  $self->$meth( shift(@_) )  );
  1177.     } elsif(  $opt eq substr("reference",0,length($opt))  ) {
  1178.         shift(@_)   if  @_;
  1179.         push(  @old,  $self  );
  1180.     } else {
  1181.         croak "${PACK}->SetOptions:  Invalid option ($opt) ",
  1182.           "not one of ( @_opt_subs )";
  1183.     }
  1184.     }
  1185.     return wantarray ? @old : $old[-1];
  1186. }
  1187.  
  1188.  
  1189. sub _parseTiedEnt
  1190. {
  1191.     my $self= shift(@_);
  1192.     $self= tied(%$self)   if  tied(%$self);
  1193.     my $ent= shift(@_);
  1194.     my $delim= shift(@_);
  1195.     my $dlen= length( $delim );
  1196.     my $parent= @_ ? shift(@_) : 0;
  1197.     my $off;
  1198.     if(  $delim x 2 eq substr($ent,0,2*$dlen)  &&  "NONE" eq $self->Handle  ) {
  1199.     if(  0 <= ( $off= index( $ent, $delim x 2, 2*$dlen ) )  ) {
  1200.         return(  substr( $ent, 0, $off ),  substr( $ent, 2*$dlen+$off )  );
  1201.     } elsif(  $delim eq substr($ent,-$dlen)  ) {
  1202.         return( substr($ent,0,-$dlen) );
  1203.     } elsif(  2*$dlen <= ( $off= rindex( $ent, $delim ) )  ) {
  1204.         return(  substr( $ent, 0, $off ),
  1205.           undef,  substr( $ent, $dlen+$off )  );
  1206.     } elsif(  $parent  ) {
  1207.         return();
  1208.     } else {
  1209.         return( $ent );
  1210.     }
  1211.     } elsif(  $delim eq substr($ent,0,$dlen)  &&  "NONE" ne $self->Handle  ) {
  1212.     return( undef, substr($ent,$dlen) );
  1213.     } elsif(  $self->{MEMBERS}  &&  $self->_MembersHash->{$ent}  ) {
  1214.     return( substr($ent,0,-$dlen) );
  1215.     } elsif(  0 <= ( $off= index( $ent, $delim x 2 ) )  ) {
  1216.     return(  substr( $ent, 0, $off ),  substr( $ent, 2*$dlen+$off ) );
  1217.     } elsif(  $delim eq substr($ent,-$dlen)  ) {
  1218.     if(  $parent
  1219.      &&  0 <= ( $off= rindex( $ent, $delim, length($ent)-2*$dlen ) )  ) {
  1220.         return(  substr($ent,0,$off),
  1221.           undef,  undef,  substr($ent,$dlen+$off,-$dlen)  );
  1222.     } else {
  1223.         return( substr($ent,0,-$dlen) );
  1224.     }
  1225.     } elsif(  0 <= ( $off= rindex( $ent, $delim ) )  ) {
  1226.     return(
  1227.       substr( $ent, 0, $off ),  undef,  substr( $ent, $dlen+$off )  );
  1228.     } else {
  1229.     return( undef, undef, $ent );
  1230.     }
  1231. }
  1232.  
  1233.  
  1234. sub _FetchValue
  1235. {
  1236.     my $self= shift( @_ );
  1237.     my( $val, $createKey )= @_;
  1238.     my( $data, $type );
  1239.     if(  ( $data, $type )= $self->GetValue( $val )  ) {
  1240.     return $self->ArrayValues ? [ $data, $type ]
  1241.            : wantarray        ? ( $data, $type )
  1242.                   : $data;
  1243.     } elsif(  $createKey  and  $data= $self->new($val)  ) {
  1244.     return $data->TiedRef;
  1245.     } else {
  1246.     return ();
  1247.     }
  1248. }
  1249.  
  1250.  
  1251. sub FETCH
  1252. {
  1253.     my $self= shift(@_);
  1254.     my $ent= shift(@_);
  1255.     my $delim= $self->Delimiter;
  1256.     my( $key, $val, $ambig )= $self->_parseTiedEnt( $ent, $delim, 0 );
  1257.     my $sub;
  1258.     if(  defined($key)  ) {
  1259.     if(  defined($self->{MEMBHASH})
  1260.      &&  $self->{MEMBHASH}->{$key.$delim}
  1261.      &&  0 <= index($key,$delim)  ) {
  1262.         return ()
  1263.           unless  $sub= $self->new( $key,
  1264.                   {"Delimiter"=>$self->OS_Delimiter} );
  1265.         $sub->Delimiter($delim);
  1266.     } else {
  1267.         return ()
  1268.           unless  $sub= $self->new( $key );
  1269.     }
  1270.     } else {
  1271.     $sub= $self;
  1272.     }
  1273.     if(  defined($val)  ) {
  1274.     return $sub->_FetchValue( $val );
  1275.     } elsif(  ! defined($ambig)  ) {
  1276.     return $sub->TiedRef;
  1277.     } elsif(  defined($key)  ) {
  1278.     return $sub->FETCH(  $ambig  );
  1279.     } else {
  1280.     return $sub->_FetchValue( $ambig, "" ne $ambig );
  1281.     }
  1282. }
  1283.  
  1284.  
  1285. sub _FetchOld
  1286. {
  1287.     my( $self, $key )= @_;
  1288.     my $old= $self->FETCH($key);
  1289.     if(  $old  ) {
  1290.     my $copy= {};
  1291.     %$copy= %$old;
  1292.     return $copy;
  1293.     }
  1294.     # return $^E;
  1295.     return _Err;
  1296. }
  1297.  
  1298.  
  1299. sub DELETE
  1300. {
  1301.     my $self= shift(@_);
  1302.     my $ent= shift(@_);
  1303.     my $delim= $self->Delimiter;
  1304.     my( $key, $val, $ambig, $subkey )= $self->_parseTiedEnt( $ent, $delim, 1 );
  1305.     my $sub;
  1306.     my $fast= defined(wantarray) ? $self->FastDelete : 2;
  1307.     my $old= 1;    # Value returned if FastDelete is set.
  1308.     if(  defined($key)
  1309.      &&  ( defined($val) || defined($ambig) || defined($subkey) )  ) {
  1310.     return ()
  1311.       unless  $sub= $self->new( $key );
  1312.     } else {
  1313.     $sub= $self;
  1314.     }
  1315.     if(  defined($val)  ) {
  1316.     $old= $sub->GetValue($val) || _Err   unless  2 <= $fast;
  1317.     $sub->RegDeleteValue( $val );
  1318.     } elsif(  defined($subkey)  ) {
  1319.     $old= $sub->_FetchOld( $subkey.$delim )   unless  $fast;
  1320.     $sub->RegDeleteKey( $subkey );
  1321.     } elsif(  defined($ambig)  ) {
  1322.     if(  defined($key)  ) {
  1323.         $old= $sub->DELETE($ambig);
  1324.     } else {
  1325.         $old= $sub->GetValue($ambig) || _Err   unless  2 <= $fast;
  1326.         if(  defined( $old )  ) {
  1327.         $sub->RegDeleteValue( $ambig );
  1328.         } else {
  1329.         $old= $sub->_FetchOld( $ambig.$delim )   unless  $fast;
  1330.         $sub->RegDeleteKey( $ambig );
  1331.         }
  1332.     }
  1333.     } elsif(  defined($key)  ) {
  1334.     $old= $sub->_FetchOld( $key.$delim )   unless  $fast;
  1335.     $sub->RegDeleteKey( $key );
  1336.     } else {
  1337.     croak "${PACK}->DELETE:  Key ($ent) can never be deleted";
  1338.     }
  1339.     return $old;
  1340. }
  1341.  
  1342.  
  1343. sub SetValue
  1344. {
  1345.     my $self= shift(@_);
  1346.     $self= tied(%$self)   if  tied(%$self);
  1347.     my $name= shift(@_);
  1348.     my $data= shift(@_);
  1349.     my( $type )= @_;
  1350.     my $size;
  1351.     if(  ! defined($type)  ) {
  1352.     if(  "ARRAY" eq ref($data)  ) {
  1353.         croak "${PACK}->SetValue:  Value is array reference but ",
  1354.           "no data type given"
  1355.           unless  2 == @$data;
  1356.         ( $data, $type )= @$data;
  1357.     } else {
  1358.         $type= REG_SZ;
  1359.     }
  1360.     }
  1361.     $type= _constant($type,"registry value data type")   if  $type =~ /^REG_/;
  1362.     if(  REG_MULTI_SZ == $type  &&  "ARRAY" eq ref($data)  ) {
  1363.     $data= join( "\0", @$data ) . "\0\0";
  1364.     ## $data= pack(  "a*" x (1+@$data),  map( $_."\0", @$data, "" )  );
  1365.     } elsif(  ( REG_SZ == $type || REG_EXPAND_SZ == $type )
  1366.           &&  $self->FixSzNulls  ) {
  1367.     $data .= "\0"    unless  "\0" eq substr($data,0,-1);
  1368.     } elsif(  REG_DWORD == $type  &&  $data =~ /^0x[0-9a-fA-F]{3,}$/  ) {
  1369.     $data= pack( "L", hex($data) );
  1370.     # We could to $data=pack("L",$data) for REG_DWORD but I see
  1371.     # no nice way to always destinguish when to do this or not.
  1372.     }
  1373.     return $self->RegSetValueEx( $name, 0, $type, $data, length($data) );
  1374. }
  1375.  
  1376.  
  1377. sub StoreKey
  1378. {
  1379.     my $this= shift(@_);
  1380.     $this= tied(%$this)   if  ref($this)  &&  tied(%$this);
  1381.     my $subKey= shift(@_);
  1382.     my $data= shift(@_);
  1383.     my $ent;
  1384.     my $self;
  1385.     if(  ! ref($data)  ||  "$data" !~ /(^|=)HASH/  ) {
  1386.     croak "${PACK}->StoreKey:  For ", $this->Path.$subKey, ",\n",
  1387.           "  subkey data must be a HASH reference";
  1388.     }
  1389.     if(  defined( $$data{""} )  &&  "HASH" eq ref($$data{""})  ) {
  1390.     $self= $this->CreateKey( $subKey, delete $$data{""} );
  1391.     } else {
  1392.     $self= $this->CreateKey( $subKey );
  1393.     }
  1394.     return ()   if  ! defined($self);
  1395.     foreach $ent (  keys(%$data)  ) {
  1396.     return ()
  1397.       unless  $self->STORE( $ent, $$data{$ent} );
  1398.     }
  1399.     return $self;
  1400. }
  1401.  
  1402.  
  1403. # = { "" => {OPT=>VAL}, "val"=>[], "key"=>{} } creates a new key
  1404. # = "string" creates a new REG_SZ value
  1405. # = [ data, type ] creates a new value
  1406. sub STORE
  1407. {
  1408.     my $self= shift(@_);
  1409.     my $ent= shift(@_);
  1410.     my $data= shift(@_);
  1411.     my $delim= $self->Delimiter;
  1412.     my( $key, $val, $ambig, $subkey )= $self->_parseTiedEnt( $ent, $delim, 1 );
  1413.     my $sub;
  1414.     if(  defined($key)
  1415.      &&  ( defined($val) || defined($ambig) || defined($subkey) )  ) {
  1416.     return ()
  1417.       unless  $sub= $self->new( $key );
  1418.     } else {
  1419.     $sub= $self;
  1420.     }
  1421.     if(  defined($val)  ) {
  1422.     croak "${PACK}->STORE:  For ", $sub->Path.$delim.$val, ",\n",
  1423.           "  value data cannot be a HASH reference"
  1424.       if  ref($data)  &&  "$data" =~ /(^|=)HASH/;
  1425.     $sub->SetValue( $val, $data );
  1426.     } elsif(  defined($subkey)  ) {
  1427.     croak "${PACK}->STORE:  For ", $sub->Path.$subkey.$delim, ",\n",
  1428.           "  subkey data must be a HASH reference"
  1429.       unless  ref($data)  &&  "$data" =~ /(^|=)HASH/;
  1430.     $sub->StoreKey( $subkey, $data );
  1431.     } elsif(  defined($ambig)  ) {
  1432.     if(  ref($data)  &&  "$data" =~ /(^|=)HASH/  ) {
  1433.         $sub->StoreKey( $ambig, $data );
  1434.     } else {
  1435.         $sub->SetValue( $ambig, $data );
  1436.     }
  1437.     } elsif(  defined($key)  ) {
  1438.     croak "${PACK}->STORE:  For ", $sub->Path.$key.$delim, ",\n",
  1439.           "  subkey data must be a HASH reference"
  1440.       unless  ref($data)  &&  "$data" =~ /(^|=)HASH/;
  1441.     $sub->StoreKey( $key, $data );
  1442.     } else {
  1443.     croak "${PACK}->STORE:  Key ($ent) can never be created nor set";
  1444.     }
  1445. }
  1446.  
  1447.  
  1448. sub EXISTS
  1449. {
  1450.     my $self= shift(@_);
  1451.     my $ent= shift(@_);
  1452.     return defined( $self->FETCH($ent) );
  1453. }
  1454.  
  1455.  
  1456. sub FIRSTKEY
  1457. {
  1458.     my $self= shift(@_);
  1459.     my $members= $self->_MemberNames;
  1460.     $self->{PREVIDX}= 0;
  1461.     return @{$members} ? $members->[0] : undef;
  1462. }
  1463.  
  1464.  
  1465. sub NEXTKEY
  1466. {
  1467.     my $self= shift(@_);
  1468.     my $prev= shift(@_);
  1469.     my $idx= $self->{PREVIDX};
  1470.     my $members= $self->_MemberNames;
  1471.     if(  ! defined($idx)  ||  $prev ne $members->[$idx]  ) {
  1472.     $idx= 0;
  1473.     while(  $idx < @$members  &&  $prev ne $members->[$idx]  ) {
  1474.         $idx++;
  1475.     }
  1476.     }
  1477.     $self->{PREVIDX}= ++$idx;
  1478.     return $members->[$idx];
  1479. }
  1480.  
  1481.  
  1482. sub DESTROY
  1483. {
  1484.     my $self= shift(@_);
  1485.     return   if  tied(%$self);
  1486.     my $unload;
  1487.     eval { $unload= $self->{UNLOADME}; 1 }
  1488.     or  return;
  1489.     my $debug= $ENV{DEBUG_TIE_REGISTRY};
  1490.     if(  defined($debug)  ) {
  1491.     if(  1 < $debug  ) {
  1492.         my $hand= $self->Handle;
  1493.         my $dep= $self->{DEPENDON};
  1494.         carp "${PACK} destroying ", $self->Path, " (",
  1495.          "NONE" eq $hand ? $hand : sprintf("0x%lX",$hand), ")",
  1496.          defined($dep) ? (" [depends on ",$dep->Path,"]") : ();
  1497.     } else {
  1498.         warn "${PACK} destroying ", $self->Path, ".\n";
  1499.     }
  1500.     }
  1501.     $self->RegCloseKey
  1502.       unless  "NONE" eq $self->Handle;
  1503.     if(  defined($unload)  ) {
  1504.     if(  defined($debug)  &&  1 < $debug  ) {
  1505.         my( $obj, $subKey, $file )= @$unload;
  1506.         warn "Unloading ", $self->Path,
  1507.           " (from ", $obj->Path, ", $subKey)...\n";
  1508.     }
  1509.     $self->UnLoad
  1510.       ||  warn "Couldn't unload ", $self->Path, ": ", _ErrMsg, "\n";
  1511.     ## carp "Never unloaded ${PACK}::Load($$unload[2])";
  1512.     }
  1513.     #delete $self->{DEPENDON};
  1514. }
  1515.  
  1516.  
  1517. use vars qw( @CreateKey_Opts %CreateKey_Opts %_KeyDispNames );
  1518. @CreateKey_Opts= qw( Access Class Options Delimiter
  1519.              Disposition Security Volatile Backup );
  1520. @CreateKey_Opts{@CreateKey_Opts}= (1) x @CreateKey_Opts;
  1521. %_KeyDispNames= ( REG_CREATED_NEW_KEY() => "REG_CREATED_NEW_KEY",
  1522.           REG_OPENED_EXISTING_KEY() => "REG_OPENED_EXISTING_KEY" );
  1523.  
  1524. sub CreateKey
  1525. {
  1526.     my $self= shift(@_);
  1527.     my $tied= tied(%$self);
  1528.     $self= tied(%$self)   if  $tied;
  1529.     my( $subKey, $opts )= @_;
  1530.     my( $sam )= $self->Access;
  1531.     my( $delim )= $self->Delimiter;
  1532.     my( $class )= "";
  1533.     my( $flags )= 0;
  1534.     my( $secure )= [];
  1535.     my( $garb )= [];
  1536.     my( $result )= \$garb;
  1537.     my( $handle )= 0;
  1538.     if(  @_ < 1  ||  2 < @_
  1539.      ||  2 == @_ && "HASH" ne ref($opts)  ) {
  1540.     croak "Usage:  \$new= \$old->CreateKey( \$subKey, {OPT=>VAL,...} );\n",
  1541.           "  options: @CreateKey_Opts\nCalled";
  1542.     }
  1543.     if(  defined($opts)  ) {
  1544.     $sam= $opts->{"Access"}   if  defined($opts->{"Access"});
  1545.     $class= $opts->{Class}   if  defined($opts->{Class});
  1546.     $flags= $opts->{Options}   if  defined($opts->{Options});
  1547.     $delim= $opts->{"Delimiter"}   if  defined($opts->{"Delimiter"});
  1548.     $secure= $opts->{Security}   if  defined($opts->{Security});
  1549.     if(  defined($opts->{Disposition})  ) {
  1550.         "SCALAR" eq ref($opts->{Disposition})
  1551.           or  croak "${PACK}->CreateKey option `Disposition'",
  1552.             " must provide a scalar reference";
  1553.         $result= $opts->{Disposition};
  1554.     }
  1555.     if(  0 == $flags  ) {
  1556.         $flags |= REG_OPTION_VOLATILE
  1557.           if  defined($opts->{Volatile})  &&  $opts->{Volatile};
  1558.         $flags |= REG_OPTION_BACKUP_RESTORE
  1559.           if  defined($opts->{Backup})  &&  $opts->{Backup};
  1560.     }
  1561.     }
  1562.     my $subPath= ref($subKey) ? $subKey : $self->_split($subKey,$delim);
  1563.     $subKey= join( $self->OS_Delimiter, @$subPath );
  1564.     $self->RegCreateKeyEx( $subKey, 0, $class, $flags, $sam,
  1565.                $secure, $handle, $$result )
  1566.       or  return ();
  1567.     if(  ! ref($$result)  &&  $self->DualTypes  ) {
  1568.     $$result= _DualVal( \%_KeyDispNames, $$result );
  1569.     }
  1570.     my $new= $self->_new( $handle, [ @{$self->_Path}, @{$subPath} ] );
  1571.     $new->{ACCESS}= $sam;
  1572.     $new->{DELIM}= $delim;
  1573.     $new= $new->TiedRef   if  $tied;
  1574.     return $new;
  1575. }
  1576.  
  1577.  
  1578. use vars qw( $Load_Cnt @Load_Opts %Load_Opts );
  1579. $Load_Cnt= 0;
  1580. @Load_Opts= qw(NewSubKey);
  1581. @Load_Opts{@Load_Opts}= (1) x @Load_Opts;
  1582.  
  1583. sub Load
  1584. {
  1585.     my $this= shift(@_);
  1586.     my $tied=  ref($this)  &&  tied(%$this);
  1587.     $this= tied(%$this)   if  $tied;
  1588.     my( $file, $subKey, $opts )= @_;
  1589.     if(  2 == @_  &&  "HASH" eq ref($subKey)  ) {
  1590.     $opts= $subKey;
  1591.     undef $subKey;
  1592.     }
  1593.     @_ < 1  ||  3 < @_  ||  defined($opts) && "HASH" ne ref($opts)
  1594.       and  croak "Usage:  \$key= ",
  1595.          "${PACK}->Load( \$fileName, [\$newSubKey,] {OPT=>VAL...} );\n",
  1596.          "  options: @Load_Opts @new_Opts\nCalled";
  1597.     if(  defined($opts)  &&  exists($opts->{NewSubKey})  ) {
  1598.     $subKey= delete $opts->{NewSubKey};
  1599.     }
  1600.     if(  ! defined( $subKey )  ) {
  1601.     if(  "" ne $this->Machine  ) {
  1602.         ( $this )= $this->_connect( [$this->Machine,"LMachine"] );
  1603.     } else {
  1604.         ( $this )= $this->_rootKey( "LMachine" );    # Could also be "Users"
  1605.     }
  1606.     $subKey= "PerlTie:$$." . ++$Load_Cnt;
  1607.     }
  1608.     $this->RegLoadKey( $subKey, $file )
  1609.       or  return ();
  1610.     my $self= $this->new( $subKey, defined($opts) ? $opts : () );
  1611.     if(  ! defined( $self )  ) {
  1612.     { my $err= Win32::GetLastError();
  1613.     #{ local( $^E ); #}
  1614.         $this->RegUnLoadKey( $subKey )  or  carp
  1615.           "Can't unload $subKey from ", $this->Path, ": ", _ErrMsg, "\n";
  1616.         Win32::SetLastError($err);
  1617.     }
  1618.     return ();
  1619.     }
  1620.     $self->{UNLOADME}= [ $this, $subKey, $file ];
  1621.     $self= $self->TiedRef   if  $tied;
  1622.     return $self;
  1623. }
  1624.  
  1625.  
  1626. sub UnLoad
  1627. {
  1628.     my $self= shift(@_);
  1629.     $self= tied(%$self)   if  tied(%$self);
  1630.     @_  and  croak "Usage:  \$key->UnLoad;";
  1631.     my $unload= $self->{UNLOADME};
  1632.     "ARRAY" eq ref($unload)
  1633.       or  croak "${PACK}->UnLoad called on a key which was not Load()ed";
  1634.     my( $obj, $subKey, $file )= @$unload;
  1635.     $self->RegCloseKey;
  1636.     return Win32API::Registry::RegUnLoadKey( $obj->Handle, $subKey );
  1637. }
  1638.  
  1639.  
  1640. sub AllowSave
  1641. {
  1642.     my $self= shift(@_);
  1643.     $self= tied(%$self)   if  tied(%$self);
  1644.     return $self->AllowPriv( "SeBackupPrivilege", @_ );
  1645. }
  1646.  
  1647.  
  1648. sub AllowLoad
  1649. {
  1650.     my $self= shift(@_);
  1651.     $self= tied(%$self)   if  tied(%$self);
  1652.     return $self->AllowPriv( "SeRestorePrivilege", @_ );
  1653. }
  1654.  
  1655.  
  1656. # RegNotifyChangeKeyValue( hKey, bWatchSubtree, iNotifyFilter, hEvent, bAsync )
  1657.  
  1658.  
  1659. sub RegCloseKey { my $self= shift(@_);
  1660.     Win32API::Registry::RegCloseKey $self->Handle, @_; }
  1661. sub RegConnectRegistry { my $self= shift(@_);
  1662.     Win32API::Registry::RegConnectRegistry @_; }
  1663. sub RegCreateKey { my $self= shift(@_);
  1664.     Win32API::Registry::RegCreateKey $self->Handle, @_; }
  1665. sub RegCreateKeyEx { my $self= shift(@_);
  1666.     Win32API::Registry::RegCreateKeyEx $self->Handle, @_; }
  1667. sub RegDeleteKey { my $self= shift(@_);
  1668.     Win32API::Registry::RegDeleteKey $self->Handle, @_; }
  1669. sub RegDeleteValue { my $self= shift(@_);
  1670.     Win32API::Registry::RegDeleteValue $self->Handle, @_; }
  1671. sub RegEnumKey { my $self= shift(@_);
  1672.     Win32API::Registry::RegEnumKey $self->Handle, @_; }
  1673. sub RegEnumKeyEx { my $self= shift(@_);
  1674.     Win32API::Registry::RegEnumKeyEx $self->Handle, @_; }
  1675. sub RegEnumValue { my $self= shift(@_);
  1676.     Win32API::Registry::RegEnumValue $self->Handle, @_; }
  1677. sub RegFlushKey { my $self= shift(@_);
  1678.     Win32API::Registry::RegFlushKey $self->Handle, @_; }
  1679. sub RegGetKeySecurity { my $self= shift(@_);
  1680.     Win32API::Registry::RegGetKeySecurity $self->Handle, @_; }
  1681. sub RegLoadKey { my $self= shift(@_);
  1682.     Win32API::Registry::RegLoadKey $self->Handle, @_; }
  1683. sub RegNotifyChangeKeyValue { my $self= shift(@_);
  1684.     Win32API::Registry::RegNotifyChangeKeyValue $self->Handle, @_; }
  1685. sub RegOpenKey { my $self= shift(@_);
  1686.     Win32API::Registry::RegOpenKey $self->Handle, @_; }
  1687. sub RegOpenKeyEx { my $self= shift(@_);
  1688.     Win32API::Registry::RegOpenKeyEx $self->Handle, @_; }
  1689. sub RegQueryInfoKey { my $self= shift(@_);
  1690.     Win32API::Registry::RegQueryInfoKey $self->Handle, @_; }
  1691. sub RegQueryMultipleValues { my $self= shift(@_);
  1692.     Win32API::Registry::RegQueryMultipleValues $self->Handle, @_; }
  1693. sub RegQueryValue { my $self= shift(@_);
  1694.     Win32API::Registry::RegQueryValue $self->Handle, @_; }
  1695. sub RegQueryValueEx { my $self= shift(@_);
  1696.     Win32API::Registry::RegQueryValueEx $self->Handle, @_; }
  1697. sub RegReplaceKey { my $self= shift(@_);
  1698.     Win32API::Registry::RegReplaceKey $self->Handle, @_; }
  1699. sub RegRestoreKey { my $self= shift(@_);
  1700.     Win32API::Registry::RegRestoreKey $self->Handle, @_; }
  1701. sub RegSaveKey { my $self= shift(@_);
  1702.     Win32API::Registry::RegSaveKey $self->Handle, @_; }
  1703. sub RegSetKeySecurity { my $self= shift(@_);
  1704.     Win32API::Registry::RegSetKeySecurity $self->Handle, @_; }
  1705. sub RegSetValue { my $self= shift(@_);
  1706.     Win32API::Registry::RegSetValue $self->Handle, @_; }
  1707. sub RegSetValueEx { my $self= shift(@_);
  1708.     Win32API::Registry::RegSetValueEx $self->Handle, @_; }
  1709. sub RegUnLoadKey { my $self= shift(@_);
  1710.     Win32API::Registry::RegUnLoadKey $self->Handle, @_; }
  1711. sub AllowPriv { my $self= shift(@_);
  1712.     Win32API::Registry::AllowPriv @_; }
  1713.  
  1714.  
  1715. # Autoload methods go after =cut, and are processed by the autosplit program.
  1716.  
  1717. 1;
  1718. __END__
  1719.  
  1720. =head1 NAME
  1721.  
  1722. Win32::TieRegistry - Powerful and easy ways to manipulate a registry
  1723. [on Win32 for now].
  1724.  
  1725. =head1 SYNOPSIS
  1726.  
  1727.   use Win32::TieRegistry 0.20 ( UseOptionName=>UseOptionValue[,...] );
  1728.  
  1729.   $Registry->SomeMethodCall(arg1,...);
  1730.  
  1731.   $subKey= $Registry->{"Key\\SubKey\\"};
  1732.   $valueData= $Registry->{"Key\\SubKey\\\\ValueName"};
  1733.   $Registry->{"Key\\SubKey\\"}= { "NewSubKey" => {...} };
  1734.   $Registry->{"Key\\SubKey\\\\ValueName"}= "NewValueData";
  1735.   $Registry->{"\\ValueName"}= [ pack("fmt",$data), REG_DATATYPE ];
  1736.  
  1737. =head1 EXAMPLES
  1738.  
  1739.   use Win32::TieRegistry( Delimiter=>"#", ArrayValues=>0 );
  1740.   $pound= $Registry->Delimiter("/");
  1741.   $diskKey= $Registry->{"LMachine/System/Disk/"}
  1742.     or  die "Can't read LMachine/System/Disk key: $^E\n";
  1743.   $data= $key->{"/Information"}
  1744.     or  die "Can't read LMachine/System/Disk//Information value: $^E\n";
  1745.   $remoteKey= $Registry->{"//ServerA/LMachine/System/"}
  1746.     or  die "Can't read //ServerA/LMachine/System/ key: $^E\n";
  1747.   $remoteData= $remoteKey->{"Disk//Information"}
  1748.     or  die "Can't read ServerA's System/Disk//Information value: $^E\n";
  1749.   foreach $entry (  keys(%$diskKey)  ) {
  1750.       ...
  1751.   }
  1752.   foreach $subKey (  $diskKey->SubKeyNames  ) {
  1753.       ...
  1754.   }
  1755.   $diskKey->AllowSave( 1 );
  1756.   $diskKey->RegSaveKey( "C:/TEMP/DiskReg", [] );
  1757.  
  1758. =head1 DESCRIPTION
  1759.  
  1760. The I<Win32::TieRegistry> module lets you manipulate the Registry
  1761. via objects [as in "object oriented"] or via tied hashes.  But
  1762. you will probably mostly use a combination reference, that is, a
  1763. reference to a tied hash that has also been made an object so that
  1764. you can mix both access methods [as shown above].
  1765.  
  1766. If you did not get this module as part of L<libwin32>, you might
  1767. want to get a recent version of L<libwin32> from CPAN which should
  1768. include this module and the I<Win32API::Registry> module that it
  1769. uses.
  1770.  
  1771. Skip to the L<SUMMARY> section if you just want to dive in and start
  1772. using the Registry from Perl.
  1773.  
  1774. Accessing and manipulating the registry is extremely simple using
  1775. I<Win32::TieRegistry>.  A single, simple expression can return
  1776. you almost any bit of information stored in the Registry.
  1777. I<Win32::TieRegistry> also gives you full access to the "raw"
  1778. underlying API calls so that you can do anything with the Registry
  1779. in Perl that you could do in C.  But the "simple" interface has
  1780. been carefully designed to handle almost all operations itself
  1781. without imposing arbitrary limits while providing sensible
  1782. defaults so you can list only the parameters you care about.
  1783.  
  1784. But first, an overview of the Registry itself.
  1785.  
  1786. =head2 The Registry
  1787.  
  1788. The Registry is a forest:  a collection of several tree structures.
  1789. The root of each tree is a key.  These root keys are identified by
  1790. predefined constants whose names start with "HKEY_".  Although all
  1791. keys have a few attributes associated with each [a class, a time
  1792. stamp, and security information], the most important aspect of keys
  1793. is that each can contain subkeys and can contain values.
  1794.  
  1795. Each subkey has a name:  a string which cannot be blank and cannot
  1796. contain the delimiter character [backslash: C<'\\'>] nor nul
  1797. [C<'\0'>].  Each subkey is also a key and so can contain subkeys
  1798. and values [and has a class, time stamp, and security information].
  1799.  
  1800. Each value has a name:  a string which I<can> be blank and I<can>
  1801. contain the delimiter character [backslash: C<'\\'>] and any
  1802. character except for null, C<'\0'>.  Each value also has data
  1803. associated with it.  Each value's data is a contiguous chunk of
  1804. bytes, which is exactly what a Perl string value is so Perl
  1805. strings will usually be used to represent value data.
  1806.  
  1807. Each value also has a data type which says how to interpret the
  1808. value data.  The primary data types are:
  1809.  
  1810. =over
  1811.  
  1812. =item REG_SZ
  1813.  
  1814. A null-terminated string.
  1815.  
  1816. =item REG_EXPAND_SZ
  1817.  
  1818. A null-terminated string which contains substrings consisting of a
  1819. percent sign [C<'%'>], an environment variable name, then a percent
  1820. sign, that should be replaced with the value associate with that
  1821. environment variable.  The system does I<not> automatically do this
  1822. substitution.
  1823.  
  1824. =item REG_BINARY
  1825.  
  1826. Some arbitrary binary value.  You can think of these as being
  1827. "packed" into a string.
  1828.  
  1829. If your system has the L<SetDualVar> module installed,
  1830. the C<DualBinVals()> option wasn't turned off, and you
  1831. fetch a C<REG_BINARY> value of 4 bytes or fewer, then
  1832. you can use the returned value in a numeric context to
  1833. get at the "unpacked" numeric value.  See C<GetValue()>
  1834. for more information.
  1835.  
  1836. =item REG_MULTI_SZ
  1837.  
  1838. Several null-terminated strings concatenated together with an
  1839. extra trailing C<'\0'> at the end of the list.  Note that the list
  1840. can include empty strings so use the value's length to determine
  1841. the end of the list, not the first occurrence of C<'\0\0'>.
  1842. It is best to set the C<SplitMultis()> option so I<Win32::TieRegistry>
  1843. will split these values into an array of strings for you.
  1844.  
  1845. =item REG_DWORD
  1846.  
  1847. A long [4-byte] integer value.  These values are expected either
  1848. packed into a 4-character string or as a hex string of I<more than>
  1849. 4 characters [but I<not> as a numeric value, unfortunately, as there is
  1850. no sure way to tell a numeric value from a packed 4-byte string that
  1851. just happens to be a string containing a valid numeric value].
  1852.  
  1853. How such values are returned depends on the C<DualBinVals()> and
  1854. C<DWordsToHex()> options.  See C<GetValue()> for details.
  1855.  
  1856. =back
  1857.  
  1858. In the underlying Registry calls, most places which take a
  1859. subkey name also allow you to pass in a subkey "path" -- a
  1860. string of several subkey names separated by the delimiter
  1861. character, backslash [C<'\\'>].  For example, doing
  1862. C<RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\DISK",...)> is much
  1863. like opening the C<"SYSTEM"> subkey of C<HKEY_LOCAL_MACHINE>,
  1864. then opening its C<"DISK"> subkey, then closing the C<"SYSTEM">
  1865. subkey.
  1866.  
  1867. All of the I<Win32::TieRegistry> features allow you to use your
  1868. own delimiter in place of the system's delimiter, [C<'\\'>].  In
  1869. most of our examples we will use a forward slash [C<'/'>] as our
  1870. delimiter as it is easier to read and less error prone to use when
  1871. writing Perl code since you have to type two backslashes for each
  1872. backslash you want in a string.  Note that this is true even when
  1873. using single quotes -- C<'\\HostName\LMachine\'> is an invalid
  1874. string and must be written as C<'\\\\HostName\\LMachine\\'>.
  1875.  
  1876. You can also connect to the registry of other computers on your
  1877. network.  This will be discussed more later.
  1878.  
  1879. Although the Registry does not have a single root key, the
  1880. I<Win32::TieRegistry> module creates a virtual root key for you
  1881. which has all of the I<HKEY_*> keys as subkeys.
  1882.  
  1883. =head2 Tied Hashes Documentation
  1884.  
  1885. Before you can use a tied hash, you must create one.  One way to
  1886. do that is via:
  1887.  
  1888.     use Win32::TieRegistry ( TiedHash => '%RegHash' );
  1889.  
  1890. which exports a C<%RegHash> variable into your package and ties it
  1891. to the virtual root key of the Registry.  An alternate method is:
  1892.  
  1893.     my %RegHash;
  1894.     use Win32::TieRegistry ( TiedHash => \%RegHash );
  1895.  
  1896. There are also several ways you can tie a hash variable to any
  1897. other key of the Registry, which are discussed later.
  1898.  
  1899. Note that you will most likely use C<$Registry> instead of using
  1900. a tied hash.  C<$Registry> is a reference to a hash that has
  1901. been tied to the virtual root of your computer's Registry [as if,
  1902. C<$Registry= \%RegHash>].  So you would use C<$Registry-I<gt>{Key}>
  1903. rather than C<$RegHash{Key}> and use C<keys %{$Registry}> rather
  1904. than C<keys %RegHash>, for example.
  1905.  
  1906. For each hash which has been tied to a Registry key, the Perl
  1907. C<keys> function will return a list containing the name of each
  1908. of the key's subkeys with a delimiter character appended to it and
  1909. containing the name of each of the key's values with a delimiter
  1910. prepended to it.  For example:
  1911.  
  1912.     keys( %{ $Registry->{"HKEY_CLASSES_ROOT\\batfile\\"} } )
  1913.  
  1914. might yield the following list value:
  1915.  
  1916.     ( "DefaultIcon\\",  # The subkey named "DefaultIcon"
  1917.       "shell\\",        # The subkey named "shell"
  1918.       "shellex\\",      # The subkey named "shellex"
  1919.       "\\",             # The default value [named ""]
  1920.       "\\EditFlags" )   # The value named "EditFlags"
  1921.  
  1922. For the virtual root key, short-hand subkey names are used as
  1923. shown below.  You can use the short-hand name, the regular
  1924. I<HKEY_*> name, or any numeric value to access these keys, but
  1925. the short-hand names are all that will be returned by the C<keys>
  1926. function.
  1927.  
  1928. =over
  1929.  
  1930. =item "Classes" for HKEY_CLASSES_ROOT
  1931.  
  1932. Contains mappings between file name extensions and the uses
  1933. for such files along with configuration information for COM
  1934. [MicroSoft's Common Object Model] objects.  Usually a link to
  1935. the C<"SOFTWARE\\Classes"> subkey of the C<HKEY_LOCAL_MACHINE>
  1936. key.
  1937.  
  1938. =item "CUser" for HKEY_CURRENT_USER
  1939.  
  1940. Contains information specific to the currently logged-in user.
  1941. Mostly software configuration information.  Usually a link to
  1942. a subkey of the C<HKEY_USERS> key.
  1943.  
  1944. =item "LMachine" for HKEY_LOCAL_MACHINE
  1945.  
  1946. Contains all manner of information about the computer.
  1947.  
  1948. =item "Users" for HKEY_USERS
  1949.  
  1950. Contains one subkey, C<".DEFAULT">, which gets copied to a new
  1951. subkey whenever a new user is added.  Also contains a subkey for
  1952. each user of the system, though only those for active users
  1953. [usually only one] are loaded at any given time.
  1954.  
  1955. =item "PerfData" for HKEY_PERFORMANCE_DATA
  1956.  
  1957. Used to access data about system performance.  Access via this key
  1958. is "special" and all but the most carefully constructed calls will
  1959. fail, usually with C<ERROR_INSUFFICIENT_BUFFER>.  For example, you
  1960. can't enumerate key names without also enumerating values which
  1961. require huge buffers but the exact buffer size required cannot be
  1962. determined beforehand because C<RegQueryInfoKey()> I<always> fails
  1963. with C<ERROR_INSUFFICIENT_BUFFER> for C<HKEY_PERFORMANCE_DATA> no
  1964. matter how it is called.  So it is currently not very useful to
  1965. tie a hash to this key.  You can use it to create an object to use
  1966. for making carefully constructed calls to the underlying Reg*()
  1967. routines.
  1968.  
  1969. =item "CConfig" for HKEY_CURRENT_CONFIG
  1970.  
  1971. Contains minimal information about the computer's current
  1972. configuration that is required very early in the boot process.
  1973. For example, setting for the display adapter such as screen
  1974. resolution and refresh rate are found in here.
  1975.  
  1976. =item "DynData" for HKEY_DYN_DATA
  1977.  
  1978. Dynamic data.  We have found no documentation for this key.
  1979.  
  1980. =back
  1981.  
  1982. A tied hash is much like a regular hash variable in Perl -- you give
  1983. it a key string inside braces, [C<{> and C<}>], and it gives you
  1984. back a value [or lets you set a value].  For I<Win32::TieRegistry>
  1985. hashes, there are two types of values that will be returned.
  1986.  
  1987. =over
  1988.  
  1989. =item SubKeys
  1990.  
  1991. If you give it a string which represents a subkey, then it will
  1992. give you back a reference to a hash which has been tied to that
  1993. subkey.  It can't return the hash itself, so it returns a
  1994. reference to it.  It also blesses that reference so that it is
  1995. also an object so you can use it to call method functions.
  1996.  
  1997. =item Values
  1998.  
  1999. If you give it a string which is a value name, then it will give
  2000. you back a string which is the data for that value.  Alternately,
  2001. you can request that it give you both the data value string and
  2002. the data value type [we discuss how to request this later].  In
  2003. this case, it would return a reference to an array where the value
  2004. data string is element C<[0]> and the value data type is element
  2005. C<[1]>.
  2006.  
  2007. =back
  2008.  
  2009. The key string which you use in the tied hash must be interpreted
  2010. to determine whether it is a value name or a key name or a path
  2011. that combines several of these or even other things.  There are
  2012. two simple rules that make this interpretation easy and
  2013. unambiguous:
  2014.  
  2015.     Put a delimiter after each key name.
  2016.     Put a delimiter in front of each value name.
  2017.  
  2018. Exactly how the key string will be intepreted is governed by the
  2019. following cases, in the order listed.  These cases are designed
  2020. to "do what you mean".  Most of the time you won't have to think
  2021. about them, especially if you follow the two simple rules above.
  2022. After the list of cases we give several examples which should be
  2023. clear enough so feel free to skip to them unless you are worried
  2024. about the details.
  2025.  
  2026. =over
  2027.  
  2028. =item Remote machines
  2029.  
  2030. If the hash is tied to the virtual root of the registry [or the
  2031. virtual root of a remote machine's registry], then we treat hash
  2032. key strings which start with the delimiter character specially.
  2033.  
  2034. If the hash key string starts with two delimiters in a row, then
  2035. those should be immediately followed by the name of a remote
  2036. machine whose registry we wish to connect to.  That can be
  2037. followed by a delimiter and more subkey names, etc.  If the
  2038. machine name is not following by anything, then a virtual root
  2039. for the remote machine's registry is created, a hash is tied to
  2040. it, and a reference to that hash it is returned.
  2041.  
  2042. =item Hash key string starts with the delimiter
  2043.  
  2044. If the hash is tied to a virtual root key, then the leading
  2045. delimiter is ignored.  It should be followed by a valid Registry
  2046. root key name [either a short-hand name like C<"LMachine">, an
  2047. I<HKEY_*> value, or a numeric value].   This alternate notation is
  2048. allowed in order to be more consistant with the C<Open()> method
  2049. function.
  2050.  
  2051. For all other Registry keys, the leading delimiter indicates
  2052. that the rest of the string is a value name.  The leading
  2053. delimiter is stripped and the rest of the string [which can
  2054. be empty and can contain more delimiters] is used as a value
  2055. name with no further parsing.
  2056.  
  2057. =item Exact match with direct subkey name followed by delimiter
  2058.  
  2059. If you have already called the Perl C<keys> function on the tied
  2060. hash [or have already called C<MemberNames> on the object] and the
  2061. hash key string exactly matches one of the strings returned, then
  2062. no further parsing is done.  In other words, if the key string
  2063. exactly matches the name of a direct subkey with a delimiter
  2064. appended, then a reference to a hash tied to that subkey is
  2065. returned [but only if C<keys> or C<MemberNames> has already
  2066. been called for that tied hash].
  2067.  
  2068. This is only important if you have selected a delimiter other than
  2069. the system default delimiter and one of the subkey names contains
  2070. the delimiter you have chosen.  This rule allows you to deal with
  2071. subkeys which contain your chosen delimiter in their name as long
  2072. as you only traverse subkeys one level at a time and always
  2073. enumerate the list of members before doing so.
  2074.  
  2075. The main advantage of this is that Perl code which recursively
  2076. traverses a hash will work on hashes tied to Registry keys even if
  2077. a non-default delimiter has been selected.
  2078.  
  2079. =item Hash key string contains two delimiters in a row
  2080.  
  2081. If the hash key string contains two [or more] delimiters in a row,
  2082. then the string is split between the first pair of delimiters.
  2083. The first part is interpreted as a subkey name or a path of subkey
  2084. names separated by delimiters and with a trailing delimiter.  The
  2085. second part is interpreted as a value name with one leading
  2086. delimiter [any extra delimiters are considered part of the value
  2087. name].
  2088.  
  2089. =item Hash key string ends with a delimiter
  2090.  
  2091. If the key string ends with a delimiter, then it is treated
  2092. as a subkey name or path of subkey names separated by delimiters.
  2093.  
  2094. =item Hash key string contains a delimiter
  2095.  
  2096. If the key string contains a delimiter, then it is split after
  2097. the last delimiter.  The first part is treated as a subkey name or
  2098. path of subkey names separated by delimiters.  The second part
  2099. is ambiguous and is treated as outlined in the next item.
  2100.  
  2101. =item Hash key string contains no delimiters
  2102.  
  2103. If the hash key string contains no delimiters, then it is ambiguous.
  2104.  
  2105. If you are reading from the hash [fetching], then we first use the
  2106. key string as a value name.  If there is a value with a matching
  2107. name in the Registry key which the hash is tied to, then the value
  2108. data string [and possibly the value data type] is returned.
  2109. Otherwise, we retry by using the hash key string as a subkey name.
  2110. If there is a subkey with a matching name, then we return a
  2111. reference to a hash tied to that subkey.  Otherwise we return
  2112. C<undef>.
  2113.  
  2114. If you are writing to the hash [storing], then we use the key
  2115. string as a subkey name only if the value you are storing is a
  2116. reference to a hash value.  Otherwise we use the key string as
  2117. a value name.
  2118.  
  2119. =back
  2120.  
  2121. =head3 Examples
  2122.  
  2123. Here are some examples showing different ways of accessing Registry
  2124. information using references to tied hashes:
  2125.  
  2126. =over
  2127.  
  2128. =item Canonical value fetch
  2129.  
  2130.     $tip18= $Registry->{"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\"
  2131.                . 'Windows\\CurrentVersion\\Explorer\\Tips\\\\18'};
  2132.  
  2133. Should return the text of important tip number 18.  Note that two
  2134. backslashes, C<"\\">, are required to get a single backslash into
  2135. a Perl double-quoted or single-qouted string.  Note that C<"\\">
  2136. is appended to each key name [C<"HKEY_LOCAL_MACHINE"> through
  2137. C<"Tips">] and C<"\\"> is prepended to the value name, C<"18">.
  2138.  
  2139. =item Changing your delimiter
  2140.  
  2141.     $Registry->Delimiter("/");
  2142.     $tip18= $Registry->{"HKEY_LOCAL_MACHINE/Software/Microsoft/"
  2143.                . 'Windows/CurrentVersion/Explorer/Tips//18'};
  2144.  
  2145. This usually makes things easier to read when working in Perl.
  2146. All remaining examples will assume the delimiter has been changed
  2147. as above.
  2148.  
  2149. =item Using intermediate keys
  2150.  
  2151.     $ms= $Registry->{"LMachine/Software/Microsoft/"};
  2152.     $tips= $ms->{"Windows/CurrentVersion/Explorer/Tips/"};
  2153.     $tip18= $winlogon->{"/18"};
  2154.  
  2155. Same as above but opens more keys into the Registry which lets you
  2156. efficiently re-access those intermediate keys.  This is slightly
  2157. less efficient if you never reuse those intermediate keys.
  2158.  
  2159. =item Chaining in a single statement
  2160.  
  2161.     $tip18= $Registry->{"LMachine/Software/Microsoft/"}->
  2162.               {"Windows/CurrentVersion/Explorer/Tips/"}->{"/18"};
  2163.  
  2164. Like above, this creates intermediate key objects then uses
  2165. them to access other data.  Once this statement finishes, the
  2166. intermediate key objects are destroyed.  Several handles into
  2167. the Registry are opened and closed by this statement so it is
  2168. less efficient but there are times when this will be useful.
  2169.  
  2170. =item Even less efficient example of chaining
  2171.  
  2172.     $tip18= $Registry->{"LMachine/Software/Microsoft"}->
  2173.               {"Windows/CurrentVersion/Explorer/Tips"}->{"/18"};
  2174.  
  2175. Because we left off the trailing delimiters, I<Win32::TieRegistry>
  2176. doesn't know whether final names, C<"Microsoft"> and C<"Tips">,
  2177. are subkey names or value names.  So this statement ends up
  2178. executing the same code as the next one.
  2179.  
  2180. =item What the above really does
  2181.  
  2182.     $tip18= $Registry->{"LMachine/Software/"}->{"Microsoft"}->
  2183.               {"Windows/CurrentVersion/Explorer/"}->{"Tips"}->{"/18"};
  2184.  
  2185. With more chains to go through, more temporary objects are created
  2186. and later destroyed than in our first chaining example.  Also,
  2187. when C<"Microsoft"> is looked up, I<Win32::TieRegistry> first
  2188. tries to open it as a value and fails then tries it as a subkey.
  2189. The same is true for when it looks up C<"Tips">.
  2190.  
  2191. =item Getting all of the tips
  2192.  
  2193.     $tips= $Registry->{"LMachine/Software/Microsoft/"}->
  2194.               {"Windows/CurrentVersion/Explorer/Tips/"}
  2195.       or  die "Can't find the Windows tips: $^E\n";
  2196.     foreach(  keys %$tips  ) {
  2197.         print "$_: ", $tips->{$_}, "\n";
  2198.     }
  2199.  
  2200. First notice that we actually check for failure for the first
  2201. time.  We are assuming that the C<"Tips"> key contains no subkeys. 
  2202. Otherwise the C<print> statement would show something like
  2203. C<"Win32::TieRegistry=HASH(0xc03ebc)"> for each subkey.
  2204.  
  2205. The output from the above code will start something like:
  2206.  
  2207.     /0: If you don't know how to do something,[...]
  2208.  
  2209. =back
  2210.  
  2211. =head3 Deleting items
  2212.  
  2213. You can use the Perl C<delete> function to delete a value from a
  2214. Registry key or to delete a subkey as long that subkey contains
  2215. no subkeys of its own.  See L<More Examples>, below, for more
  2216. information.
  2217.  
  2218. =head3 Storing items
  2219.  
  2220. You can use the Perl assignment operator [C<=>] to create new
  2221. keys, create new values, or replace values.  The values you store
  2222. should be in the same format as the values you would fetch from a
  2223. tied hash.  For example, you can use a single assignment statement
  2224. to copy an entire Registry tree.  The following statement:
  2225.  
  2226.     $Registry->{"LMachine/Software/Classes/Tie_Registry/"}=
  2227.       $Registry->{"LMachine/Software/Classes/batfile/"};
  2228.  
  2229. creates a C<"Tie_Registry"> subkey under the C<"Software\\Classes">
  2230. subkey of the C<HKEY_LOCAL_MACHINE> key.  Then it populates it
  2231. with copies of all of the subkeys and values in the C<"batfile">
  2232. subkey and all of its subkeys.  Note that you need to have
  2233. called C<$Registry-E<gt>ArrayValues(1)> for the proper value data
  2234. type information to be copied.  Note also that this release of
  2235. I<Win32::TieRegistry> does not copy key attributes such as class
  2236. name and security information [this is planned for a future release].
  2237.  
  2238. The following statement creates a whole subtree in the Registry:
  2239.  
  2240.     $Registry->{"LMachine/Software/FooCorp/"}= {
  2241.         "FooWriter/" => {
  2242.             "/Version" => "4.032",
  2243.             "Startup/" => {
  2244.                 "/Title" => "Foo Writer Deluxe ][",
  2245.                 "/WindowSize" => [ pack("LL",$wid,$ht), "REG_BINARY" ],
  2246.                 "/TaskBarIcon" => [ "0x0001", "REG_DWORD" ],
  2247.             },
  2248.             "Compatibility/" => {
  2249.                 "/AutoConvert" => "Always",
  2250.                 "/Default Palette" => "Windows Colors",
  2251.             },
  2252.         },
  2253.         "/License", => "0123-9C8EF1-09-FC",
  2254.     };
  2255.  
  2256. Note that all but the last Registry key used on the left-hand
  2257. side of the assignment [that is, "LMachine/Software/" but not
  2258. "FooCorp/"] must already exist for this statement to succeed.
  2259.  
  2260. By using the leading a trailing delimiters on each subkey name and
  2261. value name, I<Win32::TieRegistry> will tell you if you try to assign
  2262. subkey information to a value or visa-versa.
  2263.  
  2264. =head3 More examples
  2265.  
  2266. =over
  2267.  
  2268. =item Adding a new tip
  2269.  
  2270.     $tips= $Registry->{"LMachine/Software/Microsoft/"}->
  2271.               {"Windows/CurrentVersion/Explorer/Tips/"}
  2272.       or  die "Can't find the Windows tips: $^E\n";
  2273.     $tips{'/186'}= "Be very careful when making changes to the Registry!";
  2274.  
  2275. =item Deleting our new tip
  2276.  
  2277.     $tips= $Registry->{"LMachine/Software/Microsoft/"}->
  2278.               {"Windows/CurrentVersion/Explorer/Tips/"}
  2279.       or  die "Can't find the Windows tips: $^E\n";
  2280.     $tip186= delete $tips{'/186'};
  2281.  
  2282. Note that Perl's C<delete> function returns the value that was deleted.
  2283.  
  2284. =item Adding a new tip differently
  2285.  
  2286.     $Registry->{"LMachine/Software/Microsoft/" .
  2287.                 "Windows/CurrentVersion/Explorer/Tips//186"}=
  2288.       "Be very careful when making changes to the Registry!";
  2289.  
  2290. =item Deleting differently
  2291.  
  2292.     $tip186= delete $Registry->{"LMachine/Software/Microsoft/Windows/" .
  2293.                                 "CurrentVersion/Explorer/Tips//186"};
  2294.  
  2295. Note that this only deletes the tail of what we looked up, the
  2296. C<"186"> value, not any of the keys listed.
  2297.  
  2298. =item Deleting a key
  2299.  
  2300. WARNING:  The following code will delete all information about the
  2301. current user's tip preferences.  Actually executing this command
  2302. would probably cause the user to see the Welcome screen the next
  2303. time they log in and may cause more serious problems.  This
  2304. statement is shown as an example only and should not be used when
  2305. experimenting.
  2306.  
  2307.     $tips= delete $Registry->{"CUser/Software/Microsoft/Windows/" .
  2308.                               "CurrentVersion/Explorer/Tips/"};
  2309.  
  2310. This deletes the C<"Tips"> key and the values it contains.  The
  2311. C<delete> function will return a reference to a hash [not a tied
  2312. hash] containing the value names and value data that were deleted.
  2313.  
  2314. The information to be returned is copied from the Registry into a
  2315. regular Perl hash before the key is deleted.  If the key has many
  2316. subkeys, this copying could take a significant amount of memory
  2317. and/or processor time.  So you can disable this process by calling
  2318. the C<FastDelete> member function:
  2319.  
  2320.     $prevSetting= $regKey->FastDelete(1);
  2321.  
  2322. which will cause all subsequent delete operations via C<$regKey>
  2323. to simply return a true value if they succeed.  This optimization
  2324. is automatically done if you use C<delete> in a void context.
  2325.  
  2326. =item Technical notes on deleting
  2327.  
  2328. If you use C<delete> to delete a Registry key or value and use
  2329. the return value, then I<Win32::TieRegistry> usually looks up the
  2330. current contents of that key or value so they can be returned if
  2331. the deletion is successful.  If the deletion succeeds but the
  2332. attempt to lookup the old contents failed, then the return value
  2333. of C<delete> will be C<$^E> from the failed part of the operation.
  2334.  
  2335. =item Undeleting a key
  2336.  
  2337.     $Registry->{"LMachine/Software/Microsoft/Windows/" .
  2338.                 "CurrentVersion/Explorer/Tips/"}= $tips;
  2339.  
  2340. This adds back what we just deleted.  Note that this version of
  2341. I<Win32::TieRegistry> will use defaults for the key attributes
  2342. [such as class name and security] and will not restore the
  2343. previous attributes.
  2344.  
  2345. =item Not deleting a key
  2346.  
  2347. WARNING:  Actually executing the following code could cause
  2348. serious problems.  This statement is shown as an example only and
  2349. should not be used when experimenting.
  2350.  
  2351.     $res= delete $Registry->{"CUser/Software/Microsoft/Windows/"}
  2352.     defined($res)  ||  die "Can't delete URL key: $^E\n";
  2353.  
  2354. Since the "Windows" key should contain subkeys, that C<delete>
  2355. statement should make no changes to the Registry, return C<undef>,
  2356. and set C<$^E> to "Access is denied".
  2357.  
  2358. =item Not deleting again
  2359.  
  2360.     $tips= $Registry->{"CUser/Software/Microsoft/Windows/" .
  2361.                        "CurrentVersion/Explorer/Tips/"};
  2362.     delete $tips;
  2363.  
  2364. The Perl C<delete> function requires that its argument be an
  2365. expression that ends in a hash element lookup [or hash slice],
  2366. which is not the case here.  The C<delete> function doesn't
  2367. know which hash $tips came from and so can't delete it.
  2368.  
  2369. =back
  2370.  
  2371. =head2 Objects Documentation
  2372.  
  2373. The following member functions are defined for use on
  2374. I<Win32::TieRegistry> objects:
  2375.  
  2376. =over
  2377.  
  2378. =item new
  2379.  
  2380. The C<new> method creates a new I<Win32::TieRegistry> object.
  2381. C<new> is mostly a synonym for C<Open()> so see C<Open()> below for
  2382. information on what arguments to pass in.  Examples:
  2383.  
  2384.     $machKey= new Win32::TieRegistry "LMachine"
  2385.       or  die "Can't access HKEY_LOCAL_MACHINE key: $^E\n";
  2386.     $userKey= Win32::TieRegistry->new("CUser")
  2387.       or  die "Can't access HKEY_CURRENT_USER key: $^E\n";
  2388.  
  2389. Note that calling C<new> via a reference to a tied hash returns
  2390. a simple object, not a reference to a tied hash.
  2391.  
  2392. =item Open
  2393.  
  2394. =item $subKey= $key->Open( $sSubKey, $rhOptions )
  2395.  
  2396. The C<Open> method opens a Registry key and returns a new
  2397. I<Win32::TieRegistry> object associated with that Registry key.
  2398. If C<Open> is called via a reference to a tied hash, then C<Open>
  2399. returns another reference to a tied hash.  Otherwise C<Open>
  2400. returns a simple object and you should then use C<TiedRef> to get
  2401. a reference to a tied hash.
  2402.  
  2403. C<$sSubKey> is a string specifying a subkey to be opened.
  2404. Alternately C<$sSubKey> can be a reference to an array value
  2405. containing the list of increasingly deep subkeys specifying the
  2406. path to the subkey to be opened.
  2407.  
  2408. C<$rhOptions> is an optional reference to a hash containing extra
  2409. options.  The C<Open> method supports two options, C<"Delimiter">
  2410. and C<"Access">, and C<$rhOptions> should have only have zero or
  2411. more of these strings as keys.  See the "Examples" section below
  2412. for more information.
  2413.  
  2414. The C<"Delimiter"> option specifies what string [usually a single
  2415. character] will be used as the delimiter to be appended to subkey
  2416. names and prepended to value names.  If this option is not specified,
  2417. the new key [C<$subKey>] inherits the delimiter of the old key
  2418. [C<$key>].
  2419.  
  2420. The C<"Access"> option specifies what level of access to the
  2421. Registry key you wish to have once it has been opened.  If this
  2422. option is not specified, the new key [C<$subKey>] is opened with
  2423. the same access level used when the old key [C<$key>] was opened.
  2424. The virtual root of the Registry pretends it was opened with
  2425. access C<KEY_READ()|KEY_WRITE()> so this is the default access when
  2426. opening keys directory via C<$Registry>.  If you don't plan on
  2427. modifying a key, you should open it with C<KEY_READ> access as
  2428. you may not have C<KEY_WRITE> access to it or some of its subkeys.
  2429.  
  2430. If the C<"Access"> option value is a string that starts with
  2431. C<"KEY_">, then it should match I<one> of the predefined access
  2432. levels [probably C<"KEY_READ">, C<"KEY_WRITE">, or
  2433. C<"KEY_ALL_ACCESS">] exported by the I<Win32API::Registry> module.
  2434. Otherwise, a numeric value is expected.  For maximum flexibility,
  2435. include C<use Win32::TieRegistry qw(:KEY_);>, for example, near
  2436. the top of your script so you can specify more complicated access
  2437. levels such as C<KEY_READ()|KEY_WRITE()>.
  2438.  
  2439. If C<$sSubKey> does not begin with the delimiter [or C<$sSubKey>
  2440. is an array reference], then the path to the subkey to be opened
  2441. will be relative to the path of the original key [C<$key>].  If
  2442. C<$sSubKey> begins with a single delimiter, then the path to the
  2443. subkey to be opened will be relative to the virtual root of the
  2444. Registry on whichever machine the original key resides.  If
  2445. C<$sSubKey> begins with two consectutive delimiters, then those
  2446. must be followed by a machine name which causes the C<Connect()>
  2447. method function to be called.
  2448.  
  2449. Examples:
  2450.  
  2451.     $machKey= $Registry->Open( "LMachine", {Access=>KEY_READ(),Delimiter=>"/"} )
  2452.       or  die "Can't open HKEY_LOCAL_MACHINE key: $^E\n";
  2453.     $swKey= $machKey->Open( "Software" );
  2454.     $logonKey= $swKey->Open( "Microsoft/Windows NT/CurrentVersion/Winlogon/" );
  2455.     $NTversKey= $swKey->Open( ["Microsoft","Windows NT","CurrentVersion"] );
  2456.     $versKey= $swKey->Open( qw(Microsoft Windows CurrentVersion) );
  2457.  
  2458.     $remoteKey= $Registry->Open( "//HostA/LMachine/System/", {Delimiter=>"/"} )
  2459.       or  die "Can't connect to HostA or can't open subkey: $^E\n";
  2460.  
  2461. =item Clone
  2462.  
  2463. =item $copy= $key->Clone
  2464.  
  2465. Creates a new object that is associated with the same Registry key
  2466. as the invoking object.
  2467.  
  2468. =item Connect
  2469.  
  2470. =item $remoteKey= $Registry->Connect( $sMachineName, $sKeyPath, $rhOptions )
  2471.  
  2472. The C<Connect> method connects to the Registry of a remote machine,
  2473. and opens a key within it, then returns a new I<Win32::TieRegistry>
  2474. object associated with that remote Registry key.  If C<Connect>
  2475. was called using a reference to a tied hash, then the return value
  2476. will also be a reference to a tied hash [or C<undef>].  Otherwise,
  2477. if you wish to use the returned object as a tied hash [not just as
  2478. an object], then use the C<TiedRef> method function after C<Connect>.
  2479.  
  2480. C<$sMachineName> is the name of the remote machine.  You don't have
  2481. to preceed the machine name with two delimiter characters.
  2482.  
  2483. C<$sKeyPath> is a string specifying the remote key to be opened.
  2484. Alternately C<$sKeyPath> can be a reference to an array value
  2485. containing the list of increasingly deep keys specifying the path
  2486. to the key to be opened.
  2487.  
  2488. C<$rhOptions> is an optional reference to a hash containing extra
  2489. options.  The C<Connect> method supports two options, C<"Delimiter">
  2490. and C<"Access">.  See the C<Open> method documentation for more
  2491. information on these options.
  2492.  
  2493. C<$sKeyPath> is already relative to the virtual root of the Registry
  2494. of the remote machine.  A single leading delimiter on C<sKeyPath>
  2495. will be ignored and is not required.
  2496.  
  2497. C<$sKeyPath> can be empty in which case C<Connect> will return an
  2498. object representing the virtual root key of the remote Registry.
  2499. Each subsequent use of C<Open> on this virtual root key will call
  2500. the system C<RegConnectRegistry> function.
  2501.  
  2502. The C<Connect> method can be called via any I<Win32::TieRegistry>
  2503. object, not just C<$Registry>.  Attributes such as the desired
  2504. level of access and the delimiter will be inherited from the
  2505. object used but the C<$sKeyPath> will always be relative to the
  2506. virtual root of the remote machine's registry.
  2507.  
  2508. Examples:
  2509.  
  2510.     $remMachKey= $Registry->Connect( "HostA", "LMachine", {Delimiter->"/"} )
  2511.       or  die "Can't connect to HostA's HKEY_LOCAL_MACHINE key: $^E\n";
  2512.  
  2513.     $remVersKey= $remMachKey->Connect( "www.microsoft.com",
  2514.                    "LMachine/Software/Microsoft/Inetsrv/CurrentVersion/",
  2515.                    { Access=>KEY_READ, Delimiter=>"/" } )
  2516.       or  die "Can't check what version of IIS Microsoft is running: $^E\n";
  2517.  
  2518.     $remVersKey= $remMachKey->Connect( "www",
  2519.                    qw(LMachine Software Microsoft Inetsrv CurrentVersion) )
  2520.       or  die "Can't check what version of IIS we are running: $^E\n";
  2521.  
  2522. =item ObjectRef
  2523.  
  2524. =item $object_ref= $obj_or_hash_ref->ObjectRef
  2525.  
  2526. For a simple object, just returns itself [C<$obj == $obj->ObjectRef>].
  2527.  
  2528. For a reference to a tied hash [if it is also an object], C<ObjectRef>
  2529. returns the simple object that the hash is tied to.
  2530.  
  2531. This is primarilly useful when debugging since typing C<x $Registry>
  2532. will try to display your I<entire> registry contents to your screen.
  2533. But the debugger command C<x $Registry->ObjectRef> will just dump
  2534. the implementation details of the underlying object to your screen.
  2535.  
  2536. =item Flush( $bFlush )
  2537.  
  2538. Flushes all cached information about the Registry key so that future
  2539. uses will get fresh data from the Registry.
  2540.  
  2541. If the optional C<$bFlush> is specified and a true value, then
  2542. C<RegFlushKey()> will be called, which is almost never necessary.
  2543.  
  2544. =item GetValue
  2545.  
  2546. =item $ValueData= $key->GetValue( $sValueName )
  2547.  
  2548. =item ($ValueData,$ValueType)= $key->GetValue( $sValueName )
  2549.  
  2550. Gets a Registry value's data and data type.
  2551.  
  2552. C<$ValueData> is usually just a Perl string that contains the
  2553. value data [packed into it].  For certain types of data, however,
  2554. C<$ValueData> may be processed as described below.
  2555.  
  2556. C<$ValueType> is the C<REG_*> constant describing the type of value
  2557. data stored in C<$ValueData>.  If the C<DualTypes()> option is on,
  2558. then C<$ValueType> will be a dual value.  That is, when used in a
  2559. numeric context, C<$ValueType> will give the numeric value of a
  2560. C<REG_*> constant.  However, when used in a non-numeric context,
  2561. C<$ValueType> will return the name of the C<REG_*> constant, for
  2562. example C<"REG_SZ"> [note the quotes].  So both of the following
  2563. can be true at the same time:
  2564.  
  2565.     $ValueType == REG_SZ()
  2566.     $ValueType eq "REG_SZ"
  2567.  
  2568. =over
  2569.  
  2570. =item REG_SZ and REG_EXPAND_SZ
  2571.  
  2572. If the C<FixSzNulls()> option is on, then the trailing C<'\0'> will be
  2573. stripped [unless there isn't one] before values of type C<REG_SZ>
  2574. and C<REG_EXPAND_SZ> are returned.  Note that C<SetValue()> will add
  2575. a trailing C<'\0'> under similar circumstances.
  2576.  
  2577. =item REG_MULTI_SZ
  2578.  
  2579. If the C<SplitMultis()> option is on, then values of this type are
  2580. returned as a reference to an array containing the strings.  For
  2581. example, a value that, with C<SplitMultis()> off, would be returned as:
  2582.  
  2583.     "Value1\000Value2\000\000"
  2584.  
  2585. would be returned, with C<SplitMultis()> on, as:
  2586.  
  2587.     [ "Value1", "Value2" ]
  2588.  
  2589. =item REG_DWORD
  2590.  
  2591. If the C<DualBinVals()> option is on, then the value is returned
  2592. as a scalar containing both a string and a number [much like
  2593. the C<$!> variable -- see the L<SetDualVar> module for more
  2594. information] where the number part is the "unpacked" value.
  2595. Use the returned value in a numeric context to access this part
  2596. of the value.  For example:
  2597.  
  2598.     $num= 0 + $Registry->{"CUser/Console//ColorTable01"};
  2599.  
  2600. If the C<DWordsToHex()> option is off, the string part of the
  2601. returned value is a packed, 4-byte string [use C<unpack("L",$value)>
  2602. to get the numeric value.
  2603.  
  2604. If C<DWordsToHex()> is on, the string part of the returned value is
  2605. a 10-character hex strings [with leading "0x"].  You can use
  2606. C<hex($value)> to get the numeric value.
  2607.  
  2608. Note that C<SetValue()> will properly understand each of these
  2609. returned value formats no matter how C<DualBinVals()> is set.
  2610.  
  2611. =back
  2612.  
  2613. =item ValueNames
  2614.  
  2615. =item @names= $key->ValueNames
  2616.  
  2617. Returns the list of value names stored directly in a Registry key.
  2618. Note that the names returned do I<not> have a delimiter prepended
  2619. to them like with C<MemberNames()> and tied hashes.
  2620.  
  2621. Once you request this information, it is cached in the object and
  2622. future requests will always return the same list unless C<Flush()>
  2623. has been called.
  2624.  
  2625. =item SubKeyNames
  2626.  
  2627. =item @key_names= $key->SubKeyNames
  2628.  
  2629. Returns the list of subkey names stored directly in a Registry key.
  2630. Note that the names returned do I<not> have a delimiter appended
  2631. to them like with C<MemberNames()> and tied hashes.
  2632.  
  2633. Once you request this information, it is cached in the object and
  2634. future requests will always return the same list unless C<Flush()>
  2635. has been called.
  2636.  
  2637. =item SubKeyClasses
  2638.  
  2639. =item @classes= $key->SubKeyClasses
  2640.  
  2641. Returns the list of classes for subkeys stored directly in a
  2642. Registry key.  The classes are returned in the same order as
  2643. the subkey names returned by C<SubKeyNames()>.
  2644.  
  2645. =item SubKeyTimes
  2646.  
  2647. =item @times= $key->SubKeyTimes
  2648.  
  2649. Returns the list of last-modified times for subkeys stored
  2650. directly in a Registry key.  The times are returned in the same
  2651. order as the subkey names returned by C<SubKeyNames()>.  Each
  2652. time is a C<FILETIME> structure packed into a Perl string.
  2653.  
  2654. Once you request this information, it is cached in the object and
  2655. future requests will always return the same list unless C<Flush()>
  2656. has been called.
  2657.  
  2658. =item MemberNames
  2659.  
  2660. =item @members= $key->MemberNames
  2661.  
  2662. Returns the list of subkey names and value names stored directly
  2663. in a Registry key.  Subkey names have a delimiter appended to the
  2664. end and value names have a delimiter prepended to the front.
  2665.  
  2666. Note that a value name could end in a delimiter [or could be C<"">
  2667. so that the member name returned is just a delimiter] so the
  2668. presence or absence of the leading delimiter is what should be
  2669. used to determine whether a particular name is for a subkey or a
  2670. value, not the presence or absence of a trailing delimiter.
  2671.  
  2672. Once you request this information, it is cached in the object and
  2673. future requests will always return the same list unless C<Flush()>
  2674. has been called.
  2675.  
  2676. =item Information
  2677.  
  2678. =item %info= $key->Information
  2679.  
  2680. =item @items= $key->Information( @itemNames );
  2681.  
  2682. Returns the following information about a Registry key:
  2683.  
  2684. =over
  2685.  
  2686. =item LastWrite
  2687.  
  2688. A C<FILETIME> structure indicating when the key was last modified
  2689. and packed into a Perl string.
  2690.  
  2691. =item CntSubKeys
  2692.  
  2693. The number of subkeys stored directly in this key.
  2694.  
  2695. =item CntValues
  2696.  
  2697. The number of values stored directly in this key.
  2698.  
  2699. =item SecurityLen
  2700.  
  2701. The length [in bytes] of the largest[?] C<SECURITY_DESCRIPTOR>
  2702. associated with the Registry key.
  2703.  
  2704. =item MaxValDataLen
  2705.  
  2706. The length [in bytes] of the longest value data associated with
  2707. a value stored in this key.
  2708.  
  2709. =item MaxSubKeyLen
  2710.  
  2711. The length [in chars] of the longest subkey name associated with
  2712. a subkey stored in this key.
  2713.  
  2714. =item MaxSubClassLen
  2715.  
  2716. The length [in chars] of the longest class name associated with
  2717. a subkey stored directly in this key.
  2718.  
  2719. =item MaxValNameLen
  2720.  
  2721. The length [in chars] of the longest value name associated with
  2722. a value stored in this key.
  2723.  
  2724. =back
  2725.  
  2726. With no arguments, returns a hash [not a reference to a hash] where
  2727. the keys are the names for the items given above and the values
  2728. are the information describe above.  For example:
  2729.  
  2730.     %info= ( "CntValues" => 25,         # Key contains 25 values.
  2731.              "MaxValNameLen" => 20,     # One of which has a 20-char name.
  2732.              "MaxValDataLen" => 42,     # One of which has a 42-byte value.
  2733.              "CntSubKeys" => 1,         # Key has 1 immediate subkey.
  2734.              "MaxSubKeyLen" => 13,      # One of which has a 12-char name.
  2735.              "MaxSubClassLen" => 0,     # All of which have class names of "".
  2736.              "SecurityLen" => 232,      # One SECURITY_DESCRIPTOR is 232 bytes.
  2737.              "LastWrite" => "\x90mZ\cX{\xA3\xBD\cA\c@\cA"
  2738.                            # Key was last modifed 1998/06/01 16:29:32 GMT
  2739.            );
  2740.  
  2741. With arguments, each one must be the name of a item given above.
  2742. The return value is the information associated with the listed
  2743. names.  In other words:
  2744.  
  2745.     return $key->Information( @names );
  2746.  
  2747. returns the same list as:
  2748.  
  2749.     %info= $key->Information;
  2750.     return @info{@names};
  2751.  
  2752. =item Delimiter
  2753.  
  2754. =item $oldDelim= $key->Delimiter
  2755.  
  2756. =item $oldDelim= $key->Delimiter( $newDelim )
  2757.  
  2758. Gets and possibly changes the delimiter used for this object.  The
  2759. delimiter is appended to subkey names and prepended to value names
  2760. in many return values.  It is also used when parsing keys passed
  2761. to tied hashes.
  2762.  
  2763. The delimiter defaults to backslash (C<'\\'>) but is inherited from
  2764. the object used to create a new object and can be specified by an
  2765. option when a new object is created.
  2766.  
  2767. =item Handle
  2768.  
  2769. =item $handle= $key->Handle
  2770.  
  2771. Returns the raw C<HKEY> handle for the associated Registry key as
  2772. an integer value.  This value can then be used to Reg*() calls
  2773. from I<Win32API::Registry>.  However, it is usually easier to just
  2774. call the I<Win32API::Registry> calls directly via:
  2775.  
  2776.     $key->RegNotifyChangeKeyValue( ... );
  2777.  
  2778. For the virtual root of the local or a remote Registry,
  2779. C<Handle()> return C<"NONE">.
  2780.  
  2781. =item Path
  2782.  
  2783. =item $path= $key->Path
  2784.  
  2785. Returns a string describing the path of key names to this
  2786. Registry key.  The string is built so that if it were passed
  2787. to C<$Registry->Open()>, it would reopen the same Registry key
  2788. [except in the rare case where one of the key names contains
  2789. C<$key->Delimiter>].
  2790.  
  2791. =item Machine
  2792.  
  2793. =item $computerName= $key->Machine
  2794.  
  2795. Returns the name of the computer [or "machine"] on which this Registry
  2796. key resides.  Returns C<""> for local Registry keys.
  2797.  
  2798. =item Access
  2799.  
  2800. Returns the numeric value of the bit mask used to specify the
  2801. types of access requested when this Registry key was opened.  Can
  2802. be compared to C<KEY_*> values.
  2803.  
  2804. =item OS_Delimiter
  2805.  
  2806. Returns the delimiter used by the operating system's RegOpenKeyEx()
  2807. call.  For Win32, this is always backslash (C<"\\">).
  2808.  
  2809. =item Roots
  2810.  
  2811. Returns the mapping from root key names like C<"LMachine"> to their
  2812. associated C<HKEY_*> constants.  Primarily for internal use and
  2813. subject to change.
  2814.  
  2815. =item Tie
  2816.  
  2817. =item $key->Tie( \%hash );
  2818.  
  2819. Ties the referenced hash to that Registry key.  Pretty much the
  2820. same as
  2821.  
  2822.     tie %hash, ref($key), $key;
  2823.  
  2824. Since C<ref($key)> is the class [package] to tie the hash to and
  2825. C<TIEHASH()> just returns its argument, C<$key>, [without calling
  2826. C<new()>] when it sees that it is already a blessed object.
  2827.  
  2828. =item TiedRef
  2829.  
  2830. =item $TiedHashRef= $hash_or_obj_ref->TiedRef
  2831.  
  2832. For a simple object, returns a reference to a hash tied to the
  2833. object.  Used to promote a simple object into a combined object
  2834. and hash ref.
  2835.  
  2836. If already a reference to a tied hash [that is also an object],
  2837. it just returns itself [C<$ref == $ref->TiedRef>].
  2838.  
  2839. Mostly used internally.
  2840.  
  2841. =item ArrayValues
  2842.  
  2843. =item $oldBool= $key->ArrayValues
  2844.  
  2845. =item $oldBool= $key->ArrayValues( $newBool )
  2846.  
  2847. Gets the current setting of the C<ArrayValues> option and possibly
  2848. turns it on or off.
  2849.  
  2850. When off, Registry values fetched via a tied hash are returned as
  2851. just a value scalar [the same as C<GetValue()> in a scalar context].
  2852. When on, they are returned as a reference to an array containing
  2853. the value data as the C<[0]> element and the data type as the C<[1]>
  2854. element.
  2855.  
  2856. =item TieValues
  2857.  
  2858. =item $oldBool= TieValues
  2859.  
  2860. =item $oldBool= TieValues( $newBool )
  2861.  
  2862. Gets the current setting of the C<TieValues> option and possibly
  2863. turns it on or off.
  2864.  
  2865. Turning this option on is not yet supported in this release of
  2866. I<Win32::TieRegistry>.  In a future release, turning this option
  2867. on will cause Registry values returned from a tied hash to be
  2868. a tied array that you can use to modify the value in the Registry.
  2869.  
  2870. =item FastDelete
  2871.  
  2872. =item $oldBool= $key->FastDelete
  2873.  
  2874. =item $oldBool= $key->FastDelete( $newBool )
  2875.  
  2876. Gets the current setting of the C<FastDelete> option and possibly
  2877. turns it on or off.
  2878.  
  2879. When on, successfully deleting a Registry key [via a tied hash]
  2880. simply returns C<1>.
  2881.  
  2882. When off, successfully deleting a Registry key [via a tied hash
  2883. and not in a void context] returns a reference to a hash that
  2884. contains the values present in the key when it was deleted.  This
  2885. hash is just like that returned when referencing the key before it
  2886. was deleted except that it is an ordinary hash, not one tied to
  2887. the I<Win32::TieRegistry> package.
  2888.  
  2889. Note that deleting either a Registry key or value via a tied hash
  2890. I<in a void context> prevents any overhead in trying to build an
  2891. appropriate return value.
  2892.  
  2893. Note that deleting a Registry I<value> via a tied hash [not in
  2894. a void context] returns the value data even if <FastDelete> is on.
  2895.  
  2896. =item SplitMultis
  2897.  
  2898. =item $oldBool= $key->SplitMultis
  2899.  
  2900. =item $oldBool= $key->SplitMultis( $newBool )
  2901.  
  2902. Gets the current setting of the C<SplitMultis> option and possibly
  2903. turns it on or off.
  2904.  
  2905. If on, Registry values of type C<REG_MULTI_SZ> are returned as
  2906. a reference to an array of strings.  See C<GetValue()> for more
  2907. information.
  2908.  
  2909. =item DWordsToHex
  2910.  
  2911. =item $oldBool= $key->DWordsToHex
  2912.  
  2913. =item $oldBool= $key->DWordsToHex( $newBool )
  2914.  
  2915. Gets the current setting of the C<DWordsToHex> option and possibly
  2916. turns it on or off.
  2917.  
  2918. If on, Registry values of type C<REG_DWORD> are returned as a hex
  2919. string with leading C<"0x"> and longer than 4 characters.  See
  2920. C<GetValue()> for more information.
  2921.  
  2922. =item FixSzNulls
  2923.  
  2924. =item $oldBool= $key->FixSzNulls
  2925.  
  2926. =item $oldBool= $key->FixSzNulls( $newBool )
  2927.  
  2928. Gets the current setting of the C<FixSzNulls> option and possibly
  2929. turns it on or off.
  2930.  
  2931. If on, Registry values of type C<REG_SZ> and C<REG_EXPAND_SZ> have
  2932. trailing C<'\0'>s added before they are set and stripped before
  2933. they are returned.  See C<GetValue()> and C<SetValue()> for more
  2934. information.
  2935.  
  2936. =item DualTypes
  2937.  
  2938. =item $oldBool= $key->DualTypes
  2939.  
  2940. =item $oldBool= $key->DualTypes( $newBool )
  2941.  
  2942. Gets the current setting of the C<DualTypes> option and possibly
  2943. turns it on or off.
  2944.  
  2945. If on, data types are returned as a combined numeric/string value
  2946. holding both the numeric value of a C<REG_*> constant and the
  2947. string value of the constant's name.  See C<GetValue()> for
  2948. more information.
  2949.  
  2950. =item DualBinVals
  2951.  
  2952. =item $oldBool= $key->DualBinVals
  2953.  
  2954. =item $oldBool= $key->DualBinVals( $newBool )
  2955.  
  2956. Gets the current setting of the C<DualBinVals> option and possibly
  2957. turns it on or off.
  2958.  
  2959. If on, Registry value data of type C<REG_BINARY> and no more than
  2960. 4 bytes long and Registry values of type C<REG_DWORD> are returned
  2961. as a combined numeric/string value where the numeric value is the
  2962. "unpacked" binary value as returned by:
  2963.  
  2964.         hex reverse unpack( "h*", $valData )
  2965.  
  2966. on a "little-endian" computer.  [Would be C<hex unpack("H*",$valData)>
  2967. on a "big-endian" computer if this module is ever ported to one.]
  2968.  
  2969. See C<GetValue()> for more information.
  2970.  
  2971. =item GetOptions
  2972.  
  2973. =item @oldOptValues= $key->GetOptions( @optionNames )
  2974.  
  2975. =item $refHashOfOldOpts= $key->GetOptions()
  2976.  
  2977. =item $key->GetOptions( \%hashForOldOpts )
  2978.  
  2979. Returns the current setting of any of the following options:
  2980.  
  2981.     Delimiter     FixSzNulls    DWordsToHex
  2982.     ArrayValues   SplitMultis   DualBinVals
  2983.     TieValues     FastDelete    DualTypes
  2984.  
  2985. Pass in one or more of the above names (as strings) to get back
  2986. an array of the corresponding current settings in the same order:
  2987.  
  2988.   my( $fastDel, $delim )= $key->GetOptions("FastDelete","Delimiter");
  2989.  
  2990. Pass in no arguments to get back a reference to a hash where
  2991. the above option names are the keys and the values are
  2992. the corresponding current settings for each option:
  2993.  
  2994.   my $href= $key->GetOptions();
  2995.   my $delim= $href->{Delimiter};
  2996.  
  2997. Pass in a single reference to a hash to have the above key/value
  2998. pairs I<added> to the referenced hash.  For this case, the
  2999. return value is the original object so further methods can be
  3000. chained after the call to GetOptions:
  3001.  
  3002.   my %oldOpts;
  3003.   $key->GetOptions( \%oldOpts )->SetOptions( Delimiter => "/" );
  3004.  
  3005. =item SetOptions
  3006.  
  3007. =item @oldOpts= $key->SetOptions( optNames=>$optValue,... )
  3008.  
  3009. Changes the current setting of any of the following options,
  3010. returning the previous setting(s):
  3011.  
  3012.     Delimiter     FixSzNulls    DWordsToHex   AllowLoad
  3013.     ArrayValues   SplitMultis   DualBinVals   AllowSave
  3014.     TieValues     FastDelete    DualTypes
  3015.  
  3016. For C<AllowLoad> and C<AllowSave>, instead of the previous
  3017. setting, C<SetOptions> returns whether or not the change was
  3018. successful.
  3019.  
  3020. In a scalar context, returns only the last item.  The last
  3021. option can also be specified as C<"ref"> or C<"r"> [which doesn't
  3022. need to be followed by a value] to allow chaining:
  3023.  
  3024.     $key->SetOptions(AllowSave=>1,"ref")->RegSaveKey(...)
  3025.  
  3026. =item SetValue
  3027.  
  3028. =item $okay= $key->SetValue( $ValueName, $ValueData );
  3029.  
  3030. =item $okay= $key->SetValue( $ValueName, $ValueData, $ValueType );
  3031.  
  3032. Adds or replaces a Registry value.  Returns a true value if
  3033. successfully, false otherwise.
  3034.  
  3035. C<$ValueName> is the name of the value to add or replace and
  3036. should I<not> have a delimiter prepended to it.  Case is ignored.
  3037.  
  3038. C<$ValueType> is assumed to be C<REG_SZ> if it is omitted.  Otherwise,
  3039. it should be one the C<REG_*> constants.
  3040.  
  3041. C<$ValueData> is the data to be stored in the value, probably packed
  3042. into a Perl string.  Other supported formats for value data are
  3043. listed below for each posible C<$ValueType>.
  3044.  
  3045. =over
  3046.  
  3047. =item REG_SZ or REG_EXPAND_SZ
  3048.  
  3049. The only special processing for these values is the addition of
  3050. the required trailing C<'\0'> if it is missing.  This can be
  3051. turned off by disabling the C<FixSzNulls> option.
  3052.  
  3053. =item REG_MULTI_SZ
  3054.  
  3055. These values can also be specified as a reference to a list of
  3056. strings.  For example, the following two lines are equivalent:
  3057.  
  3058.     $key->SetValue( "Val1\000Value2\000LastVal\000\000", "REG_MULTI_SZ" );
  3059.     $key->SetValue( ["Val1","Value2","LastVal"], "REG_MULTI_SZ" );
  3060.  
  3061. Note that if the required two trailing nulls (C<"\000\000">) are
  3062. missing, then this release of C<SetValue()> will I<not> add them.
  3063.  
  3064. =item REG_DWORD
  3065.  
  3066. These values can also be specified as a hex value with the leading
  3067. C<"0x"> included and totaling I<more than> 4 bytes.  These will be
  3068. packed into a 4-byte string via:
  3069.  
  3070.     $data= pack( "L", hex($data) );
  3071.  
  3072. =item REG_BINARY
  3073.  
  3074. This value type is listed just to emphasize that no alternate
  3075. format is supported for it.  In particular, you should I<not> pass
  3076. in a numeric value for this type of data.  C<SetValue()> cannot
  3077. distinguish such from a packed string that just happens to match
  3078. a numeric value and so will treat it as a packed string.
  3079.  
  3080. =back
  3081.  
  3082. An alternate calling format:
  3083.  
  3084.     $okay= $key->SetValue( $ValueName, [ $ValueData, $ValueType ] );
  3085.  
  3086. [two arguments, the second of which is a reference to an array
  3087. containing the value data and value type] is supported to ease
  3088. using tied hashes with C<SetValue()>.
  3089.  
  3090. =item CreateKey
  3091.  
  3092. =item $newKey= $key->CreateKey( $subKey );
  3093.  
  3094. =item $newKey= $key->CreateKey( $subKey, { Option=>OptVal,... } );
  3095.  
  3096. Creates a Registry key or just updates attributes of one.  Calls
  3097. C<RegCreateKeyEx()> then, if it succeeded, creates an object
  3098. associated with the [possibly new] subkey.
  3099.  
  3100. C<$subKey> is the name of a subkey [or a path to one] to be
  3101. created or updated.  It can also be a reference to an array
  3102. containing a list of subkey names.
  3103.  
  3104. The second argument, if it exists, should be a reference to a
  3105. hash specifying options either to be passed to C<RegCreateKeyEx()>
  3106. or to be used when creating the associated object.  The following
  3107. items are the supported keys for this options hash:
  3108.  
  3109. =over
  3110.  
  3111. =item Delimiter
  3112.  
  3113. Specifies the delimiter to be used to parse C<$subKey> and to be
  3114. used in the new object.  Defaults to C<$key->Delimiter>.
  3115.  
  3116. =item Access
  3117.  
  3118. Specifies the types of access requested when the subkey is opened.
  3119. Should be a numeric bit mask that combines one or more C<KEY_*>
  3120. constant values.
  3121.  
  3122. =item Class
  3123.  
  3124. The name to assign as the class of the new or updated subkey.
  3125. Defaults to C<""> as we have never seen a use for this information.
  3126.  
  3127. =item Disposition
  3128.  
  3129. Lets you specify a reference to a scalar where, upon success, will be
  3130. stored either C<REG_CREATED_NEW_KEY()> or C<REG_OPENED_EXISTING_KEY()>
  3131. depending on whether a new key was created or an existing key was
  3132. opened.
  3133.  
  3134. If you, for example, did C<use Win32::TieRegistry qw(REG_CREATED_NEW_KEY)>
  3135. then you can use C<REG_CREATED_NEW_KEY()> to compare against the numeric
  3136. value stored in the referenced scalar.
  3137.  
  3138. If the C<DualTypes> option is enabled, then in addition to the
  3139. numeric value described above, the referenced scalar will also
  3140. have a string value equal to either C<"REG_CREATED_NEW_KEY"> or
  3141. C<"REG_OPENED_EXISTING_KEY">, as appropriate.
  3142.  
  3143. =item Security
  3144.  
  3145. Lets you specify a C<SECURITY_ATTRIBUTES> structure packed into a
  3146. Perl string.  See C<Win32API::Registry::RegCreateKeyEx()> for more
  3147. information.
  3148.  
  3149. =item Volatile
  3150.  
  3151. If true, specifies that the new key should be volatile, that is,
  3152. stored only in memory and not backed by a hive file [and not saved
  3153. if the computer is rebooted].  This option is ignored under
  3154. Windows 95.  Specifying C<Volatile=E<gt>1>  is the same as
  3155. specifying C<Options=E<gt>REG_OPTION_VOLATILE>.
  3156.  
  3157. =item Backup
  3158.  
  3159. If true, specifies that the new key should be opened for
  3160. backup/restore access.  The C<Access> option is ignored.  If the
  3161. calling process has enabled C<"SeBackupPrivilege">, then the
  3162. subkey is opened with C<KEY_READ> access as the C<"LocalSystem">
  3163. user which should have access to all subkeys.  If the calling
  3164. process has enabled C<"SeRestorePrivilege">, then the subkey is
  3165. opened with C<KEY_WRITE> access as the C<"LocalSystem"> user which
  3166. should have access to all subkeys.
  3167.  
  3168. This option is ignored under Windows 95.  Specifying C<Backup=E<gt>1>
  3169. is the same as specifying C<Options=E<gt>REG_OPTION_BACKUP_RESTORE>.
  3170.  
  3171. =item Options
  3172.  
  3173. Lets you specify options to the C<RegOpenKeyEx()> call.  The value
  3174. for this option should be a numeric value combining zero or more
  3175. of the C<REG_OPTION_*> bit masks.  You may with to used the
  3176. C<Volatile> and/or C<Backup> options instead of this one.
  3177.  
  3178. =back
  3179.  
  3180. =item StoreKey
  3181.  
  3182. =item $newKey= $key->StoreKey( $subKey, \%Contents );
  3183.  
  3184. Primarily for internal use.
  3185.  
  3186. Used to create or update a Registry key and any number of subkeys
  3187. or values under it or its subkeys.
  3188.  
  3189. C<$subKey> is the name of a subkey to be created [or a path of
  3190. subkey names separated by delimiters].  If that subkey already
  3191. exists, then it is updated.
  3192.  
  3193. C<\%Contents> is a reference to a hash containing pairs of
  3194. value names with value data and/or subkey names with hash
  3195. references similar to C<\%Contents>.  Each of these cause
  3196. a value or subkey of C<$subKey> to be created or updated.
  3197.  
  3198. If C<$Contents{""}> exists and is a reference to a hash, then
  3199. it used as the options argument when C<CreateKey()> is called
  3200. for C<$subKey>.  This allows you to specify ...
  3201.  
  3202.     if(  defined( $$data{""} )  &&  "HASH" eq ref($$data{""})  ) {
  3203.         $self= $this->CreateKey( $subKey, delete $$data{""} );
  3204.  
  3205. =item Load
  3206.  
  3207. =item $newKey= $key->Load( $file )
  3208.  
  3209. =item $newKey= $key->Load( $file, $newSubKey )
  3210.  
  3211. =item $newKey= $key->Load( $file, $newSubKey, { Option=>OptVal... } )
  3212.  
  3213. =item $newKey= $key->Load( $file, { Option=>OptVal... } )
  3214.  
  3215. Loads a hive file into a Registry.  That is, creates a new subkey
  3216. and associates a hive file with it.
  3217.  
  3218. C<$file> is a hive file, that is a file created by calling
  3219. C<RegSaveKey()>.  The C<$file> path is interpreted relative to
  3220. C<%SystemRoot%/System32/config> on the machine where C<$key>
  3221. resides.
  3222.  
  3223. C<$newSubKey> is the name to be given to the new subkey.  If
  3224. C<$newSubKey> is specified, then C<$key> must be
  3225. C<HKEY_LOCAL_MACHINE> or C<HKEY_USERS> of the local computer
  3226. or a remote computer and C<$newSubKey> should not contain any
  3227. occurrences of either the delimiter or the OS delimiter.
  3228.  
  3229. If C<$newSubKey> is not specified, then it is as if C<$key>
  3230. was C<$Registry-E<gt>{LMachine}> and C<$newSubKey> is
  3231. C<"PerlTie:999"> where C<"999"> is actually a sequence number
  3232. incremented each time this process calls C<Load()>.
  3233.  
  3234. You can specify as the last argument a reference to a hash
  3235. containing options.  You can specify the same options that you
  3236. can specify to C<Open()>.  See C<Open()> for more information on
  3237. those.  In addition, you can specify the option C<"NewSubKey">.
  3238. The value of this option is interpretted exactly as if it was
  3239. specified as the C<$newSubKey> parameter and overrides the
  3240. C<$newSubKey> if one was specified.
  3241.  
  3242. The hive is automatically unloaded when the returned object
  3243. [C<$newKey>] is destroyed.  Registry key objects opened within
  3244. the hive will keep a reference to the C<$newKey> object so that
  3245. it will not be destroyed before these keys are closed.
  3246.  
  3247. =item UnLoad
  3248.  
  3249. =item $okay= $key->UnLoad
  3250.  
  3251. Unloads a hive that was loaded via C<Load()>.  Cannot unload other
  3252. hives.  C<$key> must be the return from a previous call to C<Load()>.
  3253. C<$key> is closed and then the hive is unloaded.
  3254.  
  3255. =item AllowSave
  3256.  
  3257. =item $okay= AllowSave( $bool )
  3258.  
  3259. Enables or disables the C<"ReBackupPrivilege"> privilege for the
  3260. current process.  You will probably have to enable this privilege
  3261. before you can use C<RegSaveKey()>.
  3262.  
  3263. The return value indicates whether the operation succeeded, not
  3264. whether the privilege was previously enabled.
  3265.  
  3266. =item AllowLoad
  3267.  
  3268. =item $okay= AllowLoad( $bool )
  3269.  
  3270. Enables or disables the C<"ReRestorePrivilege"> privilege for the
  3271. current process.  You will probably have to enable this privilege
  3272. before you can use C<RegLoadKey()>, C<RegUnLoadKey()>,
  3273. C<RegReplaceKey()>, or C<RegRestoreKey> and thus C<Load()> and
  3274. C<UnLoad()>.
  3275.  
  3276. The return value indicates whether the operation succeeded, not
  3277. whether the privilege was previously enabled.
  3278.  
  3279. =back
  3280.  
  3281. =head2 Exports [C<use> and C<import()>]
  3282.  
  3283. To have nothing imported into your package, use something like:
  3284.  
  3285.     use Win32::TieRegistry 0.20 ();
  3286.  
  3287. which would verify that you have at least version 0.20 but wouldn't
  3288. call C<import()>.  The F<Changes> file can be useful in figuring out
  3289. which, if any, prior versions of I<Win32::TieRegistry> you want to
  3290. support in your script.
  3291.  
  3292. The code
  3293.  
  3294.     use Win32::TieRegistry;
  3295.  
  3296. imports the variable C<$Registry> into your package and sets it
  3297. to be a reference to a hash tied to a copy of the master Registry
  3298. virtual root object with the default options.  One disadvantage
  3299. to this "default" usage is that Perl does not support checking
  3300. the module version when you use it.
  3301.  
  3302. Alternately, you can specify a list of arguments on the C<use>
  3303. line that will be passed to the C<Win32::TieRegistry->import()>
  3304. method to control what items to import into your package.  These
  3305. arguments fall into the following broad categories:
  3306.  
  3307. =over
  3308.  
  3309. =item Import a reference to a hash tied to a Registry virtual root
  3310.  
  3311. You can request that a scalar variable be imported (possibly)
  3312. and set to be a reference to a hash tied to a Registry virtual root
  3313. using any of the following types of arguments or argument pairs:
  3314.  
  3315. =over
  3316.  
  3317. =item "TiedRef", '$scalar'
  3318.  
  3319. =item "TiedRef", '$pack::scalar'
  3320.  
  3321. =item "TiedRef", 'scalar'
  3322.  
  3323. =item "TiedRef", 'pack::scalar'
  3324.  
  3325. All of the above import a scalar named C<$scalar> into your package
  3326. (or the package named "pack") and then sets it.
  3327.  
  3328. =item '$scalar'
  3329.  
  3330. =item '$pack::scalar'
  3331.  
  3332. These are equivalent to the previous items to support a more
  3333. traditional appearance to the list of exports.  Note that the
  3334. scalar name cannot be "RegObj" here.
  3335.  
  3336. =item "TiedRef", \$scalar
  3337.  
  3338. =item \$scalar
  3339.  
  3340. These versions don't import anything but set the referenced C<$scalar>.
  3341.  
  3342. =back
  3343.  
  3344. =item Import a hash tied to the Registry virtual root
  3345.  
  3346. You can request that a hash variable be imported (possibly)
  3347. and tied to a Registry virtual root using any of the following
  3348. types of arguments or argument pairs:
  3349.  
  3350. =over
  3351.  
  3352. =item "TiedHash", '%hash'
  3353.  
  3354. =item "TiedHash", '%pack::hash'
  3355.  
  3356. =item "TiedHash", 'hash'
  3357.  
  3358. =item "TiedHash", 'pack::hash'
  3359.  
  3360. All of the above import a hash named C<%hash> into your package
  3361. (or the package named "pack") and then sets it.
  3362.  
  3363. =item '%hash'
  3364.  
  3365. =item '%pack::hash'
  3366.  
  3367. These are equivalent to the previous items to support a more
  3368. traditional appearance to the list of exports.
  3369.  
  3370. =item "TiedHash", \%hash
  3371.  
  3372. =item \%hash
  3373.  
  3374. These versions don't import anything but set the referenced C<%hash>.
  3375.  
  3376. =back
  3377.  
  3378. =item Import a Registry virtual root object
  3379.  
  3380. You can request that a scalar variable be imported (possibly)
  3381. and set to be a Registry virtual root object using any of the
  3382. following types of arguments or argument pairs:
  3383.  
  3384. =over
  3385.  
  3386. =item "ObjectRef", '$scalar'
  3387.  
  3388. =item "ObjectRef", '$pack::scalar'
  3389.  
  3390. =item "ObjectRef", 'scalar'
  3391.  
  3392. =item "ObjectRef", 'pack::scalar'
  3393.  
  3394. All of the above import a scalar named C<$scalar> into your package
  3395. (or the package named "pack") and then sets it.
  3396.  
  3397. =item '$RegObj'
  3398.  
  3399. This is equivalent to the previous items for backward compatibility.
  3400.  
  3401. =item "ObjectRef", \$scalar
  3402.  
  3403. This version doesn't import anything but sets the referenced C<$scalar>.
  3404.  
  3405. =back
  3406.  
  3407. =item Import constant(s) exported by I<Win32API::Registry>
  3408.  
  3409. You can list any constants that are exported by I<Win32API::Registry>
  3410. to have them imported into your package.  These constants have names
  3411. starting with "KEY_" or "REG_" (or even "HKEY_").
  3412.  
  3413. You can also specify C<":KEY_">, C<":REG_">, and even C<":HKEY_"> to
  3414. import a whole set of constants.
  3415.  
  3416. See I<Win32API::Registry> documentation for more information.
  3417.  
  3418. =item Options
  3419.  
  3420. You can list any option names that can be listed in the C<SetOptions()>
  3421. method call, each folowed by the value to use for that option.
  3422. A Registry virtual root object is created, all of these options are
  3423. set for it, then each variable to be imported/set is associated with
  3424. this object.
  3425.  
  3426. In addition, the following special options are supported:
  3427.  
  3428. =over
  3429.  
  3430. =item ExportLevel
  3431.  
  3432. Whether to import variables into your package or some
  3433. package that uses your package.  Defaults to the value of
  3434. C<$Exporter::ExportLevel> and has the same meaning.  See
  3435. the L<Exporter> module for more information.
  3436.  
  3437. =item ExportTo
  3438.  
  3439. The name of the package to import variables and constants into.
  3440. Overrides I<ExportLevel>.
  3441.  
  3442. =back
  3443.  
  3444. =back
  3445.  
  3446. =head3 Specifying constants in your Perl code
  3447.  
  3448. This module was written with a strong emphasis on the convenience of
  3449. the module user.  Therefore, most places where you can specify a
  3450. constant like C<REG_SZ()> also allow you to specify a string
  3451. containing the name of the constant, C<"REG_SZ">.  This is convenient
  3452. because you may not have imported that symbolic constant.
  3453.  
  3454. Perl also emphasizes programmer convenience so the code C<REG_SZ>
  3455. can be used to mean C<REG_SZ()> or C<"REG_SZ"> or be illegal.
  3456. Note that using C<®_SZ> (as we've seen in much Win32 Perl code)
  3457. is not a good idea since it passes the current C<@_> to the
  3458. C<constant()> routine of the module which, at the least, can give
  3459. you a warning under B<-w>.
  3460.  
  3461. Although greatly a matter of style, the "safest" practice is probably
  3462. to specifically list all constants in the C<use Win32::TieRegistry>
  3463. statement, specify C<use strict> [or at least C<use strict qw(subs)>],
  3464. and use bare constant names when you want the numeric value.  This will
  3465. detect mispelled constant names at compile time.
  3466.  
  3467.     use strict;
  3468.     my $Registry;
  3469.     use Win32::TieRegistry 0.20 (
  3470.         TiedRef => \$Registry,  Delimiter => "/",  ArrayValues => 1,
  3471.     SplitMultis => 1,  AllowLoad => 1,
  3472.         qw( REG_SZ REG_EXPAND_SZ REG_DWORD REG_BINARY REG_MULTI_SZ
  3473.         KEY_READ KEY_WRITE KEY_ALL_ACCESS ),
  3474.     );
  3475.     $Registry->{"LMachine/Software/FooCorp/"}= {
  3476.         "FooWriter/" => {
  3477.             "/Fonts" => [ ["Times","Courier","Lucinda"], REG_MULTI_SZ ],
  3478.         "/WindowSize" => [ pack("LL",24,80), REG_BINARY ],
  3479.         "/TaskBarIcon" => [ "0x0001", REG_DWORD ],
  3480.     },
  3481.     }  or  die "Can't create Software/FooCorp/: $^E\n";
  3482.  
  3483. If you don't want to C<use strict qw(subs)>, the second safest practice
  3484. is similar to the above but use the C<REG_SZ()> form for constants
  3485. when possible and quoted constant names when required.  Note that
  3486. C<qw()> is a form of quoting.
  3487.  
  3488.     use Win32::TieRegistry 0.20 qw(
  3489.         TiedRef $Registry
  3490.         Delimiter /  ArrayValues 1  SplitMultis 1  AllowLoad 1
  3491.         REG_SZ REG_EXPAND_SZ REG_DWORD REG_BINARY REG_MULTI_SZ
  3492.         KEY_READ KEY_WRITE KEY_ALL_ACCESS
  3493.     );
  3494.     $Registry->{"LMachine/Software/FooCorp/"}= {
  3495.         "FooWriter/" => {
  3496.             "/Fonts" => [ ["Times","Courier","Lucinda"], REG_MULTI_SZ() ],
  3497.         "/WindowSize" => [ pack("LL",24,80), REG_BINARY() ],
  3498.         "/TaskBarIcon" => [ "0x0001", REG_DWORD() ],
  3499.     },
  3500.     }  or  die "Can't create Software/FooCorp/: $^E\n";
  3501.  
  3502. The examples in this document mostly use quoted constant names
  3503. (C<"REG_SZ">) since that works regardless of which constants
  3504. you imported and whether or not you have C<use strict> in your
  3505. script.  It is not the best choice for you to use for real
  3506. scripts (vs. examples) because it is less efficient and is not
  3507. supported by most other similar modules.
  3508.  
  3509. =head1 SUMMARY
  3510.  
  3511. Most things can be done most easily via tied hashes.  Skip down to the
  3512. the L<Tied Hashes Summary> to get started quickly.
  3513.  
  3514. =head2 Objects Summary
  3515.  
  3516. Here are quick examples that document the most common functionality
  3517. of all of the method functions [except for a few almost useless ones].
  3518.  
  3519.     # Just another way of saying Open():
  3520.     $key= new Win32::TieRegistry "LMachine\\Software\\",
  3521.       { Access=>KEY_READ()|KEY_WRITE(), Delimiter=>"\\" };
  3522.  
  3523.     # Open a Registry key:
  3524.     $subKey= $key->Open( "SubKey/SubSubKey/",
  3525.       { Access=>KEY_ALL_ACCESS, Delimiter=>"/" } );
  3526.  
  3527.     # Connect to a remote Registry key:
  3528.     $remKey= $Registry->Connect( "MachineName", "LMachine/",
  3529.       { Access=>KEY_READ, Delimiter=>"/" } );
  3530.  
  3531.     # Get value data:
  3532.     $valueString= $key->GetValue("ValueName");
  3533.     ( $valueString, $valueType )= $key->GetValue("ValueName");
  3534.  
  3535.     # Get list of value names:
  3536.     @valueNames= $key->ValueNames;
  3537.  
  3538.     # Get list of subkey names:
  3539.     @subKeyNames= $key->SubKeyNames;
  3540.  
  3541.     # Get combined list of value names (with leading delimiters)
  3542.     # and subkey names (with trailing delimiters):
  3543.     @memberNames= $key->MemberNames;
  3544.  
  3545.     # Get all information about a key:
  3546.     %keyInfo= $key->Information;
  3547.     # keys(%keyInfo)= qw( Class LastWrite SecurityLen
  3548.     #   CntSubKeys MaxSubKeyLen MaxSubClassLen
  3549.     #   CntValues MaxValNameLen MaxValDataLen );
  3550.  
  3551.     # Get selected information about a key:
  3552.     ( $class, $cntSubKeys )= $key->Information( "Class", "CntSubKeys" );
  3553.  
  3554.     # Get and/or set delimiter:
  3555.     $delim= $key->Delimiter;
  3556.     $oldDelim= $key->Delimiter( $newDelim );
  3557.  
  3558.     # Get "path" for an open key:
  3559.     $path= $key->Path;
  3560.     # For example, "/CUser/Control Panel/Mouse/"
  3561.     # or "//HostName/LMachine/System/DISK/".
  3562.  
  3563.     # Get name of machine where key is from:
  3564.     $mach= $key->Machine;
  3565.     # Will usually be "" indicating key is on local machine.
  3566.  
  3567.     # Control different options (see main documentation for descriptions):
  3568.     $oldBool= $key->ArrayValues( $newBool );
  3569.     $oldBool= $key->FastDelete( $newBool );
  3570.     $oldBool= $key->FixSzNulls( $newBool );
  3571.     $oldBool= $key->SplitMultis( $newBool );
  3572.     $oldBool= $key->DWordsToHex( $newBool );
  3573.     $oldBool= $key->DualBinVals( $newBool );
  3574.     $oldBool= $key->DualTypes( $newBool );
  3575.     @oldBools= $key->SetOptions( ArrayValues=>1, FastDelete=>1, FixSzNulls=>0,
  3576.       Delimiter=>"/", AllowLoad=>1, AllowSave=>1 );
  3577.     @oldBools= $key->GetOptions( ArrayValues, FastDelete, FixSzNulls );
  3578.  
  3579.     # Add or set a value:
  3580.     $key->SetValue( "ValueName", $valueDataString );
  3581.     $key->SetValue( "ValueName", pack($format,$valueData), "REG_BINARY" );
  3582.  
  3583.     # Add or set a key:
  3584.     $key->CreateKey( "SubKeyName" );
  3585.     $key->CreateKey( "SubKeyName",
  3586.       { Access=>"KEY_ALL_ACCESS", Class=>"ClassName",
  3587.         Delimiter=>"/", Volatile=>1, Backup=>1 } );
  3588.  
  3589.     # Load an off-line Registry hive file into the on-line Registry:
  3590.     $newKey= $Registry->Load( "C:/Path/To/Hive/FileName" );
  3591.     $newKey= $key->Load( "C:/Path/To/Hive/FileName", "NewSubKeyName",
  3592.                      { Access=>"KEY_READ" } );
  3593.     # Unload a Registry hive file loaded via the Load() method:
  3594.     $newKey->UnLoad;
  3595.  
  3596.     # (Dis)Allow yourself to load Registry hive files:
  3597.     $success= $Registry->AllowLoad( $bool );
  3598.  
  3599.     # (Dis)Allow yourself to save a Registry key to a hive file:
  3600.     $success= $Registry->AllowSave( $bool );
  3601.  
  3602.     # Save a Registry key to a new hive file:
  3603.     $key->RegSaveKey( "C:/Path/To/Hive/FileName", [] );
  3604.  
  3605. =head3 Other Useful Methods
  3606.  
  3607. See I<Win32API::Registry> for more information on these methods.
  3608. These methods are provided for coding convenience and are
  3609. identical to the I<Win32API::Registry> functions except that these
  3610. don't take a handle to a Registry key, instead getting the handle
  3611. from the invoking object [C<$key>].
  3612.  
  3613.     $key->RegGetKeySecurity( $iSecInfo, $sSecDesc, $lenSecDesc );
  3614.     $key->RegLoadKey( $sSubKeyName, $sPathToFile );
  3615.     $key->RegNotifyChangeKeyValue(
  3616.       $bWatchSubtree, $iNotifyFilter, $hEvent, $bAsync );
  3617.     $key->RegQueryMultipleValues(
  3618.       $structValueEnts, $cntValueEnts, $Buffer, $lenBuffer );
  3619.     $key->RegReplaceKey( $sSubKeyName, $sPathToNewFile, $sPathToBackupFile );
  3620.     $key->RegRestoreKey( $sPathToFile, $iFlags );
  3621.     $key->RegSetKeySecurity( $iSecInfo, $sSecDesc );
  3622.     $key->RegUnLoadKey( $sSubKeyName );
  3623.  
  3624. =head2 Tied Hashes Summary
  3625.  
  3626. For fast learners, this may be the only section you need to read.
  3627. Always append one delimiter to the end of each Registry key name
  3628. and prepend one delimiter to the front of each Registry value name.
  3629.  
  3630. =head3 Opening keys
  3631.  
  3632.     use Win32::TieRegistry ( Delimiter=>"/", ArrayValues=>1 );
  3633.     $Registry->Delimiter("/");                  # Set delimiter to "/".
  3634.     $swKey= $Registry->{"LMachine/Software/"};
  3635.     $winKey= $swKey->{"Microsoft/Windows/CurrentVersion/"};
  3636.     $userKey= $Registry->
  3637.       {"CUser/Software/Microsoft/Windows/CurrentVersion/"};
  3638.     $remoteKey= $Registry->{"//HostName/LMachine/"};
  3639.  
  3640. =head3 Reading values
  3641.  
  3642.     $progDir= $winKey->{"/ProgramFilesDir"};    # "C:\\Program Files"
  3643.     $tip21= $winKey->{"Explorer/Tips//21"};     # Text of tip #21.
  3644.  
  3645.     $winKey->ArrayValues(1);
  3646.     ( $devPath, $type )= $winKey->{"/DevicePath"};
  3647.     # $devPath eq "%SystemRoot%\\inf"
  3648.     # $type eq "REG_EXPAND_SZ"  [if you have SetDualVar.pm installed]
  3649.     # $type == REG_EXPAND_SZ()  [if did C<use Win32::TieRegistry qw(:REG_)>]
  3650.  
  3651. =head3 Setting values
  3652.  
  3653.     $winKey->{"Setup//SourcePath"}= "\\\\SwServer\\SwShare\\Windows";
  3654.     # Simple.  Assumes data type of REG_SZ.
  3655.  
  3656.     $winKey->{"Setup//Installation Sources"}=
  3657.       [ "D:\x00\\\\SwServer\\SwShare\\Windows\0\0", "REG_MULTI_SZ" ];
  3658.     # "\x00" and "\0" used to mark ends of each string and end of list.
  3659.  
  3660.     $winKey->{"Setup//Installation Sources"}=
  3661.       [ ["D:","\\\\SwServer\\SwShare\\Windows"], "REG_MULTI_SZ" ];
  3662.     # Alternate method that is easier to read.
  3663.  
  3664.     $userKey->{"Explorer/Tips//DisplayInitialTipWindow"}=
  3665.       [ pack("L",0), "REG_DWORD" ];
  3666.     $userKey->{"Explorer/Tips//Next"}= [ pack("S",3), "REG_BINARY" ];
  3667.     $userKey->{"Explorer/Tips//Show"}= [ pack("L",0), "REG_BINARY" ];
  3668.  
  3669. =head3 Adding keys
  3670.  
  3671.     $swKey->{"FooCorp/"}= {
  3672.         "FooWriter/" => {
  3673.             "/Version" => "4.032",
  3674.             "Startup/" => {
  3675.                 "/Title" => "Foo Writer Deluxe ][",
  3676.                 "/WindowSize" => [ pack("LL",$wid,$ht), "REG_BINARY" ],
  3677.                 "/TaskBarIcon" => [ "0x0001", "REG_DWORD" ],
  3678.             },
  3679.             "Compatibility/" => {
  3680.                 "/AutoConvert" => "Always",
  3681.                 "/Default Palette" => "Windows Colors",
  3682.             },
  3683.         },
  3684.         "/License", => "0123-9C8EF1-09-FC",
  3685.     };
  3686.  
  3687. =head3 Listing all subkeys and values
  3688.  
  3689.     @members= keys( %{$swKey} );
  3690.     @subKeys= grep(  m#^/#,  keys( %{$swKey->{"Classes/batfile/"}} )  );
  3691.     # @subKeys= ( "/", "/EditFlags" );
  3692.     @valueNames= grep(  ! m#^/#,  keys( %{$swKey->{"Classes/batfile/"}} )  );
  3693.     # @valueNames= ( "DefaultIcon/", "shell/", "shellex/" );
  3694.  
  3695. =head3 Deleting values or keys with no subkeys
  3696.  
  3697.     $oldValue= delete $userKey->{"Explorer/Tips//Next"};
  3698.  
  3699.     $oldValues= delete $userKey->{"Explorer/Tips/"};
  3700.     # $oldValues will be reference to hash containing deleted keys values.
  3701.  
  3702. =head3 Closing keys
  3703.  
  3704.     undef $swKey;               # Explicit way to close a key.
  3705.     $winKey= "Anything else";   # Implicitly closes a key.
  3706.     exit 0;                     # Implicitly closes all keys.
  3707.  
  3708. =head2 Tie::Registry
  3709.  
  3710. This module was originally called I<Tie::Registry>.  Changing code
  3711. that used I<Tie::Registry> over to I<Win32::TieRegistry> is trivial
  3712. as the module name should only be mentioned once, in the C<use>
  3713. line.  However, finding all of the places that used I<Tie::Registry>
  3714. may not be completely trivial so we have included F<Tie/Registry.pm>
  3715. which you can install to provide backward compatibility.
  3716.  
  3717. =head1 AUTHOR
  3718.  
  3719. Tye McQueen.  See http://www.metronet.com/~tye/ or e-mail
  3720. tye@metronet.com with bug reports.
  3721.  
  3722. =head1 SEE ALSO
  3723.  
  3724. I<Win32API::Registry> - Provides access to C<Reg*()>, C<HKEY_*>,
  3725. C<KEY_*>, C<REG_*> [required].
  3726.  
  3727. I<Win32::WinError> - Defines C<ERROR_*> values [optional].
  3728.  
  3729. L<SetDualVar> - For returning C<REG_*> values as combined
  3730. string/integer values [optional].
  3731.  
  3732. =head1 BUGS
  3733.  
  3734. Perl5.004_02 has bugs that make I<Win32::TieRegistry> fail in
  3735. strange and subtle ways.
  3736.  
  3737. Using I<Win32::TieRegistry> with versions of Perl prior to 5.005
  3738. can be tricky or impossible.  Most notes about this have been
  3739. removed from the documentation (they get rather complicated
  3740. and confusing).  This includes references to C<$^E> perhaps not
  3741. being meaningful.
  3742.  
  3743. Because Perl hashes are case sensitive, certain lookups are also
  3744. case sensistive.  In particular, the root keys ("Classes", "CUser",
  3745. "LMachine", "Users", "PerfData", "CConfig", "DynData", and HKEY_*)
  3746. must always be entered without changing between upper and lower
  3747. case letters.  Also, the special rule for matching subkey names
  3748. that contain the user-selected delimiter only works if case is
  3749. matched.  All other key name and value name lookups should be case
  3750. insensitive because the underlying Reg*() calls ignore case.
  3751.  
  3752. Information about each key is cached when using a tied hash.
  3753. This cache is not flushed nor updated when changes are made,
  3754. I<even when the same tied hash is used> to make the changes.
  3755.  
  3756. Current implementations of Perl's "global destruction" phase can
  3757. cause objects returned by C<Load()> to be destroyed while keys
  3758. within the hive are still open, if the objects still exist when
  3759. the script starts to exit.  When this happens, the automatic
  3760. C<UnLoad()> will report a failure and the hive will remain loaded
  3761. in the Registry.
  3762.  
  3763. Trying to C<Load()> a hive file that is located on a remote network
  3764. share may silently delete all data from the hive.  This is a bug
  3765. in the Win32 APIs, not any Perl code or modules.  This module does
  3766. not try to protect you from this bug.
  3767.  
  3768. There is no test suite.
  3769.  
  3770. =head1 FUTURE DIRECTIONS
  3771.  
  3772. The following items are desired by the author and may appear in a
  3773. future release of this module.
  3774.  
  3775. =over
  3776.  
  3777. =item TieValues option
  3778.  
  3779. Currently described in main documentation but no yet implemented.
  3780.  
  3781. =item AutoRefresh option
  3782.  
  3783. Trigger use of C<RegNotifyChangeKeyValue()> to keep tied hash
  3784. caches up-to-date even when other programs make changes.
  3785.  
  3786. =item Error options
  3787.  
  3788. Allow the user to have unchecked calls (calls in a "void context")
  3789. to automatically report errors via C<warn> or C<die>.
  3790.  
  3791. For complex operations, such a copying an entire subtree, provide
  3792. access to detailed information about errors (and perhaps some
  3793. warnings) that were encountered.  Let the user control whether
  3794. the complex operation continues in spite of errors.
  3795.  
  3796. =back
  3797.  
  3798. =cut
  3799.  
  3800. # Autoload not currently supported by Perl under Windows.
  3801.