home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _ec3d16d5dd91eaed6e4f670b8840ca44 < prev    next >
Encoding:
Text File  |  2004-06-01  |  12.3 KB  |  546 lines

  1. package Win32::Registry;
  2.  
  3. =head1 NAME
  4.  
  5. Win32::Registry - accessing the Windows registry [obsolete, use Win32::TieRegistry]
  6.  
  7. =head1 SYNOPSIS
  8.  
  9.     use Win32::Registry;
  10.     my $tips;
  11.     $::HKEY_LOCAL_MACHINE->Open("SOFTWARE\\Microsoft\\Windows"
  12.                                ."\\CurrentVersion\\Explorer\\Tips", $tips)
  13.         or die "Can't open tips: $^E";
  14.     my ($type, $value);
  15.     $tips->QueryValueEx("18", $type, $value) or die "No tip #18: $^E";
  16.     print "Here's a tip: $value\n";
  17.  
  18. =head1 DESCRIPTION
  19.  
  20.     NOTE: This module provides a very klunky interface to access the
  21.     Windows registry, and is not currently being developed actively.  It
  22.     only exists for backward compatibility with old code that uses it.
  23.     For more powerful and flexible ways to access the registry, use
  24.     Win32::TieRegistry.
  25.  
  26. Win32::Registry provides an object oriented interface to the Windows
  27. Registry.
  28.  
  29. The following "root" registry objects are exported to the main:: name
  30. space.  Additional keys must be opened by calling the provided methods
  31. on one of these.
  32.  
  33.     $HKEY_CLASSES_ROOT
  34.     $HKEY_CURRENT_USER
  35.     $HKEY_LOCAL_MACHINE
  36.     $HKEY_USERS
  37.     $HKEY_PERFORMANCE_DATA
  38.     $HKEY_CURRENT_CONFIG
  39.     $HKEY_DYN_DATA
  40.  
  41. =cut
  42.  
  43. use strict;
  44. require Exporter;
  45. require DynaLoader;
  46. use Win32::WinError;
  47.  
  48. use vars qw($VERSION $AUTOLOAD @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  49.  
  50. $VERSION = '0.07';
  51.  
  52. @ISA    = qw( Exporter DynaLoader );
  53. @EXPORT = qw(
  54.     HKEY_CLASSES_ROOT
  55.     HKEY_CURRENT_USER
  56.     HKEY_LOCAL_MACHINE
  57.     HKEY_PERFORMANCE_DATA
  58.     HKEY_CURRENT_CONFIG
  59.     HKEY_DYN_DATA
  60.     HKEY_USERS
  61.     KEY_ALL_ACCESS
  62.     KEY_CREATE_LINK
  63.     KEY_CREATE_SUB_KEY
  64.     KEY_ENUMERATE_SUB_KEYS
  65.     KEY_EXECUTE
  66.     KEY_NOTIFY
  67.     KEY_QUERY_VALUE
  68.     KEY_READ
  69.     KEY_SET_VALUE
  70.     KEY_WRITE
  71.     REG_BINARY
  72.     REG_CREATED_NEW_KEY
  73.     REG_DWORD
  74.     REG_DWORD_BIG_ENDIAN
  75.     REG_DWORD_LITTLE_ENDIAN
  76.     REG_EXPAND_SZ
  77.     REG_FULL_RESOURCE_DESCRIPTOR
  78.     REG_LEGAL_CHANGE_FILTER
  79.     REG_LEGAL_OPTION
  80.     REG_LINK
  81.     REG_MULTI_SZ
  82.     REG_NONE
  83.     REG_NOTIFY_CHANGE_ATTRIBUTES
  84.     REG_NOTIFY_CHANGE_LAST_SET
  85.     REG_NOTIFY_CHANGE_NAME
  86.     REG_NOTIFY_CHANGE_SECURITY
  87.     REG_OPENED_EXISTING_KEY
  88.     REG_OPTION_BACKUP_RESTORE
  89.     REG_OPTION_CREATE_LINK
  90.     REG_OPTION_NON_VOLATILE
  91.     REG_OPTION_RESERVED
  92.     REG_OPTION_VOLATILE
  93.     REG_REFRESH_HIVE
  94.     REG_RESOURCE_LIST
  95.     REG_RESOURCE_REQUIREMENTS_LIST
  96.     REG_SZ
  97.     REG_WHOLE_HIVE_VOLATILE
  98. );
  99.  
  100. @EXPORT_OK = qw(
  101.     RegCloseKey
  102.     RegConnectRegistry
  103.     RegCreateKey
  104.     RegCreateKeyEx
  105.     RegDeleteKey
  106.     RegDeleteValue
  107.     RegEnumKey
  108.     RegEnumValue
  109.     RegFlushKey
  110.     RegGetKeySecurity
  111.     RegLoadKey
  112.     RegNotifyChangeKeyValue
  113.     RegOpenKey
  114.     RegOpenKeyEx
  115.     RegQueryInfoKey
  116.     RegQueryValue
  117.     RegQueryValueEx
  118.     RegReplaceKey
  119.     RegRestoreKey
  120.     RegSaveKey
  121.     RegSetKeySecurity
  122.     RegSetValue
  123.     RegSetValueEx
  124.     RegUnLoadKey
  125. );
  126. $EXPORT_TAGS{ALL}= \@EXPORT_OK;
  127.  
  128. bootstrap Win32::Registry;
  129.  
  130. sub import {
  131.     my $pkg = shift;
  132.     if ($_[0] && "Win32" eq $_[0]) {
  133.     Exporter::export($pkg, "Win32", @EXPORT_OK);
  134.     shift;
  135.     }
  136.     Win32::Registry->export_to_level(1+$Exporter::ExportLevel, $pkg, @_);
  137. }
  138.  
  139. #######################################################################
  140. # This AUTOLOAD is used to 'autoload' constants from the constant()
  141. # XS function.  If a constant is not found then control is passed
  142. # to the AUTOLOAD in AutoLoader.
  143.  
  144. sub AUTOLOAD {
  145.     my($constname);
  146.     ($constname = $AUTOLOAD) =~ s/.*:://;
  147.     #reset $! to zero to reset any current errors.
  148.     local $! = 0;
  149.     my $val = constant($constname, 0);
  150.     if ($! != 0) {
  151.     if ($! =~ /Invalid/) {
  152.         $AutoLoader::AUTOLOAD = $AUTOLOAD;
  153.         goto &AutoLoader::AUTOLOAD;
  154.     }
  155.     else {
  156.         my ($pack,$file,$line) = caller;
  157.         die "Unknown constant $constname in Win32::Registry "
  158.            . "at $file line $line.\n";
  159.     }
  160.     }
  161.     eval "sub $AUTOLOAD { $val }";
  162.     goto &$AUTOLOAD;
  163. }
  164.  
  165. #######################################################################
  166. # _new is a private constructor, not intended for public use.
  167. #
  168.  
  169. sub _new {
  170.     my $self;
  171.     if ($_[0]) {
  172.     $self->{'handle'} = $_[0];
  173.     bless $self;
  174.     }
  175.     $self;
  176. }
  177.  
  178. #define the basic registry objects to be exported.
  179. #these had to be hardwired unfortunately.
  180. # XXX Yuck!
  181.  
  182. {
  183.     package main;
  184.     use vars qw(
  185.         $HKEY_CLASSES_ROOT
  186.         $HKEY_CURRENT_USER
  187.         $HKEY_LOCAL_MACHINE
  188.         $HKEY_USERS
  189.         $HKEY_PERFORMANCE_DATA
  190.         $HKEY_CURRENT_CONFIG
  191.         $HKEY_DYN_DATA
  192.            );
  193. }
  194.  
  195. $::HKEY_CLASSES_ROOT        = _new(&HKEY_CLASSES_ROOT);
  196. $::HKEY_CURRENT_USER        = _new(&HKEY_CURRENT_USER);
  197. $::HKEY_LOCAL_MACHINE        = _new(&HKEY_LOCAL_MACHINE);
  198. $::HKEY_USERS            = _new(&HKEY_USERS);
  199. $::HKEY_PERFORMANCE_DATA    = _new(&HKEY_PERFORMANCE_DATA);
  200. $::HKEY_CURRENT_CONFIG        = _new(&HKEY_CURRENT_CONFIG);
  201. $::HKEY_DYN_DATA        = _new(&HKEY_DYN_DATA);
  202.  
  203. =head2 Methods
  204.  
  205. The following methods are supported.  Note that subkeys can be
  206. specified as a path name, separated by backslashes (which may
  207. need to be doubled if you put them in double quotes).
  208.  
  209. =over 8
  210.  
  211. =item Open
  212.  
  213.     $reg_obj->Open($sub_key_name, $sub_reg_obj);
  214.  
  215. Opens a subkey of a registry object, returning the new registry object
  216. in $sub_reg_obj.
  217.  
  218. =cut
  219.  
  220. sub Open {
  221.     my $self = shift;
  222.     die 'usage: $obj->Open($sub_key_name, $sub_reg_obj)' if @_ != 2;
  223.     
  224.     my ($subkey) = @_;
  225.     my ($result,$subhandle);
  226.  
  227.     $result = RegOpenKey($self->{'handle'},$subkey,$subhandle);
  228.     $_[1] = _new($subhandle);
  229.     
  230.     return 0 unless $_[1];
  231.     $! = Win32::GetLastError() unless $result;
  232.     return $result;
  233. }
  234.  
  235. =item Close
  236.  
  237.     $reg_obj->Close();
  238.  
  239. Closes an open registry key.
  240.  
  241. =cut
  242.  
  243. sub Close {
  244.     my $self = shift;
  245.     die 'usage: $obj->Close()' if @_ != 0;
  246.  
  247.     return unless exists $self->{'handle'};
  248.     my $result = RegCloseKey($self->{'handle'});
  249.     if ($result) {
  250.     delete $self->{'handle'};
  251.     }
  252.     else {
  253.     $! = Win32::GetLastError();
  254.     }
  255.     return $result;
  256. }
  257.  
  258. sub DESTROY {
  259.     my $self = shift;
  260.     return unless exists $self->{'handle'};
  261.     RegCloseKey($self->{'handle'});
  262.     delete $self->{'handle'};
  263. }
  264.  
  265.  
  266. =item Connect
  267.  
  268.     $reg_obj->Connect($node_name, $new_reg_obj);
  269.  
  270. Connects to a remote Registry on the node specified by $node_name,
  271. returning it in $new_reg_obj.  Returns false if it fails.
  272.  
  273. =cut
  274.  
  275. sub Connect {
  276.     my $self = shift;
  277.     die 'usage: $obj->Connect($node_name, $new_reg_obj)' if @_ != 2;
  278.      
  279.     my ($node) = @_;
  280.     my ($result,$subhandle);
  281.  
  282.     $result = RegConnectRegistry ($node, $self->{'handle'}, $subhandle);
  283.     $_[1] = _new($subhandle);
  284.  
  285.     return 0 unless $_[1];
  286.     $! = Win32::GetLastError() unless $result;
  287.     return $result;
  288. }  
  289.  
  290. =item Create
  291.  
  292.     $reg_obj->Create($sub_key_name, $new_reg_obj);
  293.  
  294. Opens the subkey specified by $sub_key_name, returning the new registry
  295. object in $new_reg_obj.  If the specified subkey doesn't exist, it is
  296. created.
  297.  
  298. =cut
  299.  
  300. sub Create {
  301.     my $self = shift;
  302.     die 'usage: $obj->Create($sub_key_name, $new_reg_obj)' if @_ != 2;
  303.  
  304.     my ($subkey) = @_;
  305.     my ($result,$subhandle);
  306.  
  307.     $result = RegCreateKey($self->{'handle'},$subkey,$subhandle);
  308.     $_[1] = _new ($subhandle);
  309.  
  310.     return 0 unless $_[1];
  311.     $! = Win32::GetLastError() unless $result;
  312.     return $result;
  313. }
  314.  
  315. =item SetValue
  316.  
  317.     $reg_obj->SetValue($sub_key_name, $type, $value);
  318.  
  319. Sets the default value for a subkey specified by $sub_key_name.
  320.  
  321. =cut
  322.  
  323. sub SetValue {
  324.     my $self = shift;
  325.     die 'usage: $obj->SetValue($subkey, $type, $value)' if @_ != 3;
  326.     my $result = RegSetValue($self->{'handle'}, @_);
  327.     $! = Win32::GetLastError() unless $result;
  328.     return $result;
  329. }
  330.  
  331. =item SetValueEx
  332.  
  333.     $reg_obj->SetValueEx($value_name, $reserved, $type, $value);
  334.  
  335. Sets the value for the value name identified by $value_name
  336. in the key specified by $reg_obj.
  337.  
  338. =cut
  339.  
  340. sub SetValueEx {
  341.     my $self = shift;
  342.     die 'usage: $obj->SetValueEx($value_name, $reserved, $type, $value)' if @_ != 4;
  343.     my $result = RegSetValueEx($self->{'handle'}, @_);
  344.     $! = Win32::GetLastError() unless $result;
  345.     return $result;
  346. }
  347.  
  348. =item QueryValue
  349.  
  350.     $reg_obj->QueryValue($sub_key_name, $value);
  351.  
  352. Gets the default value of the subkey specified by $sub_key_name.
  353.  
  354. =cut
  355.  
  356. sub QueryValue {
  357.     my $self = shift;
  358.     die 'usage: $obj->QueryValue($sub_key_name, $value)' if @_ != 2;
  359.     my $result = RegQueryValue($self->{'handle'}, $_[0], $_[1]);
  360.     $! = Win32::GetLastError() unless $result;
  361.     return $result;
  362. }
  363.  
  364. =item QueryKey
  365.  
  366.     $reg_obj->QueryKey($classref, $number_of_subkeys, $number_of_values);
  367.  
  368. Gets information on a key specified by $reg_obj.
  369.  
  370. =cut
  371.  
  372. sub QueryKey {
  373.     my $garbage;
  374.     my $self = shift;
  375.     die 'usage: $obj->QueryKey($classref, $number_of_subkeys, $number_of_values)'
  376.         if @_ != 3;
  377.  
  378.     my $result = RegQueryInfoKey($self->{'handle'}, $_[0],
  379.                      $garbage, $garbage, $_[1],
  380.                      $garbage, $garbage, $_[2],
  381.                      $garbage, $garbage, $garbage, $garbage);
  382.  
  383.     $! = Win32::GetLastError() unless $result;
  384.     return $result;
  385. }
  386.  
  387. =item QueryValueEx
  388.  
  389.     $reg_obj->QueryValueEx($value_name, $type, $value);
  390.  
  391. Gets the value for the value name identified by $value_name
  392. in the key specified by $reg_obj.
  393.  
  394. =cut
  395.  
  396. sub QueryValueEx {
  397.     my $self = shift;
  398.     die 'usage: $obj->QueryValueEx($value_name, $type, $value)' if @_ != 3;
  399.     my $result = RegQueryValueEx($self->{'handle'}, $_[0], undef, $_[1], $_[2]);
  400.     $! = Win32::GetLastError() unless $result;
  401.     return $result;
  402. }
  403.  
  404. =item GetKeys
  405.  
  406.     my @keys;
  407.     $reg_obj->GetKeys(\@keys);
  408.  
  409. Populates the supplied array reference with the names of all the keys
  410. within the registry object $reg_obj.
  411.  
  412. =cut
  413.  
  414. sub GetKeys {
  415.     my $self = shift;
  416.     die 'usage: $obj->GetKeys($arrayref)' if @_ != 1 or ref($_[0]) ne 'ARRAY';
  417.  
  418.     my ($result, $i, $keyname);
  419.     $keyname = "DummyVal";
  420.     $i = 0;
  421.     $result = 1;
  422.     
  423.     while ( $result ) {
  424.     $result = RegEnumKey( $self->{'handle'},$i++, $keyname );
  425.     if ($result) {
  426.         push( @{$_[0]}, $keyname );
  427.     }
  428.     }
  429.     return(1);
  430. }
  431.  
  432. =item GetValues
  433.  
  434.     my %values;
  435.     $reg_obj->GetValues(\%values);
  436.  
  437. Populates the supplied hash reference with entries of the form
  438.  
  439.     $value_name => [ $value_name, $type, $data ]
  440.  
  441. for each value in the registry object $reg_obj.
  442.  
  443. =cut
  444.  
  445. sub GetValues {
  446.     my $self = shift;
  447.     die 'usage: $obj->GetValues($hashref)' if @_ != 1;
  448.  
  449.     my ($result,$name,$type,$data,$i);
  450.     $name = "DummyVal";
  451.     $i = 0;
  452.     while ( $result=RegEnumValue( $self->{'handle'},
  453.                   $i++,
  454.                   $name,
  455.                   undef,
  456.                   $type,
  457.                   $data ))
  458.     {
  459.     $_[0]->{$name} = [ $name, $type, $data ];
  460.     }
  461.     return(1);
  462. }
  463.  
  464. =item DeleteKey
  465.  
  466.     $reg_obj->DeleteKey($sub_key_name);
  467.  
  468. Deletes a subkey specified by $sub_key_name from the registry.
  469.  
  470. =cut
  471.  
  472. sub DeleteKey {
  473.     my $self = shift;
  474.     die 'usage: $obj->DeleteKey($sub_key_name)' if @_ != 1;
  475.     my $result = RegDeleteKey($self->{'handle'}, @_);
  476.     $! = Win32::GetLastError() unless $result;
  477.     return $result;
  478. }
  479.  
  480. =item DeleteValue
  481.  
  482.     $reg_obj->DeleteValue($value_name);
  483.  
  484. Deletes a value identified by $value_name from the registry.
  485.  
  486. =cut
  487.  
  488. sub DeleteValue {
  489.     my $self = shift;
  490.     die 'usage: $obj->DeleteValue($value_name)' if @_ != 1;
  491.     my $result = RegDeleteValue($self->{'handle'}, @_);
  492.     $! = Win32::GetLastError() unless $result;
  493.     return $result;
  494. }
  495.  
  496. =item Save
  497.  
  498.     $reg_obj->Save($filename);
  499.  
  500. Saves the hive specified by $reg_obj to a file.
  501.  
  502. =cut
  503.  
  504. sub Save {
  505.     my $self = shift;
  506.     die 'usage: $obj->Save($filename)' if @_ != 1;
  507.     my $result = RegSaveKey($self->{'handle'}, @_);
  508.     $! = Win32::GetLastError() unless $result;
  509.     return $result;
  510. }
  511.  
  512. =item Load
  513.  
  514.     $reg_obj->Load($sub_key_name, $file_name);
  515.  
  516. Loads a key specified by $sub_key_name from a file.
  517.  
  518. =cut
  519.  
  520. sub Load {
  521.     my $self = shift;
  522.     die 'usage: $obj->Load($sub_key_name, $file_name)' if @_ != 2;
  523.     my $result = RegLoadKey($self->{'handle'}, @_);
  524.     $! = Win32::GetLastError() unless $result;
  525.     return $result;
  526. }
  527.  
  528. =item UnLoad
  529.  
  530.     $reg_obj->Unload($sub_key_name);
  531.  
  532. Unloads a registry hive.
  533.  
  534. =cut
  535.  
  536. sub UnLoad {
  537.     my $self = shift;
  538.     die 'usage: $obj->UnLoad($sub_key_name)' if @_ != 1;
  539.     my $result = RegUnLoadKey($self->{'handle'}, @_);
  540.     $! = Win32::GetLastError() unless $result;
  541.     return $result;
  542. }
  543.  
  544. 1;
  545. __END__
  546.