home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Web Server / Savant.exe / disk1 / data1.cab / Perl5 / perl5 / lib / Win32 / registry.pm < prev    next >
Encoding:
Perl POD Document  |  2001-02-23  |  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.  
  22. @ISA= qw( Exporter DynaLoader );
  23. @EXPORT = qw(
  24.     HKEY_CLASSES_ROOT
  25.     HKEY_CURRENT_USER
  26.     HKEY_LOCAL_MACHINE
  27.     HKEY_PERFORMANCE_DATA
  28.     HKEY_PERFORMANCE_NLSTEXT
  29.     HKEY_PERFORMANCE_TEXT
  30.     HKEY_USERS
  31.     KEY_ALL_ACCESS
  32.     KEY_CREATE_LINK
  33.     KEY_CREATE_SUB_KEY
  34.     KEY_ENUMERATE_SUB_KEYS
  35.     KEY_EXECUTE
  36.     KEY_NOTIFY
  37.     KEY_QUERY_VALUE
  38.     KEY_READ
  39.     KEY_SET_VALUE
  40.     KEY_WRITE
  41.     REG_BINARY
  42.     REG_CREATED_NEW_KEY
  43.     REG_DWORD
  44.     REG_DWORD_BIG_ENDIAN
  45.     REG_DWORD_LITTLE_ENDIAN
  46.     REG_EXPAND_SZ
  47.     REG_FULL_RESOURCE_DESCRIPTOR
  48.     REG_LEGAL_CHANGE_FILTER
  49.     REG_LEGAL_OPTION
  50.     REG_LINK
  51.     REG_MULTI_SZ
  52.     REG_NONE
  53.     REG_NOTIFY_CHANGE_ATTRIBUTES
  54.     REG_NOTIFY_CHANGE_LAST_SET
  55.     REG_NOTIFY_CHANGE_NAME
  56.     REG_NOTIFY_CHANGE_SECURITY
  57.     REG_OPENED_EXISTING_KEY
  58.     REG_OPTION_BACKUP_RESTORE
  59.     REG_OPTION_CREATE_LINK
  60.     REG_OPTION_NON_VOLATILE
  61.     REG_OPTION_RESERVED
  62.     REG_OPTION_VOLATILE
  63.     REG_REFRESH_HIVE
  64.     REG_RESOURCE_LIST
  65.     REG_RESOURCE_REQUIREMENTS_LIST
  66.     REG_SZ
  67.     REG_WHOLE_HIVE_VOLATILE
  68. );
  69.  
  70. #######################################################################
  71. # This AUTOLOAD is used to 'autoload' constants from the constant()
  72. # XS function.  If a constant is not found then control is passed
  73. # to the AUTOLOAD in AutoLoader.
  74.  
  75. sub AUTOLOAD {
  76.     my($constname);
  77.     ($constname = $AUTOLOAD) =~ s/.*:://;
  78.     #reset $! to zero to reset any current errors.
  79.     $!=0;
  80.     my $val = constant($constname, @_ ? $_[0] : 0);
  81.     if ($! != 0) {
  82.     if ($! =~ /Invalid/) {
  83.         $AutoLoader::AUTOLOAD = $AUTOLOAD;
  84.         goto &AutoLoader::AUTOLOAD;
  85.     }
  86.     else {
  87.         ($pack,$file,$line) = caller;
  88.         die "Your vendor has not defined Win32::Registry macro $constname, used at $file line $line.";
  89.     }
  90.     }
  91.     eval "sub $AUTOLOAD { $val }";
  92.     goto &$AUTOLOAD;
  93. }
  94.  
  95. #######################################################################
  96. # _new is a private constructor, not intended for public use.
  97. #
  98.  
  99. sub show_me
  100. {
  101.     $self=shift;
  102.     print $self->{'handle'};
  103. }
  104.  
  105. sub _new
  106. {
  107.     my $self={};
  108.     if ($_[0]){
  109.         $self->{'handle'} = $_[0];
  110.         bless $self
  111.         }
  112.     else{
  113.             undef($self);
  114.     }
  115.     $self;
  116. }
  117.  
  118. #define the basic registry objects to be exported.
  119. #these had to be hardwired unfortunately.
  120.  
  121.  
  122. $main::HKEY_CLASSES_ROOT = _new(0x80000000);
  123. $main::HKEY_CURRENT_USER = _new(0x80000001);
  124. $main::HKEY_LOCAL_MACHINE = _new(0x80000002);
  125. $main::HKEY_USERS = _new(0x80000003);
  126. $main::HKEY_PERFORMANCE_DATA = _new(0x80000004 );
  127. $main::HKEY_CURRENT_CONFIG = _new(0x80000005 );
  128. $main::HKEY_DYN_DATA = _new(0x80000006 );
  129.  
  130.  
  131.  
  132.  
  133.  
  134. #######################################################################
  135. #Open: creates a new Registry object from an existing one.
  136. # usage: $RegObj->Open( "SubKey",$SubKeyObj );
  137. #               $SubKeyObj->Open( "SubberKey", *SubberKeyObj );
  138.  
  139. sub Open
  140. {
  141.     my $self = shift;
  142.     
  143.     if( $#_ != 1 ){
  144.         die 'usage: Open( $SubKey, $ObjRef )';
  145.     }
  146.     
  147.     ($SubKey) = @_;
  148.     local ($Result,$SubHandle);
  149.  
  150.     $Result = RegOpenKey($self->{'handle'},$SubKey,$SubHandle);
  151.     $_[1] = _new( $SubHandle );
  152.     
  153.     if (!$_[1] ){
  154.         return 0;
  155.     }
  156.  
  157.      if(!$Result){
  158.         $! = Win32::GetLastError();
  159.     }
  160.  
  161.     # return a boolean value
  162.     return($Result);
  163.  
  164. }
  165.  
  166. #######################################################################
  167. #Close
  168. # close an open registry key.
  169. #
  170. sub Close
  171. {
  172.     my $self = shift;
  173.     
  174.     if( $#_ != -1 ){
  175.         die "usage: Close()";
  176.     }
  177.  
  178.     $Result = RegCloseKey( $self->{'handle'});
  179.     undef($self);
  180.  
  181.     if(!$Result){
  182.         $! = Win32::GetLastError();
  183.     }
  184.  
  185.     return($Result);
  186. }
  187.  
  188.  
  189. #######################################################################
  190. #Create
  191. # open a subkey.  If it doesn't exist, create it.
  192. #
  193.  
  194. sub Create
  195. {
  196.     my $self = shift;
  197.  
  198.     if($#_ != 1 ){
  199.         die 'usage: Create( $SubKey,$ScalarRef )';
  200.     }
  201.  
  202.     ($SubKey) = @_;
  203.     local ($Result,$SubHandle);
  204.  
  205.     #call the API, and create the object.
  206.     $Result = RegCreateKey($self->{'handle'},$SubKey,$SubHandle);
  207.     $_[1] = _new ( $SubHandle );
  208.     if (!$_[1]){
  209.         return(0);
  210.     }
  211.     #error checking
  212.  
  213.      if(!$Result){
  214.         $! = Win32::GetLastError();
  215.     }
  216.  
  217.     return($Result);
  218.  
  219. }
  220.  
  221. #######################################################################
  222. #SetValue
  223. # SetValue sets a value in the current key.
  224. #
  225.  
  226. sub SetValue
  227. {
  228.     my $self = shift;
  229.     if($#_ != 2 ){
  230.         die 'usage: SetValue($SubKey,$Type,$value )';
  231.     }
  232.  
  233.     local($SubKey,$type,$value) = @_;
  234.  
  235.     # set the value.
  236.     $Result = RegSetValue( $self->{'handle'},$SubKey,$type,$value);
  237.     
  238.      if(!$Result){
  239.         $! = Win32::GetLastError();
  240.     }
  241.  
  242.     return($Result);
  243.  
  244. }
  245.  
  246. sub SetValueEx
  247. {
  248.     my $self = shift;
  249.     if($#_ != 3){
  250.         die 'usage: SetValueEx( $SubKey,$Reserved,$type,$value )';
  251.     }
  252.  
  253.     local( $SubKey,$Reserved,$type,$value) =@_;
  254.  
  255.     $Result = RegSetValueEx( $self->{'handle'},$SubKey,$Reserved,$type,$value);
  256.     
  257.     if(!$Result){
  258.         $! = Win32::GetLastError();
  259.     }
  260.  
  261.     return($Result);
  262. }
  263.  
  264. #######################################################################
  265. #QueryValue  and QueryKey
  266. # QueryValue gets information on a value in the current key.
  267. # QueryKey "    "       "       "  key  "       "       "       
  268.  
  269. sub QueryValue
  270. {
  271.     my $self = shift;
  272.  
  273.     if($#_ != 1 ){
  274.         die 'usage: QueryValue( $SubKey,$valueref )';
  275.     }
  276.  
  277.     #Query the value.
  278.     $Result = RegQueryValue( $self->{'handle'}, $_[0], $_[1]);
  279.  
  280.     #check the results.
  281.  
  282.      if(!$Result){
  283.         $! = Win32::GetLastError();
  284.     }
  285.  
  286.     return($Result);
  287. }
  288.  
  289. sub QueryKey
  290. {
  291.     my $garbage;
  292.     my $self = shift;
  293.  
  294.     if($#_ != 2 ){
  295.         die 'usage: QueryKey( $classref, $numberofSubkeys, $numberofVals )';
  296.     }
  297.  
  298.     local ($Result);
  299.  
  300.     $Result = RegQueryInfoKey( $self->{'handle'}, $_[0],
  301.                     $garbage, $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.