In the code below, if the first sub key SOFTWARE\\ActiveState\\ActivePerl does not exist then it fails to get the second key as well even if the second key exists. It works fine only if both keys exist. Please help.
#!/usr/bin/env perl -w use strict; use Win32API::Registry 0.21 qw( :ALL ); &main; sub getRegistryKeys { my ($hKey,$sKey,$sValue) = @_; my ($type, $data, $error, $key); RegOpenKeyEx( $hKey, $sKey, 0, KEY_READ, $key) or warn "Can't ope +n $hKey\\$sKey: ", regLastError(),"\n"; $error = regLastError(); print "\nError after open: $error"; return "error" if ($error ne ""); RegQueryValueEx( $key, $sValue, [], $type, $data, [] ) or warn "C +an't read $hKey\\$sKey: ", regLastError(),"\n"; $error = regLastError(); print "\nError after query: $error"; if ($error ne ""){ RegCloseKey( $key ) or warn "Can't close $hKey\\$sKey: ", reg +LastError(),"\n"; return "error"; } RegCloseKey( $key ) or warn "Can't close $hKey\\$sKey: ", regLast +Error(),"\n"; return "error" if ($error ne ""); return $data; } sub main { my $ret; $ret = getRegistryKeys(HKEY_LOCAL_MACHINE,"SOFTWARE\\ActiveState\\ +ActivePerl1","CurrentVersion"); if ($ret =~ /error/) { print "\nFound error in first attemp..."; $ret = getRegistryKeys(HKEY_LOCAL_MACHINE,"SOFTWARE\\ActiveSta +te\\PerlScript\\1.0","InstallDir"); print "\nSecond attemp: $ret"; } else { print $ret; } }
Output of the script:
Can't open 2147483650\SOFTWARE\ActiveState\ActivePerl: The system cannot find the file specified
Error after open: The system cannot find the file specified
Found error in first attemp...
Error after open: The system cannot find the file specified
Second attemp: error
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |