roteme has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I would like to check if registry key is exist by the following simple Perl code:

my $UninstallRegistryPATH = "HKLM\\SOFTWARE\\Microsoft\\Windows\\Curre +ntVersion\\Uninstall\\{30277218-0100-0904-9ABB-000BDB5CF35D}"; my $cmd = "REG QUERY ${UninstallRegistryPATH}"; print "\n----$cmd----\n\n"; my @RegKeys = `$cmd 2>&1`; foreach my $var (@RegKeys) { print "$var" }
Output ERROR is:
----C:\Windows\system32\reg.exe QUERY HKLM\SOFTWARE\Microsoft\Windows\ +CurrentVersion\Uninstall\{30277218-0100-0904-9ABB-000BDB5CF35D}---- ERROR: The system was unable to find the specified registry key or val +ue.

If I run the command ($cmd) from the command line it run successfully.

Do someone have any idea?

Please advice

Thanks

I Failed even if i use the following code:

use Win32::Registry; my $Register = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{0 +34106B5-54B7-467F-B477-5B7DBB492624}'; my $hkey; $HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die print "($!) ($?) ($^E +)\n"; $hkey->Close();

If i remove the last entrance key {034106B5-54B7-467F-B477-5B7DBB492624} and run it like the following is working good:

use Win32::Registry; my $Register = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'; my $hkey; $HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die print "($!) ($?) ($^E +)\n"; $hkey->Close();

My question is why? Why {034106B5-54B7-467F-B477-5B7DBB492624} caused to read problem?

I just need to check if some product is installed

Replies are listed 'Best First'.
Re: Failed to check if registry key is exist
by wwe (Friar) on Feb 27, 2012 at 15:55 UTC
    Why don't you use Win32::TieRegistry module? It allows access to registry without callig external executables.

      I Failed even if i use the following code:

      use Win32::Registry; my $Register = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{0 +34106B5-54B7-467F-B477-5B7DBB492624}'; my $hkey; $HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die print "($!) ($?) ($^E +)\n"; $hkey->Close();

      If i remove the last entrance key {034106B5-54B7-467F-B477-5B7DBB492624} and run it like the following is working good:

      use Win32::Registry; my $Register = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'; my $hkey; $HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die print "($!) ($?) ($^E +)\n"; $hkey->Close();

      My question is why? Why {034106B5-54B7-467F-B477-5B7DBB492624} caused to read problem?

      I just need to check if some product is installed

        Did you ever figure this out? I'm able to reproduce this as well.
Re: Failed to check if registry key is exist
by RMGir (Prior) on Feb 27, 2012 at 13:17 UTC
    I'd suggest replacing your "" in the line where you're creating $UninstallRegistryPATH with single quotes, so you can just paste in the key name and not have to worry about doubling-up the \'s. Get the key name to paste in using regedit - right click on the key and choose "Copy Key Name".

    I don't know if that will fix your issue, but when I do that with a key from my registry in the same subtree (I don't have your particular node installed) in my copy of your script, it works fine.


    Mike

      I've tried your suggestion with single quotes with no success.

      Can you please share your example with me.

      Thanks.

        my $UninstallRegistryPATH = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Window\CurrentVersion\Uninstall +\{3C3901C5-3455-3E0A-A214-0B093A5070A6}'; my $cmd = "REG QUERY ${UninstallRegistryPATH}"; print "\n----$cmd----\n\n"; my @RegKeys = `$cmd 2>&1`; foreach my $var (@RegKeys) { print "$var" }
        Result:
        ----REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVer +sion\Uninstall\{3C3901C5-3455-3E0A-A214-0B093A5070A6}---- ! REG.EXE VERSION 3.0 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall +\{3C3901C5-3455-3E0A-A214-0B093A5070A6} AuthorizedCDFPrefix REG_SZ Comments REG_SZ Contact REG_SZ DisplayVersion REG_SZ 4.0.30319 HelpLink REG_SZ HelpTelephone REG_SZ InstallDate REG_SZ 20120209 InstallLocation REG_SZ InstallSource REG_SZ C:\313aede3e0764106ff44\ ModifyPath REG_EXPAND_SZ MsiExec.exe /X{3C3901C5-3455-3E0A-A +214-0B093A5070A6} NoModify REG_DWORD 0x1 NoRepair REG_DWORD 0x1 Publisher REG_SZ Microsoft Corporation Readme REG_EXPAND_SZ http://go.microsoft.com/fwlink/?LinkId= +164156 Size REG_DWORD 0x9b34 EstimatedSize REG_DWORD 0x206c7b SystemComponent REG_DWORD 0x1 UninstallString REG_EXPAND_SZ MsiExec.exe /X{3C3901C5-3455-3 +E0A-A214-0B093A5070A6} URLInfoAbout REG_SZ http://go.microsoft.com/fwlink/?LinkId=1 +64164 URLUpdateInfo REG_SZ http://go.microsoft.com/fwlink/?LinkId= +164165 VersionMajor REG_DWORD 0x4 VersionMinor REG_DWORD 0x0 WindowsInstaller REG_DWORD 0x1 Version REG_DWORD 0x400766f Language REG_DWORD 0x0 DisplayName REG_SZ Microsoft .NET Framework 4 Client Profile
        </code>

        Mike