Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for readreg.pl

(view source code of readreg.pl as plain text)

  1. #! perl
  2.  
  3. # Store help text in variable
  4. $syntax = "\nReadReg.pl,  Version 1.00 for Windows\n";
  5. $syntax = $syntax."Read a value from the registry\n\n";
  6. $syntax = $syntax."Usage:  READREG.PL  \"section\"  \"key\"\n\n";
  7. $syntax = $syntax."Where:              \"section\"  is the section name, without brackets\n";
  8. $syntax = $syntax."                    \"key\"      is the key whose value must be read\n\n";
  9. $syntax = $syntax."Arguments should be enclosed in quotes\n\n";
  10. $syntax = $syntax."Example:\nREADREG.PL \"HKEY_CURRENT_USER\\Environment\" \"path\"\n";
  11. $syntax = $syntax."should return the user part of NT's PATH variable\n\n";
  12. $syntax = $syntax."Written by Rob van der Woude\n";
  13. $syntax = $syntax."http://www.robvanderwoude.com\n";
  14.  
  15. # Check number of command line arguments (2 required)
  16. if ( !@ARGV[1] ) {
  17. 	die $syntax;
  18. }
  19.  
  20. # Parse command line arguments
  21. $section = @ARGV[0];
  22. $key     = @ARGV[1];
  23. if ( $section =~ m/^(HKEY_[^\\]+)\\(.+)$/i ) {
  24. 	$hive = uc( $1 );
  25. 	$tree = $2;
  26. } else {
  27. 	die $syntax;
  28. }
  29.  
  30. # Initialize the required module
  31. use Win32API::Registry 0.21 qw( :ALL );
  32.  
  33. # Convert specified hive from string to constant
  34. if ( $hive eq "HKEY_CLASSES_ROOT"   ) { $hkey = HKEY_CLASSES_ROOT;   }
  35. if ( $hive eq "HKEY_CURRENT_CONFIG" ) { $hkey = HKEY_CURRENT_CONFIG; }
  36. if ( $hive eq "HKEY_CURRENT_USER"   ) { $hkey = HKEY_CURRENT_USER;   }
  37. if ( $hive eq "HKEY_LOCAL_MACHINE"  ) { $hkey = HKEY_LOCAL_MACHINE;  }
  38. if ( $hive eq "HKEY_USERS"          ) { $hkey = HKEY_USERS;          }
  39.  
  40. # Read the value from the registry
  41. RegOpenKeyEx( $hkey, $tree, 0, KEY_READ, $handle ) || die "Can't open $section: ".regLastError()."\n";
  42. RegQueryValueEx( $handle, $key, [], $type, $regval, [] ) || die "Can't read $section\\$key: ".regLastError()."\n";
  43. RegCloseKey( $handle ) ||  die "Can't close $section: ".regLastError()."\n";
  44.  
  45. # Display the result
  46. print "\n[$section]\n$key=$regval\n";
  47.  

page last modified: 2024-04-16; loaded in 0.0058 seconds