home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / lib / site / Win32 / Registry.pm < prev    next >
Encoding:
Perl POD Document  |  1997-08-10  |  9.5 KB  |  488 lines

  1. package Win32::Registry;
  2. #######################################################################
  3. #Perl Module for Registry Extensions
  4. # This module creates an object oriented interface to the Win32
  5. # Registry.
  6. #
  7. # NOTE: This package exports four instantiated keys to
  8. # the main:: name space.  ( The pre-defined keys )
  9. # these keys:
  10. # $main::CLASSES_ROOT
  11. # $main::CURRENT_USER
  12. # $main::LOCAL_MACHINE
  13. # $main::USERS
  14. #
  15. #######################################################################
  16.  
  17. require Exporter;       #to export the constants to the main:: space
  18. require DynaLoader;     # to dynuhlode the module.
  19. use Win32::WinError;         # for windows constants.
  20.  
  21. $VERSION = '0.01';
  22.  
  23. @ISA= qw( Exporter DynaLoader );
  24. @EXPORT = qw(
  25.     HKEY_CLASSES_ROOT
  26.     HKEY_CURRENT_USER
  27.     HKEY_LOCAL_MACHINE
  28.     HKEY_PERFORMANCE_DATA
  29.     HKEY_PERFORMANCE_NLSTEXT
  30.     HKEY_PERFORMANCE_TEXT
  31.     HKEY_USERS
  32.     KEY_ALL_ACCESS
  33.     KEY_CREATE_LINK
  34.     KEY_CREATE_SUB_KEY
  35.     KEY_ENUMERATE_SUB_KEYS
  36.     KEY_EXECUTE
  37.     KEY_NOTIFY
  38.     KEY_QUERY_VALUE
  39.     KEY_READ
  40.     KEY_SET_VALUE
  41.     KEY_WRITE
  42.     REG_BINARY
  43.     REG_CREATED_NEW_KEY
  44.     REG_DWORD
  45.     REG_DWORD_BIG_ENDIAN
  46.     REG_DWORD_LITTLE_ENDIAN
  47.     REG_EXPAND_SZ
  48.     REG_FULL_RESOURCE_DESCRIPTOR
  49.     REG_LEGAL_CHANGE_FILTER
  50.     REG_LEGAL_OPTION
  51.     REG_LINK
  52.     REG_MULTI_SZ
  53.     REG_NONE
  54.     REG_NOTIFY_CHANGE_ATTRIBUTES
  55.     REG_NOTIFY_CHANGE_LAST_SET
  56.     REG_NOTIFY_CHANGE_NAME
  57.     REG_NOTIFY_CHANGE_SECURITY
  58.     REG_OPENED_EXISTING_KEY
  59.     REG_OPTION_BACKUP_RESTORE
  60.     REG_OPTION_CREATE_LINK
  61.     REG_OPTION_NON_VOLATILE
  62.     REG_OPTION_RESERVED
  63.     REG_OPTION_VOLATILE
  64.     REG_REFRESH_HIVE
  65.     REG_RESOURCE_LIST
  66.     REG_RESOURCE_REQUIREMENTS_LIST
  67.     REG_SZ
  68.     REG_WHOLE_HIVE_VOLATILE
  69. );
  70.  
  71. #######################################################################
  72. # This AUTOLOAD is used to 'autoload' constants from the constant()
  73. # XS function.  If a constant is not found then control is passed
  74. # to the AUTOLOAD in AutoLoader.
  75.  
  76. sub AUTOLOAD {
  77.     my($constname);
  78.     ($constname = $AUTOLOAD) =~ s/.*:://;
  79.     #reset $! to zero to reset any current errors.
  80.     $!=0;
  81.     my $val = constant($constname, @_ ? $_[0] : 0);
  82.     if ($! != 0) {
  83.     if ($! =~ /Invalid/) {
  84.         $AutoLoader::AUTOLOAD = $AUTOLOAD;
  85.         goto &AutoLoader::AUTOLOAD;
  86.     }
  87.     else {
  88.         ($pack,$file,$line) = caller;
  89.         die "Your vendor has not defined Win32::Registry macro $constname, used at $file line $line.";
  90.     }
  91.     }
  92.     eval "sub $AUTOLOAD { $val }";
  93.     goto &$AUTOLOAD;
  94. }
  95.  
  96. #######################################################################
  97. # _new is a private constructor, not intended for public use.
  98. #
  99.  
  100. sub show_me
  101. {
  102.     $self=shift;
  103.     print $self->{'handle'};
  104. }
  105.  
  106. sub _new
  107. {
  108.     my $self={};
  109.     if ($_[0]){
  110.         $self->{'handle'} = $_[0];
  111.         bless $self
  112.         }
  113.     else{
  114.             undef($self);
  115.     }
  116.     $self;
  117. }
  118.  
  119. #define the basic registry objects to be exported.
  120. #these had to be hardwired unfortunately.
  121.  
  122.  
  123. $main::HKEY_CLASSES_ROOT = _new(0x80000000);
  124. $main::HKEY_CURRENT_USER = _new(0x80000001);
  125. $main::HKEY_LOCAL_MACHINE = _new(0x80000002);
  126. $main::HKEY_USERS = _new(0x80000003);
  127. $main::HKEY_PERFORMANCE_DATA = _new(0x80000004 );
  128. $main::HKEY_PERFORMANCE_TEXT =_new(0x80000050 );
  129. $main::HKEY_PERFORMANCE_NLSTEXT =_new(0x80000060 );
  130.  
  131.  
  132.  
  133.  
  134.  
  135. #######################################################################
  136. #Open: creates a new Registry object from an existing one.
  137. # usage: $RegObj->Open( "SubKey",$SubKeyObj );
  138. #               $SubKeyObj->Open( "SubberKey", *SubberKeyObj );
  139.  
  140. sub Open
  141. {
  142.     my $self = shift;
  143.     
  144.     if( $#_ != 1 ){
  145.         die 'usage: Open( $SubKey, $ObjRef )';
  146.     }
  147.     
  148.     ($SubKey) = @_;
  149.     local ($Result,$SubHandle);
  150.  
  151.     $Result = RegOpenKey($self->{'handle'},$SubKey,$SubHandle);
  152.     $_[1] = _new( $SubHandle );
  153.     
  154.     if (!$_[1] ){
  155.         return 0;
  156.     }
  157.  
  158.      if(!$Result){
  159.         $! = Win32::GetLastError();
  160.     }
  161.  
  162.     # return a boolean value
  163.     return($Result);
  164.  
  165. }
  166.  
  167. #######################################################################
  168. #Close
  169. # close an open registry key.
  170. #
  171. sub Close
  172. {
  173.     my $self = shift;
  174.     
  175.     if( $#_ != -1 ){
  176.         die "usage: Close()";
  177.     }
  178.  
  179.     $Result = RegCloseKey( $self->{'handle'});
  180.     undef($self);
  181.  
  182.     if(!$Result){
  183.         $! = Win32::GetLastError();
  184.     }
  185.  
  186.     return($Result);
  187. }
  188.  
  189.  
  190. #######################################################################
  191. #Create
  192. # open a subkey.  If it doesn't exist, create it.
  193. #
  194.  
  195. sub Create
  196. {
  197.     my $self = shift;
  198.  
  199.     if($#_ != 1 ){
  200.         die 'usage: Create( $SubKey,$ScalarRef )';
  201.     }
  202.  
  203.     ($SubKey) = @_;
  204.     local ($Result,$SubHandle);
  205.  
  206.     #call the API, and create the object.
  207.     $Result = RegCreateKey($self->{'handle'},$SubKey,$SubHandle);
  208.     $_[1] = _new ( $SubHandle );
  209.     if (!$_[1]){
  210.         return(0);
  211.     }
  212.     #error checking
  213.  
  214.      if(!$Result){
  215.         $! = Win32::GetLastError();
  216.     }
  217.  
  218.     return($Result);
  219.  
  220. }
  221.  
  222. #######################################################################
  223. #SetValue
  224. # SetValue sets a value in the current key.
  225. #
  226.  
  227. sub SetValue
  228. {
  229.     my $self = shift;
  230.     if($#_ != 2 ){
  231.         die 'usage: SetValue($SubKey,$Type,$value )';
  232.     }
  233.  
  234.     local($SubKey,$type,$value) = @_;
  235.  
  236.     # set the value.
  237.     $Result = RegSetValue( $self->{'handle'},$SubKey,$type,$value);
  238.     
  239.      if(!$Result){
  240.         $! = Win32::GetLastError();
  241.     }
  242.  
  243.     return($Result);
  244.  
  245. }
  246.  
  247. sub SetValueEx
  248. {
  249.     my $self = shift;
  250.     if($#_ != 3){
  251.         die 'usage: SetValueEx( $SubKey,$Reserved,$type,$value )';
  252.     }
  253.  
  254.     local( $SubKey,$Reserved,$type,$value) =@_;
  255.  
  256.     $Result = RegSetValueEx( $self->{'handle'},$SubKey,$Reserved,$type,$value);
  257.     
  258.     if(!$Result){
  259.         $! = Win32::GetLastError();
  260.     }
  261.  
  262.     return($Result);
  263. }
  264.  
  265. #######################################################################
  266. #QueryValue  and QueryKey
  267. # QueryValue gets information on a value in the current key.
  268. # QueryKey "    "       "       "  key  "       "       "       
  269.  
  270. sub QueryValue
  271. {
  272.     my $self = shift;
  273.  
  274.     if($#_ != 1 ){
  275.         die 'usage: QueryValue( $SubKey,$valueref )';
  276.     }
  277.  
  278.     #Query the value.
  279.     $Result = RegQueryValue( $self->{'handle'}, $_[0], $_[1]);
  280.  
  281.     #check the results.
  282.  
  283.      if(!$Result){
  284.         $! = Win32::GetLastError();
  285.     }
  286.  
  287.     return($Result);
  288. }
  289.  
  290. sub QueryKey
  291. {
  292.     my $garbage;
  293.     my $self = shift;
  294.  
  295.     if($#_ != 2 ){
  296.         die 'usage: QueryKey( $classref, $numberofSubkeys, $numberofVals )';
  297.     }
  298.  
  299.     local ($Result);
  300.  
  301.     $Result = RegQueryInfoKey( $self->{'handle'}, $_[0], $garbage, $_[1],
  302.                    $garbage, $garbage, $_[2],
  303.                    $garbage, $garbage, $garbage, $garbage);
  304.  
  305.  
  306.      if(!$Result){
  307.         $! = Win32::GetLastError();
  308.     }
  309.     return($Result);
  310. }
  311.  
  312. #######################################################################
  313. #GetKeys
  314. #Note: the list object must be passed by reference: 
  315. #       $myobj->GetKeys( \@mylist )
  316. sub GetKeys
  317. {
  318.     my $self = shift;
  319.     if($#_ != 0 ){
  320.         die 'usage: GetKeys( $arrayref )';
  321.     }
  322.  
  323.     if (ref $_[0] ne ARRAY){
  324.         die "GetKeys requires a list reference as an arguement";
  325.     }
  326.  
  327.     local ($Result,$ValueName,$i,$keyname);
  328.  
  329.     $ValueName="DummyVal";$i=0;
  330.     $Result = 1;
  331.     
  332.     while( $Result ){
  333.         $Result = RegEnumKey( $self->{'handle'},$i++, $keyname );
  334.         if ($Result){
  335.             push( @{$_[0]}, $keyname );
  336.         }
  337.     }
  338.     return(1);
  339.  
  340. }
  341. #######################################################################
  342. #GetValues
  343. # GetValues creates a hash containing 'name'=> ( name,type,data )
  344. # for each value in the current key.
  345.  
  346. sub GetValues
  347. {
  348.     my $self = shift;
  349.  
  350.     if($#_ != 0 ){
  351.         die 'usage: GetValues( $hashref )';
  352.     }
  353.  
  354.     local ($Result,$ValueName,$i);
  355.  
  356.     $ValueName="DummyVal";$i=0;
  357.     while( $Result=RegEnumValue( $self->{'handle'},
  358.                     $i++,
  359.                     $ValueName,
  360.                     NULL,
  361.                     $ValueType,
  362.                     $ValueData )){
  363.  
  364.         $aref = [ $ValueName, $ValueType,$ValueData ];
  365.  
  366.         $_[0]->{$ValueName} = $aref;
  367.     }
  368.         
  369.     return(1);
  370. }
  371.  
  372. #######################################################################
  373. #DeleteKey
  374. # delete a key from the registry.
  375. #  eg: $CLASSES_ROOT->DeleteKey( "KeyNameToDelete");
  376. #
  377.  
  378. sub DeleteKey
  379. {
  380.     my $self = shift;
  381.     local($Result);
  382.     if($#_ != 0 ){
  383.         die 'usage: DeleteKey( $SubKey )';
  384.     }
  385.  
  386.     local( $name ) = @_;
  387.  
  388.     $Result=RegDeleteKey($self->{'handle'},$name);
  389.  
  390.      if(!$Result){
  391.         $! = Win32::GetLastError();
  392.     }
  393.     return($Result);
  394.  
  395. }
  396. #######################################################################
  397. #DeleteValue
  398. # delete a value from the current key in the registry
  399. #  $CLASSES_ROOT->DeleteValue( "\000" );
  400.  
  401. sub DeleteValue
  402. {
  403.     my $self = shift;
  404.     local( $Result );
  405.  
  406.     if($#_ != 0 ){
  407.         die 'usage: DeleteValue( $SubKey )';
  408.     }
  409.  
  410.     local( $name )=@_;
  411.     
  412.     $Result=RegDeleteValue( $self->{'handle'},$name);
  413.     
  414.     if( !$Result){
  415.         $!=Win32::GetLastError();
  416.     }
  417.  
  418.     return($Result);
  419.  
  420. }
  421.  
  422. #######################################################################
  423. #save
  424. #saves the current hive to a file.
  425. #
  426.  
  427. sub Save
  428. {
  429.     my $self=shift;
  430.  
  431.     if($#_ != 0 ){
  432.         die 'usage: Save( $FileName )';
  433.     }
  434.  
  435.     local( $FileName ) = @_;
  436.  
  437.     $Result=RegSaveKey( $self->{'handle'},$FileName );
  438.  
  439.     if( !$Result){
  440.         $!=Win32::GetLastError();
  441.     }
  442.  
  443.     return($Result);
  444. }
  445.  
  446. #######################################################################
  447. #Load
  448. #loads a saved key from a file.
  449.  
  450. sub Load
  451. {
  452.     my $self = shift;
  453.     if($#_ != 1 ){
  454.         die 'usage: Load( $SubKey,$FileName )';
  455.     }
  456.  
  457.     local( $SubKey,$FileName) = @_;
  458.  
  459.     $Result=RegLoadKey( $self->{'handle'},$SubKey,$FileName);
  460.  
  461.     if( !$Result){
  462.         $!=Win32::GetLastError();
  463.     }
  464.  
  465.     return($Result);
  466. }
  467. #######################################################################
  468. # dynamically load in the Registry.pll module.
  469.  
  470.  
  471. bootstrap Win32::Registry;
  472.  
  473. # Preloaded methods go here.
  474.  
  475. #Currently Autoloading is not implemented in Perl for win32
  476. # Autoload methods go after __END__, and are processed by the autosplit program.
  477.  
  478. 1;
  479. __END__
  480.  
  481.  
  482.  
  483.  
  484.  
  485.     
  486.  
  487.     
  488.