home *** CD-ROM | disk | FTP | other *** search
/ Peanuts NeXT Software Archives / Peanuts-2.iso / Developer / webobjects / extensions / win-nt / NTPerl5-109.exe / Lib / Win32 / REGISTRY.PM < prev    next >
Encoding:
Perl POD Document  |  1996-06-04  |  9.4 KB  |  485 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.     local($constname);
  77.     ($constname = $AUTOLOAD) =~ s/.*:://;
  78.     $val = constant($constname, @_ ? $_[0] : 0);
  79.     if ($! != 0) {
  80.     if ($! =~ /Invalid/) {
  81.         $AutoLoader::AUTOLOAD = $AUTOLOAD;
  82.         goto &AutoLoader::AUTOLOAD;
  83.     }
  84.     else {
  85.         ($pack,$file,$line) = caller;
  86.         die "Your vendor has not defined Win32::Registry macro $constname, used at $file line $line.";
  87.     }
  88.     }
  89.     eval "sub $AUTOLOAD { $val }";
  90.     goto &$AUTOLOAD;
  91. }
  92.  
  93. #######################################################################
  94. # _new is a private constructor, not intended for public use.
  95. #
  96.  
  97. sub show_me
  98. {
  99.     $self=shift;
  100.     print $self->{'handle'};
  101. }
  102.  
  103. sub _new
  104. {
  105.     my $self={};
  106.     if ($_[0]){
  107.         $self->{'handle'} = $_[0];
  108.         bless $self
  109.         }
  110.     else{
  111.             undef($self);
  112.     }
  113.     $self;
  114. }
  115.  
  116. #define the basic registry objects to be exported.
  117. #these had to be hardwired unfortunately.
  118.  
  119.  
  120. $main::HKEY_CLASSES_ROOT = _new(0x80000000);
  121. $main::HKEY_CURRENT_USER = _new(0x80000001);
  122. $main::HKEY_LOCAL_MACHINE = _new(0x80000002);
  123. $main::HKEY_USERS = _new(0x80000003);
  124. $main::HKEY_PERFORMANCE_DATA = _new(0x80000004 );
  125. $main::HKEY_PERFORMANCE_TEXT =_new(0x80000050 );
  126. $main::HKEY_PERFORMANCE_NLSTEXT =_new(0x80000060 );
  127.  
  128.  
  129.  
  130.  
  131.  
  132. #######################################################################
  133. #Open: creates a new Registry object from an existing one.
  134. # usage: $RegObj->Open( "SubKey",$SubKeyObj );
  135. #               $SubKeyObj->Open( "SubberKey", *SubberKeyObj );
  136.  
  137. sub Open
  138. {
  139.     my $self = shift;
  140.     
  141.     if( $#_ != 1 ){
  142.         die 'usage: Open( $SubKey, $ObjRef )';
  143.     }
  144.     
  145.     ($SubKey) = @_;
  146.     local ($Result,$SubHandle);
  147.  
  148.     $Result = RegOpenKey($self->{'handle'},$SubKey,$SubHandle);
  149.     $_[1] = _new( $SubHandle );
  150.     
  151.     if (!$_[1] ){
  152.         return 0;
  153.     }
  154.  
  155.      if(!$Result){
  156.         $! = Win32::GetLastError();
  157.     }
  158.  
  159.     # return a boolean value
  160.     return($Result);
  161.  
  162. }
  163.  
  164. #######################################################################
  165. #Close
  166. # close an open registry key.
  167. #
  168. sub Close
  169. {
  170.     my $self = shift;
  171.     
  172.     if( $#_ != -1 ){
  173.         die "usage: Close()";
  174.     }
  175.  
  176.     $Result = RegCloseKey( $self->{'handle'});
  177.     undef($self);
  178.  
  179.     if(!$Result){
  180.         $! = Win32::GetLastError();
  181.     }
  182.  
  183.     return($Result);
  184. }
  185.  
  186.  
  187. #######################################################################
  188. #Create
  189. # open a subkey.  If it doesn't exist, create it.
  190. #
  191.  
  192. sub Create
  193. {
  194.     my $self = shift;
  195.  
  196.     if($#_ != 1 ){
  197.         die 'usage: Create( $SubKey,$ScalarRef )';
  198.     }
  199.  
  200.     ($SubKey) = @_;
  201.     local ($Result,$SubHandle);
  202.  
  203.     #call the API, and create the object.
  204.     $Result = RegCreateKey($self->{'handle'},$SubKey,$SubHandle);
  205.     $_[1] = _new ( $SubHandle );
  206.     if (!$_[1]){
  207.         return(0);
  208.     }
  209.     #error checking
  210.  
  211.      if(!$Result){
  212.         $! = Win32::GetLastError();
  213.     }
  214.  
  215.     return($Result);
  216.  
  217. }
  218.  
  219. #######################################################################
  220. #SetValue
  221. # SetValue sets a value in the current key.
  222. #
  223.  
  224. sub SetValue
  225. {
  226.     my $self = shift;
  227.     if($#_ != 2 ){
  228.         die 'usage: SetValue($SubKey,$Type,$value )';
  229.     }
  230.  
  231.     local($SubKey,$type,$value) = @_;
  232.  
  233.     # set the value.
  234.     $Result = RegSetValue( $self->{'handle'},$SubKey,$type,$value);
  235.     
  236.      if(!$Result){
  237.         $! = Win32::GetLastError();
  238.     }
  239.  
  240.     return($Result);
  241.  
  242. }
  243.  
  244. sub SetValueEx
  245. {
  246.     my $self = shift;
  247.     if($#_ != 3){
  248.         die 'usage: SetValueEx( $SubKey,$Reserved,$type,$value )';
  249.     }
  250.  
  251.     local( $SubKey,$Reserved,$type,$value) =@_;
  252.  
  253.     $Result = RegSetValueEx( $self->{'handle'},$SubKey,$Reserved,$type,$value);
  254.     
  255.     if(!$Result){
  256.         $! = Win32::GetLastError();
  257.     }
  258.  
  259.     return($Result);
  260. }
  261.  
  262. #######################################################################
  263. #QueryValue  and QueryKey
  264. # QueryValue gets information on a value in the current key.
  265. # QueryKey "    "       "       "  key  "       "       "       
  266.  
  267. sub QueryValue
  268. {
  269.     my $self = shift;
  270.  
  271.     if($#_ != 1 ){
  272.         die 'usage: QueryValue( $SubKey,$valueref )';
  273.     }
  274.  
  275.     #Query the value.
  276.     $Result = RegQueryValue( $self->{'handle'},$_[0],$_[1]);
  277.  
  278.     #check the results.
  279.  
  280.      if(!$Result){
  281.         $! = Win32::GetLastError();
  282.     }
  283.  
  284.     return($Result);
  285. }
  286.  
  287. sub QueryKey
  288. {
  289.     my $self = shift;
  290.  
  291.     if($#_ != 2 ){
  292.         die 'usage: QueryKey( $classref,$numberofSubkeys,$numberofVals )';
  293.     }
  294.  
  295.     local ($Result);
  296.  
  297.     $Result = RegQueryInfoKey( $self->{'handle'},$_[0],
  298.                    $garbage,$garbage,$_[1],
  299.                    $garbage,$garbage,$_[2],
  300.                    $garbage,$garbage,$garbage,$garbage );
  301.  
  302.  
  303.      if(!$Result){
  304.         $! = Win32::GetLastError();
  305.     }
  306.     return($Result);
  307. }
  308.  
  309. #######################################################################
  310. #GetKeys
  311. #Note: the list object must be passed by reference: 
  312. #       $myobj->GetKeys( \@mylist )
  313. sub GetKeys
  314. {
  315.     my $self = shift;
  316.     if($#_ != 0 ){
  317.         die 'usage: GetKeys( $arrayref )';
  318.     }
  319.  
  320.     if (ref $_[0] != ARRAY){
  321.         die "GetKeys requires a list reference as an arguement";
  322.     }
  323.  
  324.     local ($Result,$ValueName,$i,$keyname);
  325.  
  326.     $ValueName="DummyVal";$i=0;
  327.     $Result = 1;
  328.     
  329.     while( $Result ){
  330.         $Result = RegEnumKey( $self->{'handle'},$i++, $keyname );
  331.         if ($Result){
  332.             push( @{$_[0]}, $keyname );
  333.         }
  334.     }
  335.     return(1);
  336.  
  337. }
  338. #######################################################################
  339. #GetValues
  340. # GetValues creates a hash containing 'name'=> ( name,type,data )
  341. # for each value in the current key.
  342.  
  343. sub GetValues
  344. {
  345.     my $self = shift;
  346.  
  347.     if($#_ != 0 ){
  348.         die 'usage: GetValues( $hashref )';
  349.     }
  350.  
  351.     local ($Result,$ValueName,$i);
  352.  
  353.     $ValueName="DummyVal";$i=0;
  354.     while( $Result=RegEnumValue( $self->{'handle'},
  355.                     $i++,
  356.                     $ValueName,
  357.                     NULL,
  358.                     $ValueType,
  359.                     $ValueData )){
  360.  
  361.         $aref = [ $ValueName, $ValueType,$ValueData ];
  362.  
  363.         $_[0]->{$ValueName} = $aref;
  364.     }
  365.         
  366.     return(1);
  367. }
  368.  
  369. #######################################################################
  370. #DeleteKey
  371. # delete a key from the registry.
  372. #  eg: $CLASSES_ROOT->DeleteKey( "KeyNameToDelete");
  373. #
  374.  
  375. sub DeleteKey
  376. {
  377.     my $self = shift;
  378.     local($Result);
  379.     if($#_ != 0 ){
  380.         die 'usage: DeleteKey( $SubKey )';
  381.     }
  382.  
  383.     local( $name ) = @_;
  384.  
  385.     $Result=RegDeleteKey($self->{'handle'},$name);
  386.  
  387.      if(!$Result){
  388.         $! = Win32::GetLastError();
  389.     }
  390.     return($Result);
  391.  
  392. }
  393. #######################################################################
  394. #DeleteValue
  395. # delete a value from the current key in the registry
  396. #  $CLASSES_ROOT->DeleteValue( "\000" );
  397.  
  398. sub DeleteValue
  399. {
  400.     my $self = shift;
  401.     local( $Result );
  402.  
  403.     if($#_ != 0 ){
  404.         die 'usage: DeleteValue( $SubKey )';
  405.     }
  406.  
  407.     local( $name )=@_;
  408.     
  409.     $Result=RegDeleteValue( $self->{'handle'},$name);
  410.     
  411.     if( !$Result){
  412.         $!=Win32::GetLastError();
  413.     }
  414.  
  415.     return($Result);
  416.  
  417. }
  418.  
  419. #######################################################################
  420. #save
  421. #saves the current hive to a file.
  422. #
  423.  
  424. sub Save
  425. {
  426.     my $self=shift;
  427.  
  428.     if($#_ != 0 ){
  429.         die 'usage: Save( $FileName )';
  430.     }
  431.  
  432.     local( $FileName ) = @_;
  433.  
  434.     $Result=RegSaveKey( $self->{'handle'},$FileName );
  435.  
  436.     if( !$Result){
  437.         $!=Win32::GetLastError();
  438.     }
  439.  
  440.     return($Result);
  441. }
  442.  
  443. #######################################################################
  444. #Load
  445. #loads a saved key from a file.
  446.  
  447. sub Load
  448. {
  449.     my $self = shift;
  450.     if($#_ != 1 ){
  451.         die 'usage: Load( $SubKey,$FileName )';
  452.     }
  453.  
  454.     local( $SubKey,$FileName) = @_;
  455.  
  456.     $Result=RegLoadKey( $self->{'handle'},$SubKey,$FileName);
  457.  
  458.     if( !$Result){
  459.         $!=Win32::GetLastError();
  460.     }
  461.  
  462.     return($Result);
  463. }
  464. #######################################################################
  465. # dynamically load in the Registry.pll module.
  466.  
  467.  
  468. bootstrap Win32::Registry;
  469.  
  470. # Preloaded methods go here.
  471.  
  472. #Currently Autoloading is not implemented in Perl for win32
  473. # Autoload methods go after __END__, and are processed by the autosplit program.
  474.  
  475. 1;
  476. __END__
  477.  
  478.  
  479.  
  480.  
  481.  
  482.     
  483.  
  484.     
  485.