home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Win32 / Registry.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  6.3 KB  |  409 lines

  1. package Win32::Registry;
  2.  
  3. require Exporter;       #to export the constants to the main:: space
  4. require DynaLoader;     # to dynuhlode the module.
  5. use Win32::WinError;         # for windows constants.
  6.  
  7. $VERSION = '0.01';
  8.  
  9. @ISA= qw( Exporter DynaLoader );
  10. @EXPORT = qw(
  11.     HKEY_CLASSES_ROOT
  12.     HKEY_CURRENT_USER
  13.     HKEY_LOCAL_MACHINE
  14.     HKEY_PERFORMANCE_DATA
  15.     HKEY_PERFORMANCE_NLSTEXT
  16.     HKEY_PERFORMANCE_TEXT
  17.     HKEY_USERS
  18.     KEY_ALL_ACCESS
  19.     KEY_CREATE_LINK
  20.     KEY_CREATE_SUB_KEY
  21.     KEY_ENUMERATE_SUB_KEYS
  22.     KEY_EXECUTE
  23.     KEY_NOTIFY
  24.     KEY_QUERY_VALUE
  25.     KEY_READ
  26.     KEY_SET_VALUE
  27.     KEY_WRITE
  28.     REG_BINARY
  29.     REG_CREATED_NEW_KEY
  30.     REG_DWORD
  31.     REG_DWORD_BIG_ENDIAN
  32.     REG_DWORD_LITTLE_ENDIAN
  33.     REG_EXPAND_SZ
  34.     REG_FULL_RESOURCE_DESCRIPTOR
  35.     REG_LEGAL_CHANGE_FILTER
  36.     REG_LEGAL_OPTION
  37.     REG_LINK
  38.     REG_MULTI_SZ
  39.     REG_NONE
  40.     REG_NOTIFY_CHANGE_ATTRIBUTES
  41.     REG_NOTIFY_CHANGE_LAST_SET
  42.     REG_NOTIFY_CHANGE_NAME
  43.     REG_NOTIFY_CHANGE_SECURITY
  44.     REG_OPENED_EXISTING_KEY
  45.     REG_OPTION_BACKUP_RESTORE
  46.     REG_OPTION_CREATE_LINK
  47.     REG_OPTION_NON_VOLATILE
  48.     REG_OPTION_RESERVED
  49.     REG_OPTION_VOLATILE
  50.     REG_REFRESH_HIVE
  51.     REG_RESOURCE_LIST
  52.     REG_RESOURCE_REQUIREMENTS_LIST
  53.     REG_SZ
  54.     REG_WHOLE_HIVE_VOLATILE
  55. );
  56.  
  57.  
  58. sub AUTOLOAD {
  59.     my($constname);
  60.     ($constname = $AUTOLOAD) =~ s/.*:://;
  61.     $!=0;
  62.     my $val = constant($constname, @_ ? $_[0] : 0);
  63.     if ($! != 0) {
  64.     if ($! =~ /Invalid/) {
  65.         $AutoLoader::AUTOLOAD = $AUTOLOAD;
  66.         goto &AutoLoader::AUTOLOAD;
  67.     }
  68.     else {
  69.         ($pack,$file,$line) = caller;
  70.         die "Your vendor has not defined Win32::Registry macro $constname, used at $file line $line.";
  71.     }
  72.     }
  73.     eval "sub $AUTOLOAD { $val }";
  74.     goto &$AUTOLOAD;
  75. }
  76.  
  77.  
  78. sub show_me
  79. {
  80.     $self=shift;
  81.     print $self->{'handle'};
  82. }
  83.  
  84. sub _new
  85. {
  86.     my $self={};
  87.     if ($_[0]){
  88.         $self->{'handle'} = $_[0];
  89.         bless $self
  90.         }
  91.     else{
  92.             undef($self);
  93.     }
  94.     $self;
  95. }
  96.  
  97.  
  98.  
  99. $main::HKEY_CLASSES_ROOT = _new(0x80000000);
  100. $main::HKEY_CURRENT_USER = _new(0x80000001);
  101. $main::HKEY_LOCAL_MACHINE = _new(0x80000002);
  102. $main::HKEY_USERS = _new(0x80000003);
  103. $main::HKEY_PERFORMANCE_DATA = _new(0x80000004 );
  104. $main::HKEY_PERFORMANCE_TEXT =_new(0x80000050 );
  105. $main::HKEY_PERFORMANCE_NLSTEXT =_new(0x80000060 );
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. sub Open
  113. {
  114.     my $self = shift;
  115.     
  116.     if( $#_ != 1 ){
  117.         die 'usage: Open( $SubKey, $ObjRef )';
  118.     }
  119.     
  120.     ($SubKey) = @_;
  121.     local ($Result,$SubHandle);
  122.  
  123.     $Result = RegOpenKey($self->{'handle'},$SubKey,$SubHandle);
  124.     $_[1] = _new( $SubHandle );
  125.     
  126.     if (!$_[1] ){
  127.         return 0;
  128.     }
  129.  
  130.      if(!$Result){
  131.         $! = Win32::GetLastError();
  132.     }
  133.  
  134.     return($Result);
  135.  
  136. }
  137.  
  138. sub Close
  139. {
  140.     my $self = shift;
  141.     
  142.     if( $#_ != -1 ){
  143.         die "usage: Close()";
  144.     }
  145.  
  146.     $Result = RegCloseKey( $self->{'handle'});
  147.     undef($self);
  148.  
  149.     if(!$Result){
  150.         $! = Win32::GetLastError();
  151.     }
  152.  
  153.     return($Result);
  154. }
  155.  
  156.  
  157.  
  158. sub Create
  159. {
  160.     my $self = shift;
  161.  
  162.     if($#_ != 1 ){
  163.         die 'usage: Create( $SubKey,$ScalarRef )';
  164.     }
  165.  
  166.     ($SubKey) = @_;
  167.     local ($Result,$SubHandle);
  168.  
  169.     $Result = RegCreateKey($self->{'handle'},$SubKey,$SubHandle);
  170.     $_[1] = _new ( $SubHandle );
  171.     if (!$_[1]){
  172.         return(0);
  173.     }
  174.  
  175.      if(!$Result){
  176.         $! = Win32::GetLastError();
  177.     }
  178.  
  179.     return($Result);
  180.  
  181. }
  182.  
  183.  
  184. sub SetValue
  185. {
  186.     my $self = shift;
  187.     if($#_ != 2 ){
  188.         die 'usage: SetValue($SubKey,$Type,$value )';
  189.     }
  190.  
  191.     local($SubKey,$type,$value) = @_;
  192.  
  193.     $Result = RegSetValue( $self->{'handle'},$SubKey,$type,$value);
  194.     
  195.      if(!$Result){
  196.         $! = Win32::GetLastError();
  197.     }
  198.  
  199.     return($Result);
  200.  
  201. }
  202.  
  203. sub SetValueEx
  204. {
  205.     my $self = shift;
  206.     if($#_ != 3){
  207.         die 'usage: SetValueEx( $SubKey,$Reserved,$type,$value )';
  208.     }
  209.  
  210.     local( $SubKey,$Reserved,$type,$value) =@_;
  211.  
  212.     $Result = RegSetValueEx( $self->{'handle'},$SubKey,$Reserved,$type,$value);
  213.     
  214.     if(!$Result){
  215.         $! = Win32::GetLastError();
  216.     }
  217.  
  218.     return($Result);
  219. }
  220.  
  221.  
  222. sub QueryValue
  223. {
  224.     my $self = shift;
  225.  
  226.     if($#_ != 1 ){
  227.         die 'usage: QueryValue( $SubKey,$valueref )';
  228.     }
  229.  
  230.     $Result = RegQueryValue( $self->{'handle'}, $_[0], $_[1]);
  231.  
  232.  
  233.      if(!$Result){
  234.         $! = Win32::GetLastError();
  235.     }
  236.  
  237.     return($Result);
  238. }
  239.  
  240. sub QueryKey
  241. {
  242.     my $garbage;
  243.     my $self = shift;
  244.  
  245.     if($#_ != 2 ){
  246.         die 'usage: QueryKey( $classref, $numberofSubkeys, $numberofVals )';
  247.     }
  248.  
  249.     local ($Result);
  250.  
  251.     $Result = RegQueryInfoKey( $self->{'handle'}, $_[0], $garbage, $_[1],
  252.                    $garbage, $garbage, $_[2],
  253.                    $garbage, $garbage, $garbage, $garbage);
  254.  
  255.  
  256.      if(!$Result){
  257.         $! = Win32::GetLastError();
  258.     }
  259.     return($Result);
  260. }
  261.  
  262. sub GetKeys
  263. {
  264.     my $self = shift;
  265.     if($#_ != 0 ){
  266.         die 'usage: GetKeys( $arrayref )';
  267.     }
  268.  
  269.     if (ref $_[0] ne ARRAY){
  270.         die "GetKeys requires a list reference as an arguement";
  271.     }
  272.  
  273.     local ($Result,$ValueName,$i,$keyname);
  274.  
  275.     $ValueName="DummyVal";$i=0;
  276.     $Result = 1;
  277.     
  278.     while( $Result ){
  279.         $Result = RegEnumKey( $self->{'handle'},$i++, $keyname );
  280.         if ($Result){
  281.             push( @{$_[0]}, $keyname );
  282.         }
  283.     }
  284.     return(1);
  285.  
  286. }
  287.  
  288. sub GetValues
  289. {
  290.     my $self = shift;
  291.  
  292.     if($#_ != 0 ){
  293.         die 'usage: GetValues( $hashref )';
  294.     }
  295.  
  296.     local ($Result,$ValueName,$i);
  297.  
  298.     $ValueName="DummyVal";$i=0;
  299.     while( $Result=RegEnumValue( $self->{'handle'},
  300.                     $i++,
  301.                     $ValueName,
  302.                     NULL,
  303.                     $ValueType,
  304.                     $ValueData )){
  305.  
  306.         $aref = [ $ValueName, $ValueType,$ValueData ];
  307.  
  308.         $_[0]->{$ValueName} = $aref;
  309.     }
  310.         
  311.     return(1);
  312. }
  313.  
  314.  
  315. sub DeleteKey
  316. {
  317.     my $self = shift;
  318.     local($Result);
  319.     if($#_ != 0 ){
  320.         die 'usage: DeleteKey( $SubKey )';
  321.     }
  322.  
  323.     local( $name ) = @_;
  324.  
  325.     $Result=RegDeleteKey($self->{'handle'},$name);
  326.  
  327.      if(!$Result){
  328.         $! = Win32::GetLastError();
  329.     }
  330.     return($Result);
  331.  
  332. }
  333.  
  334. sub DeleteValue
  335. {
  336.     my $self = shift;
  337.     local( $Result );
  338.  
  339.     if($#_ != 0 ){
  340.         die 'usage: DeleteValue( $SubKey )';
  341.     }
  342.  
  343.     local( $name )=@_;
  344.     
  345.     $Result=RegDeleteValue( $self->{'handle'},$name);
  346.     
  347.     if( !$Result){
  348.         $!=Win32::GetLastError();
  349.     }
  350.  
  351.     return($Result);
  352.  
  353. }
  354.  
  355.  
  356. sub Save
  357. {
  358.     my $self=shift;
  359.  
  360.     if($#_ != 0 ){
  361.         die 'usage: Save( $FileName )';
  362.     }
  363.  
  364.     local( $FileName ) = @_;
  365.  
  366.     $Result=RegSaveKey( $self->{'handle'},$FileName );
  367.  
  368.     if( !$Result){
  369.         $!=Win32::GetLastError();
  370.     }
  371.  
  372.     return($Result);
  373. }
  374.  
  375.  
  376. sub Load
  377. {
  378.     my $self = shift;
  379.     if($#_ != 1 ){
  380.         die 'usage: Load( $SubKey,$FileName )';
  381.     }
  382.  
  383.     local( $SubKey,$FileName) = @_;
  384.  
  385.     $Result=RegLoadKey( $self->{'handle'},$SubKey,$FileName);
  386.  
  387.     if( !$Result){
  388.         $!=Win32::GetLastError();
  389.     }
  390.  
  391.     return($Result);
  392. }
  393.  
  394.  
  395. bootstrap Win32::Registry;
  396.  
  397.  
  398.  
  399. 1;
  400. __END__
  401.  
  402.  
  403.  
  404.  
  405.  
  406.     
  407.  
  408.     
  409.