Belmonts has asked for the wisdom of the Perl Monks concerning the following question:

Hello all! I have been playing with TieRegistry lately and keep running into a "problem" with the Load() function. It seems that TieRegistry automatically UnLoads the hive that I have loaded after the script has completed. I am wanting to keep the hive open so that numerous other processes can use the hive after it has been loaded. Anyone know how to do this? Here is what I have so far:
use strict; use Win32::TieRegistry; my $newSubKey='TEMPHIVE'; my $thefile = 'C://Documents and Settings//DefaultUser//ntuser.dat'; # Specify Hive my $key= $Registry->{ 'HKEY_USERS' }; # Allow access to load hives my $optValue = 'true'; my $oldOpts= $key->SetOptions( AllowLoad=>$optValue ); # Load the hive my $newKey = $key->Load( $thefile, $newSubKey ); print $^E; #prints error description, if any. # GetValues in TEMPHIVE my $key = $Registry->{ 'HKEY_USERS\TEMPHIVE\SOFTWARE\Microsoft\Windows +\CurrentVersion\Run' }; my $sValueName ='TEST'; my $ValueData = $key->GetValue( $sValueName ); print "\n $ValueData \n"; #prints value of TEST from loaded hive
Upon completion of the script the hive is unloaded automatically. How do I keep TieRegistry from unloading the hive? Thanks! -Belmont

Replies are listed 'Best First'.
Re: Win32::TieRegistry and Load() (hack)
by tye (Sage) on Mar 11, 2004 at 18:34 UTC

    When this type of question comes up, I find I'm rather quick to "use the source".

    sub Load { my $this= shift(@_); my $tied= ref($this) && tied(%$this); $this= tied(%$this) if $tied; my( $file, $subKey, $opts )= @_; if( 2 == @_ && "HASH" eq ref($subKey) ) { $opts= $subKey; undef $subKey; } @_ < 1 || 3 < @_ || defined($opts) && "HASH" ne ref($opts) and croak "Usage: \$key= ", "${PACK}->Load( \$fileName, [\$newSubKey,] {OPT=>VAL...} +);\n", " options: @Load_Opts @new_Opts\nCalled"; if( defined($opts) && exists($opts->{NewSubKey}) ) { $subKey= delete $opts->{NewSubKey}; } if( ! defined( $subKey ) ) { if( "" ne $this->Machine ) { ( $this )= $this->_connect( [$this->Machine,"LMachine"] ); } else { ( $this )= $this->_rootKey( "LMachine" ); # Could also +be "Users" } $subKey= "PerlTie:$$." . ++$Load_Cnt; } $this->RegLoadKey( $subKey, $file ) or return (); my $self= $this->new( $subKey, defined($opts) ? $opts : () ); if( ! defined( $self ) ) { { my $err= Win32::GetLastError(); #{ local( $^E ); #} $this->RegUnLoadKey( $subKey ) or carp "Can't unload $subKey from ", $this->Path, ": ", _ErrMsg +, "\n"; Win32::SetLastError($err); } return (); } $self->{UNLOADME}= [ $this, $subKey, $file ]; $self= $self->TiedRef if $tied; return $self; }

    Which boils down to a whole lot of error checking and DWIM and other nonsense and

    $this->RegLoadKey( $subKey, $file )

    so just do that. See Win32API::Registry for more on this method (note that you can use nearly any Win32API::Registry method directly on a Win32::TieRegistry object, as documented).

    - tye        

      Thanks for the tip, and I have it working now. I am basically using the same Load subroutine as TieRegistry uses without the  $self->[UNLOADME]=... Thanks again...I'll get into the habit of digging through the modules before posting questions like this again :) -Belmont