Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3800 lines
124 KiB

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