How do I make the 32-bit Perl read the 64-bit Windows registry?

后端 未结 3 651
情话喂你
情话喂你 2021-01-18 12:20

I have a 32-bit perl installer. Using this I need to be able to install and uninstall both 32- and 64-bit applications.

Installing 32- and 64-bit is fine. Uninstalli

相关标签:
3条回答
  • 2021-01-18 12:54

    As you note in your question, it is possible to specify a 64-bit or 32-bit registry views with the KEY_WOW64_64KEY flag.

    The old Win32API::Registry can specify the 64-bit registry with KEY_WOW64_64KEY, but these days it is better to use TieRegistry's object functions, which wraps the functionality to make it easier to work with the registry:

    #!/usr/bin/perl -w
    use strict;
    use Win32::TieRegistry (Delimiter => '/');
    
    print "registry 64-bit:\n";
    my $mykey = new Win32::TieRegistry
      'HKEY_LOCAL_MACHINE/Software/Microsoft/Windows/CurrentVersion/Uninstall',
      { Access=>Win32::TieRegistry::KEY_READ()|0x0100, Delimiter=>'/' };
    
    print "\tValues are:\n";
    print join("\n\t\t", $mykey->ValueNames);
    print "\n";
    
    #Getting a specific value's value
    #$mykeyval = $mykey->GetValue('Path');
    
    print "\tFiltered subkeys are:\n\t\t";
    print join("\n\t\t", grep(!/\{[-A-Fa-f0-9]+\}/, $mykey->SubKeyNames));
    print "\n";
    
    print "registry 32-bit explicit:\n";
    $mykey = new Win32::TieRegistry
      'HKEY_LOCAL_MACHINE/Software/Microsoft/Windows/CurrentVersion/Uninstall',
      { Access=>Win32::TieRegistry::KEY_READ()|0x0200, Delimiter=>'/' };
    
    print "\tValues are:\n\t\t";
    print join("\n\t\t", $mykey->ValueNames);
    print "\n";
    
    #Getting a specific value's value
    #$mykeyval = $mykey->GetValue('Path');
    
    print "\tFiltered subkeys are:\n\t\t";
    print join("\n\t\t", grep(!/\{[-A-Fa-f0-9]+\}/, $mykey->SubKeyNames));
    print "\n";
    

    This gives results as expected for both the 32-bit and 64-bit keys, and additionally should work the same way in both 32-bit and 64-bit Perl (in theory anyway).

    Note: I needed to specify the full namespace for the KEY_READ() function in my version of Perl to prevent compile errors, and I'm not certain whether there are named values for the 0x0100 and 0x0200 constants, so it is possible this could be prettier. But it works!


    (Adapted from my solution to my question about not being able to read the registry, asked before I knew my problem was related to 64-bit vs. 32-bit).

    0 讨论(0)
  • 2021-01-18 12:55

    You could also call the reg tool directly, instead of the batch file:

    $WINDIR/system32/reg.exe
    

    This is the default location for reg.exe when included with operating system.

    $WINDIR/sysnative/reg.exe
    

    This is the virtual location of the native 64-bit reg.exe when executed from a 32-bit process.

    0 讨论(0)
  • 2021-01-18 13:18

    Yes, you have to use KEY_WOW64_64KEY, there is no other workaround for a 32-bit process. Calling the Win32 API directly from Perl appears possible, judging from this web page.

    0 讨论(0)
提交回复
热议问题