#! perl # Store help text in variable $syntax = "\nReadReg.pl, Version 1.00 for Windows\n"; $syntax = $syntax."Read a value from the registry\n\n"; $syntax = $syntax."Usage: READREG.PL \"section\" \"key\"\n\n"; $syntax = $syntax."Where: \"section\" is the section name, without brackets\n"; $syntax = $syntax." \"key\" is the key whose value must be read\n\n"; $syntax = $syntax."Arguments should be enclosed in quotes\n\n"; $syntax = $syntax."Example:\nREADREG.PL \"HKEY_CURRENT_USER\\Environment\" \"path\"\n"; $syntax = $syntax."should return the user part of NT's PATH variable\n\n"; $syntax = $syntax."Written by Rob van der Woude\n"; $syntax = $syntax."http://www.robvanderwoude.com\n"; # Check number of command line arguments (2 required) if ( !@ARGV[1] ) { die $syntax; } # Parse command line arguments $section = @ARGV[0]; $key = @ARGV[1]; if ( $section =~ m/^(HKEY_[^\\]+)\\(.+)$/i ) { $hive = uc( $1 ); $tree = $2; } else { die $syntax; } # Initialize the required module use Win32API::Registry 0.21 qw( :ALL ); # Convert specified hive from string to constant if ( $hive eq "HKEY_CLASSES_ROOT" ) { $hkey = HKEY_CLASSES_ROOT; } if ( $hive eq "HKEY_CURRENT_CONFIG" ) { $hkey = HKEY_CURRENT_CONFIG; } if ( $hive eq "HKEY_CURRENT_USER" ) { $hkey = HKEY_CURRENT_USER; } if ( $hive eq "HKEY_LOCAL_MACHINE" ) { $hkey = HKEY_LOCAL_MACHINE; } if ( $hive eq "HKEY_USERS" ) { $hkey = HKEY_USERS; } # Read the value from the registry RegOpenKeyEx( $hkey, $tree, 0, KEY_READ, $handle ) || die "Can't open $section: ".regLastError()."\n"; RegQueryValueEx( $handle, $key, [], $type, $regval, [] ) || die "Can't read $section\\$key: ".regLastError()."\n"; RegCloseKey( $handle ) || die "Can't close $section: ".regLastError()."\n"; # Display the result print "\n[$section]\n$key=$regval\n";